1
mirror of https://github.com/home-assistant/core synced 2024-09-12 15:16:21 +02:00
ha-core/homeassistant/components/worldclock/sensor.py

69 lines
1.9 KiB
Python
Raw Normal View History

"""Support for showing the time in a different time zone."""
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
2019-07-31 21:25:30 +02:00
from homeassistant.const import CONF_NAME, CONF_TIME_ZONE
import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util
2015-10-02 23:49:00 +02:00
2020-06-25 20:41:53 +02:00
CONF_TIME_FORMAT = "time_format"
2019-07-31 21:25:30 +02:00
DEFAULT_NAME = "Worldclock Sensor"
ICON = "mdi:clock"
2020-06-25 20:41:53 +02:00
DEFAULT_TIME_STR_FORMAT = "%H:%M"
2015-10-02 23:49:00 +02:00
2019-07-31 21:25:30 +02: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 20:41:53 +02:00
vol.Optional(CONF_TIME_FORMAT, default=DEFAULT_TIME_STR_FORMAT): cv.string,
2019-07-31 21:25:30 +02:00
}
)
2015-10-02 23:49:00 +02:00
2019-07-31 21:25:30 +02:00
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
2016-10-30 15:23:47 +01:00
"""Set up the World clock sensor."""
name = config.get(CONF_NAME)
time_zone = dt_util.get_time_zone(config.get(CONF_TIME_ZONE))
2020-06-25 20:41:53 +02:00
async_add_entities(
2020-08-27 13:56:20 +02:00
[
WorldClockSensor(
time_zone,
name,
config.get(CONF_TIME_FORMAT),
)
],
True,
2020-06-25 20:41:53 +02:00
)
2015-10-02 23:49:00 +02:00
class WorldClockSensor(SensorEntity):
2016-10-25 07:01:38 +02:00
"""Representation of a World clock sensor."""
2015-10-02 23:49:00 +02:00
2020-06-25 20:41:53 +02:00
def __init__(self, time_zone, name, time_format):
2016-03-08 16:46:34 +01:00
"""Initialize the sensor."""
2015-10-02 23:49:00 +02:00
self._name = name
self._time_zone = time_zone
self._state = None
2020-06-25 20:41:53 +02:00
self._time_format = time_format
2015-10-02 23:49:00 +02:00
@property
def name(self):
2016-03-08 16:46:34 +01:00
"""Return the name of the device."""
2015-10-02 23:49:00 +02:00
return self._name
@property
def state(self):
2016-03-08 16:46:34 +01:00
"""Return the state of the device."""
2015-10-02 23:49:00 +02:00
return self._state
2016-02-05 13:08:17 +01:00
@property
def icon(self):
2016-02-23 06:21:49 +01:00
"""Icon to use in the frontend, if any."""
2016-02-05 13:08:17 +01:00
return ICON
2018-06-02 14:30:54 +02:00
async def async_update(self):
2016-03-08 16:46:34 +01:00
"""Get the time and updates the states."""
2020-06-25 20:41:53 +02:00
self._state = dt_util.now(time_zone=self._time_zone).strftime(self._time_format)