Use voluptuous for time/date sensors (#2799)
* Use voluptuous for time/date sensors * Extend platform * Remove additional checks
This commit is contained in:
parent
1c140de0dc
commit
dab5a78f88
3 changed files with 35 additions and 25 deletions
|
@ -5,12 +5,18 @@ 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
|
import logging
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
|
from homeassistant.const import CONF_DISPLAY_OPTIONS
|
||||||
import homeassistant.util.dt as dt_util
|
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
|
||||||
|
|
||||||
|
TIME_STR_FORMAT = "%H:%M"
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
OPTION_TYPES = {
|
OPTION_TYPES = {
|
||||||
'time': 'Time',
|
'time': 'Time',
|
||||||
'date': 'Date',
|
'date': 'Date',
|
||||||
|
@ -20,7 +26,12 @@ OPTION_TYPES = {
|
||||||
'time_utc': 'Time (UTC)',
|
'time_utc': 'Time (UTC)',
|
||||||
}
|
}
|
||||||
|
|
||||||
TIME_STR_FORMAT = "%H:%M"
|
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__)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
|
@ -29,14 +40,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
_LOGGER.error("Timezone is not set in Home Assistant config")
|
_LOGGER.error("Timezone is not set in Home Assistant config")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
dev = []
|
devices = []
|
||||||
for variable in config['display_options']:
|
for variable in config[CONF_DISPLAY_OPTIONS]:
|
||||||
if variable not in OPTION_TYPES:
|
devices.append(TimeDateSensor(variable))
|
||||||
_LOGGER.error('Option type: "%s" does not exist', variable)
|
|
||||||
else:
|
|
||||||
dev.append(TimeDateSensor(variable))
|
|
||||||
|
|
||||||
add_devices(dev)
|
add_devices(devices)
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
|
|
|
@ -6,31 +6,32 @@ https://home-assistant.io/components/sensor.worldclock/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
|
from homeassistant.const import (CONF_NAME, CONF_TIME_ZONE)
|
||||||
import homeassistant.util.dt as dt_util
|
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
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
DEFAULT_NAME = "Worldclock Sensor"
|
DEFAULT_NAME = "Worldclock Sensor"
|
||||||
ICON = 'mdi:clock'
|
ICON = 'mdi:clock'
|
||||||
TIME_STR_FORMAT = "%H:%M"
|
TIME_STR_FORMAT = "%H:%M"
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Required(CONF_TIME_ZONE): cv.time_zone,
|
||||||
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
|
})
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the Worldclock sensor."""
|
"""Setup the Worldclock sensor."""
|
||||||
try:
|
name = config.get(CONF_NAME)
|
||||||
time_zone = dt_util.get_time_zone(config.get('time_zone'))
|
time_zone = dt_util.get_time_zone(config.get(CONF_TIME_ZONE))
|
||||||
except AttributeError:
|
|
||||||
_LOGGER.error("time_zone in platform configuration is missing.")
|
|
||||||
return False
|
|
||||||
|
|
||||||
if time_zone is None:
|
add_devices([WorldClockSensor(time_zone, name)])
|
||||||
_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):
|
class WorldClockSensor(Entity):
|
||||||
|
|
|
@ -25,10 +25,11 @@ CONF_ALIAS = 'alias'
|
||||||
CONF_API_KEY = 'api_key'
|
CONF_API_KEY = 'api_key'
|
||||||
CONF_BEFORE = 'before'
|
CONF_BEFORE = 'before'
|
||||||
CONF_BELOW = 'below'
|
CONF_BELOW = 'below'
|
||||||
CONF_CONDITION = 'condition'
|
|
||||||
CONF_CODE = 'code'
|
CONF_CODE = 'code'
|
||||||
|
CONF_CONDITION = 'condition'
|
||||||
CONF_CUSTOMIZE = 'customize'
|
CONF_CUSTOMIZE = 'customize'
|
||||||
CONF_DISARM_AFTER_TRIGGER = 'disarm_after_trigger'
|
CONF_DISARM_AFTER_TRIGGER = 'disarm_after_trigger'
|
||||||
|
CONF_DISPLAY_OPTIONS = 'display_options'
|
||||||
CONF_ELEVATION = 'elevation'
|
CONF_ELEVATION = 'elevation'
|
||||||
CONF_ENTITY_ID = 'entity_id'
|
CONF_ENTITY_ID = 'entity_id'
|
||||||
CONF_ENTITY_NAMESPACE = 'entity_namespace'
|
CONF_ENTITY_NAMESPACE = 'entity_namespace'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue