This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+30 -29
View File
@@ -11,21 +11,23 @@ from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
CONF_DOMAIN = 'domain'
CONF_DOMAIN = "domain"
DEFAULT_NAME = 'Whois'
DEFAULT_NAME = "Whois"
ATTR_EXPIRES = 'expires'
ATTR_NAME_SERVERS = 'name_servers'
ATTR_REGISTRAR = 'registrar'
ATTR_UPDATED = 'updated'
ATTR_EXPIRES = "expires"
ATTR_NAME_SERVERS = "name_servers"
ATTR_REGISTRAR = "registrar"
ATTR_UPDATED = "updated"
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
})
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_entities, discovery_info=None):
@@ -36,16 +38,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
name = config.get(CONF_NAME)
try:
if 'expiration_date' in whois.whois(domain):
if "expiration_date" in whois.whois(domain):
add_entities([WhoisSensor(name, domain)], True)
else:
_LOGGER.error(
"WHOIS lookup for %s didn't contain expiration_date",
domain)
_LOGGER.error("WHOIS lookup for %s didn't contain expiration_date", domain)
return
except whois.BaseException as ex:
_LOGGER.error(
"Exception %s occurred during WHOIS lookup for %s", ex, domain)
_LOGGER.error("Exception %s occurred during WHOIS lookup for %s", ex, domain)
return
@@ -72,12 +71,12 @@ class WhoisSensor(Entity):
@property
def icon(self):
"""Return the icon to represent this sensor."""
return 'mdi:calendar-clock'
return "mdi:calendar-clock"
@property
def unit_of_measurement(self):
"""Return the unit of measurement to present the value in."""
return 'days'
return "days"
@property
def state(self):
@@ -106,37 +105,39 @@ class WhoisSensor(Entity):
return
if response:
if 'expiration_date' not in response:
if "expiration_date" not in response:
_LOGGER.error(
"Failed to find expiration_date in whois lookup response. "
"Did find: %s", ', '.join(response.keys()))
"Did find: %s",
", ".join(response.keys()),
)
self._empty_state_and_attributes()
return
if not response['expiration_date']:
if not response["expiration_date"]:
_LOGGER.error("Whois response contains empty expiration_date")
self._empty_state_and_attributes()
return
attrs = {}
expiration_date = response['expiration_date']
expiration_date = response["expiration_date"]
attrs[ATTR_EXPIRES] = expiration_date.isoformat()
if 'nameservers' in response:
attrs[ATTR_NAME_SERVERS] = ' '.join(response['nameservers'])
if "nameservers" in response:
attrs[ATTR_NAME_SERVERS] = " ".join(response["nameservers"])
if 'updated_date' in response:
update_date = response['updated_date']
if "updated_date" in response:
update_date = response["updated_date"]
if isinstance(update_date, list):
attrs[ATTR_UPDATED] = update_date[0].isoformat()
else:
attrs[ATTR_UPDATED] = update_date.isoformat()
if 'registrar' in response:
attrs[ATTR_REGISTRAR] = response['registrar']
if "registrar" in response:
attrs[ATTR_REGISTRAR] = response["registrar"]
time_delta = (expiration_date - expiration_date.now())
time_delta = expiration_date - expiration_date.now()
self._attributes = attrs
self._state = time_delta.days