From 43dc12b1c871e3f50de949ca226d3645471946e9 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 28 Dec 2021 21:25:09 +0100 Subject: [PATCH] Add strict typing to Whois (#62959) --- .strict-typing | 1 + homeassistant/components/whois/sensor.py | 24 +++++++++++++++++------- mypy.ini | 11 +++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/.strict-typing b/.strict-typing index 3117c6ed908..e0ab8b83ac7 100644 --- a/.strict-typing +++ b/.strict-typing @@ -158,6 +158,7 @@ homeassistant.components.watttime.* homeassistant.components.weather.* homeassistant.components.websocket_api.* homeassistant.components.wemo.* +homeassistant.components.whois.* homeassistant.components.zodiac.* homeassistant.components.zeroconf.* homeassistant.components.zone.* diff --git a/homeassistant/components/whois/sensor.py b/homeassistant/components/whois/sensor.py index 3742ed90131..a7b39d098eb 100644 --- a/homeassistant/components/whois/sensor.py +++ b/homeassistant/components/whois/sensor.py @@ -1,4 +1,6 @@ """Get WHOIS information for a given host.""" +from __future__ import annotations + from datetime import timedelta import logging @@ -7,7 +9,10 @@ import whois from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.const import CONF_DOMAIN, CONF_NAME, TIME_DAYS +from homeassistant.core import HomeAssistant import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType _LOGGER = logging.getLogger(__name__) @@ -28,10 +33,15 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( ) -def setup_platform(hass, config, add_entities, discovery_info=None): +def setup_platform( + hass: HomeAssistant, + config: ConfigType, + add_entities: AddEntitiesCallback, + discovery_info: DiscoveryInfoType | None = None, +) -> None: """Set up the WHOIS sensor.""" - domain = config.get(CONF_DOMAIN) - name = config.get(CONF_NAME) + domain = config[CONF_DOMAIN] + name = config[CONF_NAME] try: if "expiration_date" in whois.whois(domain): @@ -52,18 +62,18 @@ class WhoisSensor(SensorEntity): _attr_icon = "mdi:calendar-clock" _attr_native_unit_of_measurement = TIME_DAYS - def __init__(self, name, domain): + def __init__(self, name: str, domain: str) -> None: """Initialize the sensor.""" self.whois = whois.whois self._domain = domain self._attr_name = name - def _empty_value_and_attributes(self): + def _empty_value_and_attributes(self) -> None: """Empty the state and attributes on an error.""" self._attr_native_value = None - self._attr_extra_state_attributes = None + self._attr_extra_state_attributes = {} - def update(self): + def update(self) -> None: """Get the current WHOIS data for the domain.""" try: response = self.whois(self._domain) diff --git a/mypy.ini b/mypy.ini index 53b022dd98c..ab9f02990b1 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1749,6 +1749,17 @@ no_implicit_optional = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.whois.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +no_implicit_optional = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.zodiac.*] check_untyped_defs = true disallow_incomplete_defs = true