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
|
|
|
"""
|
2016-10-30 14:23:47 +00:00
|
|
|
import asyncio
|
2015-10-02 21:49:00 +00:00
|
|
|
import logging
|
|
|
|
|
2016-08-16 19:43:56 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (CONF_NAME, CONF_TIME_ZONE)
|
2015-10-02 21:49:00 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-08-16 19:43:56 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-10-02 21:49:00 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'Worldclock Sensor'
|
2016-10-25 05:01:38 +00:00
|
|
|
|
2016-02-05 12:08:17 +00:00
|
|
|
ICON = 'mdi:clock'
|
2016-10-25 05:01:38 +00:00
|
|
|
|
|
|
|
TIME_STR_FORMAT = '%H:%M'
|
2015-10-02 21:49:00 +00:00
|
|
|
|
2016-08-16 19:43:56 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_TIME_ZONE): cv.time_zone,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
})
|
|
|
|
|
2015-10-02 21:49:00 +00:00
|
|
|
|
2016-10-30 14:23:47 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|
|
|
"""Set up the World clock sensor."""
|
2016-08-16 19:43:56 +00:00
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
time_zone = dt_util.get_time_zone(config.get(CONF_TIME_ZONE))
|
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
async_add_devices([WorldClockSensor(time_zone, name)], True)
|
2015-10-02 21:49:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WorldClockSensor(Entity):
|
2016-10-25 05:01:38 +00:00
|
|
|
"""Representation of a World clock 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
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2016-10-30 14:23:47 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_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)
|