-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
New sensor domain expiry #14067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New sensor domain expiry #14067
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """ | ||
| Counter for the days till domain will expire. | ||
|
|
||
| For more details about this sensor please refer to the documentation at | ||
| https://home-assistant.io/components/sensor.domain_expiry/ | ||
| """ | ||
| import logging | ||
| from datetime import datetime, timedelta | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.components.sensor import PLATFORM_SCHEMA | ||
| from homeassistant.const import (CONF_NAME, CONF_DOMAIN) | ||
| from homeassistant.helpers.entity import Entity | ||
|
|
||
| REQUIREMENTS = ['python-whois==0.6.9'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DEFAULT_NAME = 'Domain Expiry' | ||
|
|
||
| SCAN_INTERVAL = timedelta(hours=24) | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_DOMAIN): cv.string, | ||
| vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string | ||
| }) | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_devices, discovery_info=None): | ||
| """Set up domain expiry sensor.""" | ||
| server_name = config.get(CONF_DOMAIN) | ||
| sensor_name = config.get(CONF_NAME) | ||
|
|
||
| add_devices([DomainExpiry(sensor_name, server_name)], True) | ||
|
|
||
|
|
||
| class DomainExpiry(Entity): | ||
| """Implementation of the domain expiry sensor.""" | ||
|
|
||
| def __init__(self, sensor_name, server_name): | ||
| """Initialize the sensor.""" | ||
| self.server_name = server_name | ||
| self._name = sensor_name | ||
| self._state = None | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return the unit this state is expressed in.""" | ||
| return 'days' | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the state of the sensor.""" | ||
| return self._state | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Icon to use in the frontend, if any.""" | ||
| return 'mdi:earth' | ||
|
|
||
| def update(self): | ||
| """Fetch the domain information.""" | ||
| import whois | ||
| domain = whois.whois(self.server_name) | ||
| if isinstance(domain.expiration_date, datetime): | ||
| expiry = domain.expiration_date - datetime.today() | ||
| self._state = expiry.days | ||
| else: | ||
| _LOGGER.error("Cannot get expiry date for %s", self.server_name) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set
self._state = None?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't change it because expiration date will always be
datetime. This check is for some domains that don't share this information and error will be triggered only for them