mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Code quality improvements for Worldclock (#67392)
This commit is contained in:
parent
c456b4c646
commit
2ca97f6309
@ -217,6 +217,7 @@ homeassistant.components.websocket_api.*
|
|||||||
homeassistant.components.wemo.*
|
homeassistant.components.wemo.*
|
||||||
homeassistant.components.whois.*
|
homeassistant.components.whois.*
|
||||||
homeassistant.components.wiz.*
|
homeassistant.components.wiz.*
|
||||||
|
homeassistant.components.worldclock.*
|
||||||
homeassistant.components.zodiac.*
|
homeassistant.components.zodiac.*
|
||||||
homeassistant.components.zeroconf.*
|
homeassistant.components.zeroconf.*
|
||||||
homeassistant.components.zone.*
|
homeassistant.components.zone.*
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
"""Support for showing the time in a different time zone."""
|
"""Support for showing the time in a different time zone."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import tzinfo
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||||
@ -14,7 +16,6 @@ import homeassistant.util.dt as dt_util
|
|||||||
CONF_TIME_FORMAT = "time_format"
|
CONF_TIME_FORMAT = "time_format"
|
||||||
|
|
||||||
DEFAULT_NAME = "Worldclock Sensor"
|
DEFAULT_NAME = "Worldclock Sensor"
|
||||||
ICON = "mdi:clock"
|
|
||||||
DEFAULT_TIME_STR_FORMAT = "%H:%M"
|
DEFAULT_TIME_STR_FORMAT = "%H:%M"
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
@ -33,15 +34,13 @@ async def async_setup_platform(
|
|||||||
discovery_info: DiscoveryInfoType | None = None,
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the World clock sensor."""
|
"""Set up the World clock sensor."""
|
||||||
name = config.get(CONF_NAME)
|
|
||||||
time_zone = dt_util.get_time_zone(config[CONF_TIME_ZONE])
|
time_zone = dt_util.get_time_zone(config[CONF_TIME_ZONE])
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
WorldClockSensor(
|
WorldClockSensor(
|
||||||
time_zone,
|
time_zone,
|
||||||
name,
|
config[CONF_NAME],
|
||||||
config.get(CONF_TIME_FORMAT),
|
config[CONF_TIME_FORMAT],
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
True,
|
True,
|
||||||
@ -51,28 +50,16 @@ async def async_setup_platform(
|
|||||||
class WorldClockSensor(SensorEntity):
|
class WorldClockSensor(SensorEntity):
|
||||||
"""Representation of a World clock sensor."""
|
"""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."""
|
"""Initialize the sensor."""
|
||||||
self._name = name
|
self._attr_name = name
|
||||||
self._time_zone = time_zone
|
self._time_zone = time_zone
|
||||||
self._state = None
|
|
||||||
self._time_format = time_format
|
self._time_format = time_format
|
||||||
|
|
||||||
@property
|
async def async_update(self) -> None:
|
||||||
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):
|
|
||||||
"""Get the time and updates the states."""
|
"""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
|
||||||
|
)
|
||||||
|
11
mypy.ini
11
mypy.ini
@ -2188,6 +2188,17 @@ no_implicit_optional = true
|
|||||||
warn_return_any = true
|
warn_return_any = true
|
||||||
warn_unreachable = 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.*]
|
[mypy-homeassistant.components.zodiac.*]
|
||||||
check_untyped_defs = true
|
check_untyped_defs = true
|
||||||
disallow_incomplete_defs = true
|
disallow_incomplete_defs = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user