core/homeassistant/components/whois/sensor.py

146 lines
4.8 KiB
Python
Raw Normal View History

"""Get WHOIS information for a given host."""
2021-12-28 20:25:09 +00:00
from __future__ import annotations
from datetime import timedelta
import voluptuous as vol
2019-09-21 18:52:46 +00:00
import whois
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_DOMAIN, CONF_NAME, TIME_DAYS
2021-12-28 20:25:09 +00:00
from homeassistant.core import HomeAssistant
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
2021-12-30 08:05:16 +00:00
from .const import (
ATTR_EXPIRES,
ATTR_NAME_SERVERS,
ATTR_REGISTRAR,
ATTR_UPDATED,
DEFAULT_NAME,
DOMAIN,
2021-12-30 08:05:16 +00:00
LOGGER,
)
SCAN_INTERVAL = timedelta(hours=24)
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,
}
)
2021-12-28 20:25:09 +00:00
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the WHOIS sensor."""
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]},
)
)
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]
try:
info = await hass.async_add_executor_job(whois.whois, domain)
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)
return
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)
class WhoisSensor(SensorEntity):
"""Implementation of a WHOIS sensor."""
_attr_icon = "mdi:calendar-clock"
_attr_native_unit_of_measurement = TIME_DAYS
def __init__(self, domain: str) -> None:
"""Initialize the sensor."""
self._attr_name = domain
self.whois = whois.whois
self._domain = domain
2021-12-28 20:25:09 +00:00
def _empty_value_and_attributes(self) -> None:
"""Empty the state and attributes on an error."""
self._attr_native_value = None
2021-12-28 20:25:09 +00:00
self._attr_extra_state_attributes = {}
2021-12-28 20:25:09 +00:00
def update(self) -> None:
"""Get the current WHOIS data for the domain."""
try:
response = self.whois(self._domain)
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)
self._empty_value_and_attributes()
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(
"Failed to find expiration_date in whois lookup response. "
2019-07-31 19:25:30 +00:00
"Did find: %s",
", ".join(response.keys()),
)
self._empty_value_and_attributes()
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")
self._empty_value_and_attributes()
return
attrs = {}
2019-07-31 19:25:30 +00:00
expiration_date = response["expiration_date"]
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]
else:
attrs[ATTR_EXPIRES] = expiration_date.isoformat()
2019-07-31 19:25:30 +00:00
if "nameservers" in response:
attrs[ATTR_NAME_SERVERS] = " ".join(response["nameservers"])
2019-07-31 19:25:30 +00:00
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()
2019-07-31 19:25:30 +00:00
if "registrar" in response:
attrs[ATTR_REGISTRAR] = response["registrar"]
2019-07-31 19:25:30 +00:00
time_delta = expiration_date - expiration_date.now()
self._attr_extra_state_attributes = attrs
self._attr_native_value = time_delta.days