2015-05-08 14:59:46 +00:00
|
|
|
"""
|
2016-02-23 05:21:49 +00:00
|
|
|
Support for showing the date and the time.
|
2015-05-08 14:59:46 +00:00
|
|
|
|
2015-10-20 20:15:53 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/sensor.time_date/
|
2015-05-08 14:59:46 +00:00
|
|
|
"""
|
2016-05-31 14:19:00 +00:00
|
|
|
from datetime import timedelta
|
2016-10-30 14:21:23 +00:00
|
|
|
import logging
|
2016-08-16 19:43:56 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-05-23 23:00:26 +00:00
|
|
|
from homeassistant.core import callback
|
2016-08-16 19:43:56 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import CONF_DISPLAY_OPTIONS
|
2015-05-08 14:59:46 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-08-16 19:43:56 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-10-30 14:21:23 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2017-05-23 23:00:26 +00:00
|
|
|
from homeassistant.helpers.event import async_track_point_in_utc_time
|
2016-08-16 19:43:56 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-10-30 14:21:23 +00:00
|
|
|
TIME_STR_FORMAT = '%H:%M'
|
2015-05-08 14:59:46 +00:00
|
|
|
|
2015-05-12 18:28:04 +00:00
|
|
|
OPTION_TYPES = {
|
2015-05-08 16:31:48 +00:00
|
|
|
'time': 'Time',
|
|
|
|
'date': 'Date',
|
2015-05-08 16:50:57 +00:00
|
|
|
'date_time': 'Date & Time',
|
2019-03-21 07:55:30 +00:00
|
|
|
'date_time_iso': 'Date & Time ISO',
|
2015-05-12 18:28:04 +00:00
|
|
|
'time_date': 'Time & Date',
|
2016-05-31 14:19:00 +00:00
|
|
|
'beat': 'Internet Time',
|
2015-05-12 18:28:04 +00:00
|
|
|
'time_utc': 'Time (UTC)',
|
2015-05-08 14:59:46 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 19:43:56 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Optional(CONF_DISPLAY_OPTIONS, default=['time']):
|
|
|
|
vol.All(cv.ensure_list, [vol.In(OPTION_TYPES)]),
|
|
|
|
})
|
|
|
|
|
2015-05-08 14:59:46 +00:00
|
|
|
|
2018-10-01 06:55:43 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities,
|
|
|
|
discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Time and Date sensor."""
|
2015-05-08 14:59:46 +00:00
|
|
|
if hass.config.time_zone is None:
|
2016-10-30 14:21:23 +00:00
|
|
|
_LOGGER.error("Timezone is not set in Home Assistant configuration")
|
2015-05-08 14:59:46 +00:00
|
|
|
return False
|
|
|
|
|
2016-08-16 19:43:56 +00:00
|
|
|
devices = []
|
|
|
|
for variable in config[CONF_DISPLAY_OPTIONS]:
|
2017-05-23 23:00:26 +00:00
|
|
|
device = TimeDateSensor(hass, variable)
|
|
|
|
async_track_point_in_utc_time(
|
|
|
|
hass, device.point_in_time_listener, device.get_next_interval())
|
|
|
|
devices.append(device)
|
2015-05-08 14:59:46 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(devices, True)
|
2015-05-08 14:59:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TimeDateSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Implementation of a Time and Date sensor."""
|
2015-05-08 14:59:46 +00:00
|
|
|
|
2017-05-23 23:00:26 +00:00
|
|
|
def __init__(self, hass, option_type):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2015-05-12 18:28:04 +00:00
|
|
|
self._name = OPTION_TYPES[option_type]
|
|
|
|
self.type = option_type
|
2015-05-08 14:59:46 +00:00
|
|
|
self._state = None
|
2017-05-23 23:00:26 +00:00
|
|
|
self.hass = hass
|
|
|
|
|
|
|
|
self._update_internal_state(dt_util.utcnow())
|
2015-05-08 14:59:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-05-08 14:59:46 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2015-05-08 14:59:46 +00:00
|
|
|
return self._state
|
|
|
|
|
2016-02-04 20:55:22 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Icon to use in the frontend, if any."""
|
2016-10-30 14:21:23 +00:00
|
|
|
if 'date' in self.type and 'time' in self.type:
|
|
|
|
return 'mdi:calendar-clock'
|
2018-07-23 08:16:05 +00:00
|
|
|
if 'date' in self.type:
|
2016-10-30 14:21:23 +00:00
|
|
|
return 'mdi:calendar'
|
2017-07-06 06:30:01 +00:00
|
|
|
return 'mdi:clock'
|
2016-02-04 20:55:22 +00:00
|
|
|
|
2017-05-23 23:00:26 +00:00
|
|
|
def get_next_interval(self, now=None):
|
|
|
|
"""Compute next time an update should occur."""
|
|
|
|
if now is None:
|
|
|
|
now = dt_util.utcnow()
|
|
|
|
if self.type == 'date':
|
2017-11-20 03:41:30 +00:00
|
|
|
now = dt_util.start_of_local_day(dt_util.as_local(now))
|
2017-05-23 23:00:26 +00:00
|
|
|
return now + timedelta(seconds=86400)
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.type == 'beat':
|
2017-05-23 23:00:26 +00:00
|
|
|
interval = 86.4
|
|
|
|
else:
|
|
|
|
interval = 60
|
|
|
|
timestamp = int(dt_util.as_timestamp(now))
|
|
|
|
delta = interval - (timestamp % interval)
|
|
|
|
return now + timedelta(seconds=delta)
|
|
|
|
|
|
|
|
def _update_internal_state(self, time_date):
|
2016-04-16 07:55:35 +00:00
|
|
|
time = dt_util.as_local(time_date).strftime(TIME_STR_FORMAT)
|
|
|
|
time_utc = time_date.strftime(TIME_STR_FORMAT)
|
|
|
|
date = dt_util.as_local(time_date).date().isoformat()
|
2015-05-12 18:28:04 +00:00
|
|
|
|
2016-05-31 14:19:00 +00:00
|
|
|
# Calculate Swatch Internet Time.
|
|
|
|
time_bmt = time_date + timedelta(hours=1)
|
2016-10-30 14:21:23 +00:00
|
|
|
delta = timedelta(
|
|
|
|
hours=time_bmt.hour, minutes=time_bmt.minute,
|
|
|
|
seconds=time_bmt.second, microseconds=time_bmt.microsecond)
|
2016-05-31 14:19:00 +00:00
|
|
|
beat = int((delta.seconds + delta.microseconds / 1000000.0) / 86.4)
|
2015-05-08 16:50:57 +00:00
|
|
|
|
2015-05-08 14:59:46 +00:00
|
|
|
if self.type == 'time':
|
2015-05-08 16:50:57 +00:00
|
|
|
self._state = time
|
2015-05-08 16:39:28 +00:00
|
|
|
elif self.type == 'date':
|
2015-05-08 16:50:57 +00:00
|
|
|
self._state = date
|
|
|
|
elif self.type == 'date_time':
|
2016-10-30 14:21:23 +00:00
|
|
|
self._state = '{}, {}'.format(date, time)
|
2015-05-08 16:50:57 +00:00
|
|
|
elif self.type == 'time_date':
|
2016-10-30 14:21:23 +00:00
|
|
|
self._state = '{}, {}'.format(time, date)
|
2015-05-12 18:28:04 +00:00
|
|
|
elif self.type == 'time_utc':
|
|
|
|
self._state = time_utc
|
|
|
|
elif self.type == 'beat':
|
2016-05-31 14:19:00 +00:00
|
|
|
self._state = '@{0:03d}'.format(beat)
|
2019-03-21 07:55:30 +00:00
|
|
|
elif self.type == 'date_time_iso':
|
|
|
|
self._state = dt_util.parse_datetime(
|
|
|
|
'{} {}'.format(date, time)).isoformat()
|
2017-05-23 23:00:26 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def point_in_time_listener(self, time_date):
|
|
|
|
"""Get the latest data and update state."""
|
|
|
|
self._update_internal_state(time_date)
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-05-23 23:00:26 +00:00
|
|
|
async_track_point_in_utc_time(
|
|
|
|
self.hass, self.point_in_time_listener, self.get_next_interval())
|