2015-05-08 14:59:46 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.time_date
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Date and Time service.
|
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
|
|
|
To use the Date and Time sensor you will need to add something like the
|
|
|
|
following to your config/configuration.yaml
|
|
|
|
|
|
|
|
sensor:
|
|
|
|
platform: time_date
|
2015-05-12 18:28:04 +00:00
|
|
|
display_options:
|
2015-05-08 14:59:46 +00:00
|
|
|
- type: 'time'
|
|
|
|
- type: 'date'
|
2015-05-08 16:50:57 +00:00
|
|
|
- type: 'date_time'
|
|
|
|
- type: 'time_date'
|
2015-05-12 18:28:04 +00:00
|
|
|
- type: 'time_utc'
|
|
|
|
- type: 'beat'
|
2015-05-08 14:59:46 +00:00
|
|
|
|
2015-05-12 18:28:04 +00:00
|
|
|
Variables:
|
2015-05-08 14:59:46 +00:00
|
|
|
|
2015-05-12 18:28:04 +00:00
|
|
|
display_options
|
2015-05-08 14:59:46 +00:00
|
|
|
*Required
|
2015-05-12 18:28:04 +00:00
|
|
|
An array specifying the variables to display.
|
2015-05-08 14:59:46 +00:00
|
|
|
|
2015-05-12 18:28:04 +00:00
|
|
|
These are the variables for the display_options array.:
|
2015-05-08 14:59:46 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
*Required
|
2015-05-08 15:03:42 +00:00
|
|
|
The variable you wish to display, see the configuration example above for a
|
2015-05-12 18:28:04 +00:00
|
|
|
list of all available variables.
|
2015-05-08 14:59:46 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
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',
|
2015-05-12 18:28:04 +00:00
|
|
|
'time_date': 'Time & Date',
|
|
|
|
'beat': 'Time (beat)',
|
|
|
|
'time_utc': 'Time (UTC)',
|
2015-05-08 14:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
""" Get the Time and Date sensor. """
|
|
|
|
|
|
|
|
if hass.config.time_zone is None:
|
|
|
|
_LOGGER.error("Timezone is not set in Home Assistant config")
|
|
|
|
return False
|
|
|
|
|
|
|
|
dev = []
|
2015-05-12 18:28:04 +00:00
|
|
|
for variable in config['display_options']:
|
|
|
|
if variable['type'] not in OPTION_TYPES:
|
|
|
|
_LOGGER.error('Option type: "%s" does not exist', variable['type'])
|
2015-05-08 14:59:46 +00:00
|
|
|
else:
|
|
|
|
dev.append(TimeDateSensor(variable['type']))
|
|
|
|
|
|
|
|
add_devices(dev)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class TimeDateSensor(Entity):
|
|
|
|
""" Implements a Time and Date sensor. """
|
|
|
|
|
2015-05-12 18:28:04 +00:00
|
|
|
def __init__(self, option_type):
|
|
|
|
self._name = OPTION_TYPES[option_type]
|
|
|
|
self.type = option_type
|
2015-05-08 14:59:46 +00:00
|
|
|
self._state = None
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
""" Returns the name of the device. """
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the state of the device. """
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
""" Gets the latest data and updates the states. """
|
2015-05-08 16:50:57 +00:00
|
|
|
|
2015-05-12 18:28:04 +00:00
|
|
|
time_date = dt_util.utcnow()
|
2015-06-15 05:56:55 +00:00
|
|
|
time = dt_util.datetime_to_time_str(dt_util.as_local(time_date))
|
|
|
|
time_utc = dt_util.datetime_to_time_str(time_date)
|
|
|
|
date = dt_util.datetime_to_date_str(dt_util.as_local(time_date))
|
2015-05-12 18:28:04 +00:00
|
|
|
|
|
|
|
# Calculate the beat (Swatch Internet Time) time without date.
|
|
|
|
hours, minutes, seconds = time_date.strftime('%H:%M:%S').split(':')
|
|
|
|
beat = ((int(seconds) + (int(minutes) * 60) + ((int(hours) + 1) *
|
|
|
|
3600)) / 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':
|
|
|
|
self._state = date + ', ' + time
|
|
|
|
elif self.type == 'time_date':
|
|
|
|
self._state = time + ', ' + date
|
2015-05-12 18:28:04 +00:00
|
|
|
elif self.type == 'time_utc':
|
|
|
|
self._state = time_utc
|
|
|
|
elif self.type == 'beat':
|
|
|
|
self._state = '{0:.2f}'.format(beat)
|