Add worldclock custom format (#36157)

This commit is contained in:
Indu Prakash authored and GitHub committed 2020-06-25 11:41:53 -07:00
1 parent 4aedafc73a
commit 15773cb3e0
2 files changed
+33 -9

No files matched your search

+10 -6
View File
@@ -9,18 +9,19 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
import homeassistant.util.dt as dt_util
CONF_TIME_FORMAT = "time_format"
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "Worldclock Sensor"
ICON = "mdi:clock"
TIME_STR_FORMAT = "%H:%M"
DEFAULT_TIME_STR_FORMAT = "%H:%M"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_TIME_ZONE): cv.time_zone,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_TIME_FORMAT, default=DEFAULT_TIME_STR_FORMAT): cv.string,
}
)
@@ -30,17 +31,20 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
name = config.get(CONF_NAME)
time_zone = dt_util.get_time_zone(config.get(CONF_TIME_ZONE))
async_add_entities([WorldClockSensor(time_zone, name)], True)
async_add_entities(
[WorldClockSensor(time_zone, name, config.get(CONF_TIME_FORMAT),)], True,
)
class WorldClockSensor(Entity):
"""Representation of a World clock sensor."""
def __init__(self, time_zone, name):
def __init__(self, time_zone, name, time_format):
"""Initialize the sensor."""
self._name = name
self._time_zone = time_zone
self._state = None
self._time_format = time_format
@property
def name(self):
@@ -59,4 +63,4 @@ class WorldClockSensor(Entity):
async def async_update(self):
"""Get the time and updates the states."""
self._state = dt_util.now(time_zone=self._time_zone).strftime(TIME_STR_FORMAT)
self._state = dt_util.now(time_zone=self._time_zone).strftime(self._time_format)