hass-core/homeassistant/components/sensor/time_date.py

108 lines
3.2 KiB
Python
Raw Normal View History

2015-05-08 16:59:46 +02:00
"""
2016-02-23 06:21:49 +01:00
Support for showing the date and the time.
2015-05-08 16:59:46 +02:00
2015-10-20 22:15:53 +02:00
For more details about this platform, please refer to the documentation at
2015-11-09 13:12:18 +01:00
https://home-assistant.io/components/sensor.time_date/
2015-05-08 16:59:46 +02:00
"""
import logging
from datetime import timedelta
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_DISPLAY_OPTIONS
2015-05-08 16:59:46 +02:00
import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv
TIME_STR_FORMAT = "%H:%M"
2015-05-08 16:59:46 +02:00
OPTION_TYPES = {
2015-05-08 18:31:48 +02:00
'time': 'Time',
'date': 'Date',
'date_time': 'Date & Time',
'time_date': 'Time & Date',
'beat': 'Internet Time',
'time_utc': 'Time (UTC)',
2015-05-08 16:59:46 +02:00
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_DISPLAY_OPTIONS, default=['time']):
vol.All(cv.ensure_list, [vol.In(OPTION_TYPES)]),
})
_LOGGER = logging.getLogger(__name__)
2016-04-16 00:55:35 -07:00
2015-05-08 16:59:46 +02:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 16:46:34 +01:00
"""Setup the Time and Date sensor."""
2015-05-08 16:59:46 +02:00
if hass.config.time_zone is None:
_LOGGER.error("Timezone is not set in Home Assistant config")
return False
devices = []
for variable in config[CONF_DISPLAY_OPTIONS]:
devices.append(TimeDateSensor(variable))
2015-05-08 16:59:46 +02:00
add_devices(devices)
2015-05-08 16:59:46 +02:00
# pylint: disable=too-few-public-methods
class TimeDateSensor(Entity):
2016-03-08 16:46:34 +01:00
"""Implementation of a Time and Date sensor."""
2015-05-08 16:59:46 +02:00
def __init__(self, option_type):
2016-03-08 16:46:34 +01:00
"""Initialize the sensor."""
self._name = OPTION_TYPES[option_type]
self.type = option_type
2015-05-08 16:59:46 +02:00
self._state = None
self.update()
@property
def name(self):
2016-03-08 16:46:34 +01:00
"""Return the name of the sensor."""
2015-05-08 16:59:46 +02:00
return self._name
@property
def state(self):
2016-03-08 16:46:34 +01:00
"""Return the state of the sensor."""
2015-05-08 16:59:46 +02:00
return self._state
2016-02-04 21:55:22 +01:00
@property
def icon(self):
2016-02-23 06:21:49 +01:00
"""Icon to use in the frontend, if any."""
2016-02-04 21:55:22 +01:00
if "date" in self.type and "time" in self.type:
return "mdi:calendar-clock"
elif "date" in self.type:
return "mdi:calendar"
else:
return "mdi:clock"
2015-05-08 16:59:46 +02:00
def update(self):
2016-03-08 16:46:34 +01:00
"""Get the latest data and updates the states."""
time_date = dt_util.utcnow()
2016-04-16 00:55:35 -07: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()
# Calculate Swatch Internet Time.
time_bmt = time_date + timedelta(hours=1)
delta = timedelta(hours=time_bmt.hour,
minutes=time_bmt.minute,
seconds=time_bmt.second,
microseconds=time_bmt.microsecond)
beat = int((delta.seconds + delta.microseconds / 1000000.0) / 86.4)
2015-05-08 16:59:46 +02:00
if self.type == 'time':
self._state = time
2015-05-08 18:39:28 +02:00
elif self.type == 'date':
self._state = date
elif self.type == 'date_time':
self._state = date + ', ' + time
elif self.type == 'time_date':
self._state = time + ', ' + date
elif self.type == 'time_utc':
self._state = time_utc
elif self.type == 'beat':
self._state = '@{0:03d}'.format(beat)