From 2ca97f63090676edbedd06cac3f0b1cf402daaef Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 28 Feb 2022 20:03:43 +0100 Subject: [PATCH] Code quality improvements for Worldclock (#67392) --- .strict-typing | 1 + homeassistant/components/worldclock/sensor.py | 37 ++++++------------- mypy.ini | 11 ++++++ 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/.strict-typing b/.strict-typing index 621c6c315fc..f6908fc39a6 100644 --- a/.strict-typing +++ b/.strict-typing @@ -217,6 +217,7 @@ homeassistant.components.websocket_api.* homeassistant.components.wemo.* homeassistant.components.whois.* homeassistant.components.wiz.* +homeassistant.components.worldclock.* homeassistant.components.zodiac.* homeassistant.components.zeroconf.* homeassistant.components.zone.* diff --git a/homeassistant/components/worldclock/sensor.py b/homeassistant/components/worldclock/sensor.py index 069ca5c55e7..e626add2534 100644 --- a/homeassistant/components/worldclock/sensor.py +++ b/homeassistant/components/worldclock/sensor.py @@ -1,6 +1,8 @@ """Support for showing the time in a different time zone.""" from __future__ import annotations +from datetime import tzinfo + import voluptuous as vol from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity @@ -14,7 +16,6 @@ import homeassistant.util.dt as dt_util CONF_TIME_FORMAT = "time_format" DEFAULT_NAME = "Worldclock Sensor" -ICON = "mdi:clock" DEFAULT_TIME_STR_FORMAT = "%H:%M" PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( @@ -33,15 +34,13 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the World clock sensor.""" - name = config.get(CONF_NAME) time_zone = dt_util.get_time_zone(config[CONF_TIME_ZONE]) - async_add_entities( [ WorldClockSensor( time_zone, - name, - config.get(CONF_TIME_FORMAT), + config[CONF_NAME], + config[CONF_TIME_FORMAT], ) ], True, @@ -51,28 +50,16 @@ async def async_setup_platform( class WorldClockSensor(SensorEntity): """Representation of a World clock sensor.""" - def __init__(self, time_zone, name, time_format): + _attr_icon = "mdi:clock" + + def __init__(self, time_zone: tzinfo | None, name: str, time_format: str) -> None: """Initialize the sensor.""" - self._name = name + self._attr_name = name self._time_zone = time_zone - self._state = None self._time_format = time_format - @property - def name(self): - """Return the name of the device.""" - return self._name - - @property - def native_value(self): - """Return the state of the device.""" - return self._state - - @property - def icon(self): - """Icon to use in the frontend, if any.""" - return ICON - - async def async_update(self): + async def async_update(self) -> None: """Get the time and updates the states.""" - self._state = dt_util.now(time_zone=self._time_zone).strftime(self._time_format) + self._attr_native_value = dt_util.now(time_zone=self._time_zone).strftime( + self._time_format + ) diff --git a/mypy.ini b/mypy.ini index f817a5c58a6..55d608c8628 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2188,6 +2188,17 @@ no_implicit_optional = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.worldclock.*] +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