Add strict typing to Whois (#62959)

This commit is contained in:
Franck Nijhof 2021-12-28 21:25:09 +01:00 committed by GitHub
parent 53f4a3d8bc
commit 43dc12b1c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 7 deletions

View File

@ -158,6 +158,7 @@ homeassistant.components.watttime.*
homeassistant.components.weather.* homeassistant.components.weather.*
homeassistant.components.websocket_api.* homeassistant.components.websocket_api.*
homeassistant.components.wemo.* homeassistant.components.wemo.*
homeassistant.components.whois.*
homeassistant.components.zodiac.* homeassistant.components.zodiac.*
homeassistant.components.zeroconf.* homeassistant.components.zeroconf.*
homeassistant.components.zone.* homeassistant.components.zone.*

View File

@ -1,4 +1,6 @@
"""Get WHOIS information for a given host.""" """Get WHOIS information for a given host."""
from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import logging import logging
@ -7,7 +9,10 @@ import whois
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CONF_DOMAIN, CONF_NAME, TIME_DAYS from homeassistant.const import CONF_DOMAIN, CONF_NAME, TIME_DAYS
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv 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__) _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.""" """Set up the WHOIS sensor."""
domain = config.get(CONF_DOMAIN) domain = config[CONF_DOMAIN]
name = config.get(CONF_NAME) name = config[CONF_NAME]
try: try:
if "expiration_date" in whois.whois(domain): if "expiration_date" in whois.whois(domain):
@ -52,18 +62,18 @@ class WhoisSensor(SensorEntity):
_attr_icon = "mdi:calendar-clock" _attr_icon = "mdi:calendar-clock"
_attr_native_unit_of_measurement = TIME_DAYS _attr_native_unit_of_measurement = TIME_DAYS
def __init__(self, name, domain): def __init__(self, name: str, domain: str) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self.whois = whois.whois self.whois = whois.whois
self._domain = domain self._domain = domain
self._attr_name = name 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.""" """Empty the state and attributes on an error."""
self._attr_native_value = None 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.""" """Get the current WHOIS data for the domain."""
try: try:
response = self.whois(self._domain) response = self.whois(self._domain)

View File

@ -1749,6 +1749,17 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = 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.*] [mypy-homeassistant.components.zodiac.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true