From e78c656bfe3cad6a3ca50d2a6d10cb2bcc0c0b2f Mon Sep 17 00:00:00 2001 From: Yuval Aboulafia Date: Wed, 9 Jun 2021 15:30:33 +0300 Subject: [PATCH] Static typing for Uptime (#51638) * uptime typing * Clean up name type Co-authored-by: Martin Hjelmare --- .strict-typing | 1 + homeassistant/components/uptime/sensor.py | 29 +++++++++++++++-------- mypy.ini | 11 +++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/.strict-typing b/.strict-typing index b44b04dd606..9a25425d39e 100644 --- a/.strict-typing +++ b/.strict-typing @@ -71,6 +71,7 @@ homeassistant.components.systemmonitor.* homeassistant.components.tcp.* homeassistant.components.tts.* homeassistant.components.upcloud.* +homeassistant.components.uptime.* homeassistant.components.vacuum.* homeassistant.components.water_heater.* homeassistant.components.weather.* diff --git a/homeassistant/components/uptime/sensor.py b/homeassistant/components/uptime/sensor.py index 7e79c2fbb5e..98c673b8878 100644 --- a/homeassistant/components/uptime/sensor.py +++ b/homeassistant/components/uptime/sensor.py @@ -1,14 +1,18 @@ """Platform to retrieve uptime for Home Assistant.""" +from __future__ import annotations import voluptuous as vol -from homeassistant.components.sensor import ( +from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity +from homeassistant.const import ( + CONF_NAME, + CONF_UNIT_OF_MEASUREMENT, DEVICE_CLASS_TIMESTAMP, - PLATFORM_SCHEMA, - SensorEntity, ) -from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT +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 import homeassistant.util.dt as dt_util DEFAULT_NAME = "Uptime" @@ -26,9 +30,14 @@ PLATFORM_SCHEMA = vol.All( ) -async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): +async def async_setup_platform( + hass: HomeAssistant, + config: ConfigType, + async_add_entities: AddEntitiesCallback, + discovery_info: DiscoveryInfoType | None = None, +) -> None: """Set up the uptime sensor platform.""" - name = config.get(CONF_NAME) + name = config[CONF_NAME] async_add_entities([UptimeSensor(name)], True) @@ -36,23 +45,23 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= class UptimeSensor(SensorEntity): """Representation of an uptime sensor.""" - def __init__(self, name): + def __init__(self, name: str) -> None: """Initialize the uptime sensor.""" self._name = name self._state = dt_util.now().isoformat() @property - def name(self): + def name(self) -> str: """Return the name of the sensor.""" return self._name @property - def device_class(self): + def device_class(self) -> str: """Return device class.""" return DEVICE_CLASS_TIMESTAMP @property - def state(self): + def state(self) -> str: """Return the state of the sensor.""" return self._state diff --git a/mypy.ini b/mypy.ini index d501056ecec..89a4b6fc122 100644 --- a/mypy.ini +++ b/mypy.ini @@ -792,6 +792,17 @@ no_implicit_optional = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.uptime.*] +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.vacuum.*] check_untyped_defs = true disallow_incomplete_defs = true