Migrate to async (sensor.time_date) (#4100)

* Migrate to async

* Update acc. #4114
This commit is contained in:
Fabian Affolter 2016-10-30 15:21:23 +01:00 committed by Pascal Vizeli
parent 9f2aae1357
commit 27abac85b6

View File

@ -4,20 +4,21 @@ Support for showing the date and the time.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.time_date/ https://home-assistant.io/components/sensor.time_date/
""" """
import logging
from datetime import timedelta from datetime import timedelta
import asyncio
import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_DISPLAY_OPTIONS from homeassistant.const import CONF_DISPLAY_OPTIONS
import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
TIME_STR_FORMAT = "%H:%M" TIME_STR_FORMAT = '%H:%M'
OPTION_TYPES = { OPTION_TYPES = {
'time': 'Time', 'time': 'Time',
@ -34,17 +35,19 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
}) })
def setup_platform(hass, config, add_devices, discovery_info=None): @asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Setup the Time and Date sensor.""" """Setup the Time and Date sensor."""
if hass.config.time_zone is None: if hass.config.time_zone is None:
_LOGGER.error("Timezone is not set in Home Assistant config") _LOGGER.error("Timezone is not set in Home Assistant configuration")
return False return False
devices = [] devices = []
for variable in config[CONF_DISPLAY_OPTIONS]: for variable in config[CONF_DISPLAY_OPTIONS]:
devices.append(TimeDateSensor(variable)) devices.append(TimeDateSensor(variable))
add_devices(devices) hass.loop.create_task(async_add_devices(devices, True))
return True
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
@ -56,7 +59,6 @@ class TimeDateSensor(Entity):
self._name = OPTION_TYPES[option_type] self._name = OPTION_TYPES[option_type]
self.type = option_type self.type = option_type
self._state = None self._state = None
self.update()
@property @property
def name(self): def name(self):
@ -71,14 +73,15 @@ class TimeDateSensor(Entity):
@property @property
def icon(self): def icon(self):
"""Icon to use in the frontend, if any.""" """Icon to use in the frontend, if any."""
if "date" in self.type and "time" in self.type: if 'date' in self.type and 'time' in self.type:
return "mdi:calendar-clock" return 'mdi:calendar-clock'
elif "date" in self.type: elif 'date' in self.type:
return "mdi:calendar" return 'mdi:calendar'
else: else:
return "mdi:clock" return 'mdi:clock'
def update(self): @asyncio.coroutine
def async_update(self):
"""Get the latest data and updates the states.""" """Get the latest data and updates the states."""
time_date = dt_util.utcnow() time_date = dt_util.utcnow()
time = dt_util.as_local(time_date).strftime(TIME_STR_FORMAT) time = dt_util.as_local(time_date).strftime(TIME_STR_FORMAT)
@ -87,10 +90,9 @@ class TimeDateSensor(Entity):
# Calculate Swatch Internet Time. # Calculate Swatch Internet Time.
time_bmt = time_date + timedelta(hours=1) time_bmt = time_date + timedelta(hours=1)
delta = timedelta(hours=time_bmt.hour, delta = timedelta(
minutes=time_bmt.minute, hours=time_bmt.hour, minutes=time_bmt.minute,
seconds=time_bmt.second, seconds=time_bmt.second, microseconds=time_bmt.microsecond)
microseconds=time_bmt.microsecond)
beat = int((delta.seconds + delta.microseconds / 1000000.0) / 86.4) beat = int((delta.seconds + delta.microseconds / 1000000.0) / 86.4)
if self.type == 'time': if self.type == 'time':
@ -98,9 +100,9 @@ class TimeDateSensor(Entity):
elif self.type == 'date': elif self.type == 'date':
self._state = date self._state = date
elif self.type == 'date_time': elif self.type == 'date_time':
self._state = date + ', ' + time self._state = '{}, {}'.format(date, time)
elif self.type == 'time_date': elif self.type == 'time_date':
self._state = time + ', ' + date self._state = '{}, {}'.format(time, date)
elif self.type == 'time_utc': elif self.type == 'time_utc':
self._state = time_utc self._state = time_utc
elif self.type == 'beat': elif self.type == 'beat':