2019-04-03 15:40:03 +00:00
|
|
|
"""Get WHOIS information for a given host."""
|
2021-12-28 20:25:09 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-10-24 07:34:06 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
import voluptuous as vol
|
2019-09-21 18:52:46 +00:00
|
|
|
import whois
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2021-03-22 18:50:29 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2021-12-30 21:42:46 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2021-02-15 01:14:48 +00:00
|
|
|
from homeassistant.const import CONF_DOMAIN, CONF_NAME, TIME_DAYS
|
2021-12-28 20:25:09 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2017-10-24 07:34:06 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-12-28 20:25:09 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2021-12-30 08:05:16 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_EXPIRES,
|
|
|
|
ATTR_NAME_SERVERS,
|
|
|
|
ATTR_REGISTRAR,
|
|
|
|
ATTR_UPDATED,
|
|
|
|
DEFAULT_NAME,
|
2021-12-30 21:42:46 +00:00
|
|
|
DOMAIN,
|
2021-12-30 08:05:16 +00:00
|
|
|
LOGGER,
|
|
|
|
)
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2021-12-30 21:42:46 +00:00
|
|
|
SCAN_INTERVAL = timedelta(hours=24)
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_DOMAIN): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-10-24 07:34:06 +00:00
|
|
|
|
|
|
|
|
2021-12-28 20:25:09 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-10-24 07:34:06 +00:00
|
|
|
"""Set up the WHOIS sensor."""
|
2021-12-30 21:42:46 +00:00
|
|
|
LOGGER.warning(
|
|
|
|
"Configuration of the Whois platform in YAML is deprecated and will be "
|
|
|
|
"removed in Home Assistant 2022.4; Your existing configuration "
|
|
|
|
"has been imported into the UI automatically and can be safely removed "
|
|
|
|
"from your configuration.yaml file"
|
|
|
|
)
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data={CONF_DOMAIN: config[CONF_DOMAIN], CONF_NAME: config[CONF_NAME]},
|
|
|
|
)
|
|
|
|
)
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2021-12-30 21:42:46 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the platform from config_entry."""
|
|
|
|
domain = entry.data[CONF_DOMAIN]
|
2017-10-24 07:34:06 +00:00
|
|
|
try:
|
2021-12-30 21:42:46 +00:00
|
|
|
info = await hass.async_add_executor_job(whois.whois, domain)
|
2019-12-06 20:58:32 +00:00
|
|
|
except whois.BaseException as ex: # pylint: disable=broad-except
|
2021-12-30 08:05:16 +00:00
|
|
|
LOGGER.error("Exception %s occurred during WHOIS lookup for %s", ex, domain)
|
2017-10-24 07:34:06 +00:00
|
|
|
return
|
|
|
|
|
2021-12-30 21:42:46 +00:00
|
|
|
if "expiration_date" not in info:
|
|
|
|
LOGGER.error("WHOIS lookup for %s didn't contain an expiration date", domain)
|
|
|
|
return
|
|
|
|
|
|
|
|
async_add_entities([WhoisSensor(domain)], True)
|
|
|
|
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2021-03-22 18:50:29 +00:00
|
|
|
class WhoisSensor(SensorEntity):
|
2017-10-24 07:34:06 +00:00
|
|
|
"""Implementation of a WHOIS sensor."""
|
|
|
|
|
2021-12-08 18:58:46 +00:00
|
|
|
_attr_icon = "mdi:calendar-clock"
|
|
|
|
_attr_native_unit_of_measurement = TIME_DAYS
|
|
|
|
|
2021-12-30 21:42:46 +00:00
|
|
|
def __init__(self, domain: str) -> None:
|
2017-10-24 07:34:06 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-12-30 21:42:46 +00:00
|
|
|
self._attr_name = domain
|
2019-03-10 09:08:13 +00:00
|
|
|
self.whois = whois.whois
|
2017-10-24 07:34:06 +00:00
|
|
|
self._domain = domain
|
|
|
|
|
2021-12-28 20:25:09 +00:00
|
|
|
def _empty_value_and_attributes(self) -> None:
|
2017-12-01 16:36:15 +00:00
|
|
|
"""Empty the state and attributes on an error."""
|
2021-12-08 18:58:46 +00:00
|
|
|
self._attr_native_value = None
|
2021-12-28 20:25:09 +00:00
|
|
|
self._attr_extra_state_attributes = {}
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2021-12-28 20:25:09 +00:00
|
|
|
def update(self) -> None:
|
2017-12-01 16:36:15 +00:00
|
|
|
"""Get the current WHOIS data for the domain."""
|
2017-10-24 07:34:06 +00:00
|
|
|
try:
|
2019-03-10 09:08:13 +00:00
|
|
|
response = self.whois(self._domain)
|
2019-12-06 20:58:32 +00:00
|
|
|
except whois.BaseException as ex: # pylint: disable=broad-except
|
2021-12-30 08:05:16 +00:00
|
|
|
LOGGER.error("Exception %s occurred during WHOIS lookup", ex)
|
2021-12-08 18:58:46 +00:00
|
|
|
self._empty_value_and_attributes()
|
2017-10-24 07:34:06 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if response:
|
2019-07-31 19:25:30 +00:00
|
|
|
if "expiration_date" not in response:
|
2021-12-30 08:05:16 +00:00
|
|
|
LOGGER.error(
|
2017-12-01 16:36:15 +00:00
|
|
|
"Failed to find expiration_date in whois lookup response. "
|
2019-07-31 19:25:30 +00:00
|
|
|
"Did find: %s",
|
|
|
|
", ".join(response.keys()),
|
|
|
|
)
|
2021-12-08 18:58:46 +00:00
|
|
|
self._empty_value_and_attributes()
|
2017-12-01 16:36:15 +00:00
|
|
|
return
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if not response["expiration_date"]:
|
2021-12-30 08:05:16 +00:00
|
|
|
LOGGER.error("Whois response contains empty expiration_date")
|
2021-12-08 18:58:46 +00:00
|
|
|
self._empty_value_and_attributes()
|
2017-12-01 16:36:15 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
attrs = {}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
expiration_date = response["expiration_date"]
|
2019-10-20 08:07:34 +00:00
|
|
|
if isinstance(expiration_date, list):
|
|
|
|
attrs[ATTR_EXPIRES] = expiration_date[0].isoformat()
|
2021-06-17 03:57:46 +00:00
|
|
|
expiration_date = expiration_date[0]
|
2019-10-20 08:07:34 +00:00
|
|
|
else:
|
|
|
|
attrs[ATTR_EXPIRES] = expiration_date.isoformat()
|
2017-12-01 16:36:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if "nameservers" in response:
|
|
|
|
attrs[ATTR_NAME_SERVERS] = " ".join(response["nameservers"])
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if "updated_date" in response:
|
|
|
|
update_date = response["updated_date"]
|
2019-03-13 20:00:58 +00:00
|
|
|
if isinstance(update_date, list):
|
|
|
|
attrs[ATTR_UPDATED] = update_date[0].isoformat()
|
|
|
|
else:
|
|
|
|
attrs[ATTR_UPDATED] = update_date.isoformat()
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if "registrar" in response:
|
|
|
|
attrs[ATTR_REGISTRAR] = response["registrar"]
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
time_delta = expiration_date - expiration_date.now()
|
2017-10-24 07:34:06 +00:00
|
|
|
|
2021-12-08 18:58:46 +00:00
|
|
|
self._attr_extra_state_attributes = attrs
|
|
|
|
self._attr_native_value = time_delta.days
|