2019-04-03 15:40:03 +00:00
|
|
|
"""Support for showing the time in a different time zone."""
|
2022-01-05 17:23:43 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-02-28 19:03:43 +00:00
|
|
|
from datetime import tzinfo
|
|
|
|
|
2016-08-16 19:43:56 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 18:50:29 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_TIME_ZONE
|
2022-01-05 17:23:43 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2016-08-16 19:43:56 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-05 17:23:43 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-12-09 13:47:53 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-10-02 21:49:00 +00:00
|
|
|
|
2020-06-25 18:41:53 +00:00
|
|
|
CONF_TIME_FORMAT = "time_format"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Worldclock Sensor"
|
2020-06-25 18:41:53 +00:00
|
|
|
DEFAULT_TIME_STR_FORMAT = "%H:%M"
|
2015-10-02 21:49:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_TIME_ZONE): cv.time_zone,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2020-06-25 18:41:53 +00:00
|
|
|
vol.Optional(CONF_TIME_FORMAT, default=DEFAULT_TIME_STR_FORMAT): cv.string,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2016-08-16 19:43:56 +00:00
|
|
|
|
2015-10-02 21:49:00 +00:00
|
|
|
|
2022-01-05 17:23:43 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2016-10-30 14:23:47 +00:00
|
|
|
"""Set up the World clock sensor."""
|
2022-01-05 17:23:43 +00:00
|
|
|
time_zone = dt_util.get_time_zone(config[CONF_TIME_ZONE])
|
2020-06-25 18:41:53 +00:00
|
|
|
async_add_entities(
|
2020-08-27 11:56:20 +00:00
|
|
|
[
|
|
|
|
WorldClockSensor(
|
|
|
|
time_zone,
|
2022-02-28 19:03:43 +00:00
|
|
|
config[CONF_NAME],
|
|
|
|
config[CONF_TIME_FORMAT],
|
2020-08-27 11:56:20 +00:00
|
|
|
)
|
|
|
|
],
|
|
|
|
True,
|
2020-06-25 18:41:53 +00:00
|
|
|
)
|
2015-10-02 21:49:00 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:50:29 +00:00
|
|
|
class WorldClockSensor(SensorEntity):
|
2016-10-25 05:01:38 +00:00
|
|
|
"""Representation of a World clock sensor."""
|
2015-10-02 21:49:00 +00:00
|
|
|
|
2022-02-28 19:03:43 +00:00
|
|
|
_attr_icon = "mdi:clock"
|
|
|
|
|
|
|
|
def __init__(self, time_zone: tzinfo | None, name: str, time_format: str) -> None:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2022-02-28 19:03:43 +00:00
|
|
|
self._attr_name = name
|
2015-10-02 21:49:00 +00:00
|
|
|
self._time_zone = time_zone
|
2020-06-25 18:41:53 +00:00
|
|
|
self._time_format = time_format
|
2015-10-02 21:49:00 +00:00
|
|
|
|
2022-02-28 19:03:43 +00:00
|
|
|
async def async_update(self) -> None:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the time and updates the states."""
|
2022-02-28 19:03:43 +00:00
|
|
|
self._attr_native_value = dt_util.now(time_zone=self._time_zone).strftime(
|
|
|
|
self._time_format
|
|
|
|
)
|