Code quality improvements for Worldclock (#67392)

This commit is contained in:
Franck Nijhof
2022-02-28 20:03:43 +01:00
committed by GitHub
parent c456b4c646
commit 2ca97f6309
3 changed files with 24 additions and 25 deletions

View File

@ -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.*

View File

@ -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
)

View File

@ -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