core/homeassistant/components/sensor/worldclock.py

65 lines
1.7 KiB
Python
Raw Normal View History

2015-10-02 21:49:00 +00:00
"""
2016-02-23 05:21:49 +00:00
Support for showing the time in a different time zone.
2015-10-02 21:49:00 +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.worldclock/
2015-10-02 21:49:00 +00:00
"""
import logging
import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "Worldclock Sensor"
2016-02-05 12:08:17 +00:00
ICON = 'mdi:clock'
2016-04-16 07:55:35 +00:00
TIME_STR_FORMAT = "%H:%M"
2015-10-02 21:49:00 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 15:46:34 +00:00
"""Setup the Worldclock sensor."""
2015-10-02 21:49:00 +00:00
try:
time_zone = dt_util.get_time_zone(config.get('time_zone'))
except AttributeError:
_LOGGER.error("time_zone in platform configuration is missing.")
return False
if time_zone is None:
_LOGGER.error("Timezone '%s' is not valid.", config.get('time_zone'))
return False
add_devices([WorldClockSensor(
time_zone,
config.get('name', DEFAULT_NAME)
)])
class WorldClockSensor(Entity):
"""Representation of a Worldclock sensor."""
2015-10-02 21:49:00 +00:00
def __init__(self, time_zone, name):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2015-10-02 21:49:00 +00:00
self._name = name
self._time_zone = time_zone
self._state = None
self.update()
@property
def name(self):
2016-03-08 15:46:34 +00:00
"""Return the name of the device."""
2015-10-02 21:49:00 +00:00
return self._name
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the device."""
2015-10-02 21:49:00 +00:00
return self._state
2016-02-05 12:08:17 +00:00
@property
def icon(self):
2016-02-23 05:21:49 +00:00
"""Icon to use in the frontend, if any."""
2016-02-05 12:08:17 +00:00
return ICON
2015-10-02 21:49:00 +00:00
def update(self):
2016-03-08 15:46:34 +00:00
"""Get the time and updates the states."""
2016-04-16 07:55:35 +00:00
self._state = dt_util.now(time_zone=self._time_zone).strftime(
TIME_STR_FORMAT)