From dab5a78f8863f99ce8ae3affa4bc71ed19c453e1 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Tue, 16 Aug 2016 21:43:56 +0200 Subject: [PATCH] Use voluptuous for time/date sensors (#2799) * Use voluptuous for time/date sensors * Extend platform * Remove additional checks --- homeassistant/components/sensor/time_date.py | 28 +++++++++++------- homeassistant/components/sensor/worldclock.py | 29 ++++++++++--------- homeassistant/const.py | 3 +- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/sensor/time_date.py b/homeassistant/components/sensor/time_date.py index 48760a4463e..ea1b0c1e742 100644 --- a/homeassistant/components/sensor/time_date.py +++ b/homeassistant/components/sensor/time_date.py @@ -5,12 +5,18 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/sensor.time_date/ """ import logging - 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 from homeassistant.helpers.entity import Entity +import homeassistant.helpers.config_validation as cv + +TIME_STR_FORMAT = "%H:%M" -_LOGGER = logging.getLogger(__name__) OPTION_TYPES = { 'time': 'Time', 'date': 'Date', @@ -20,7 +26,12 @@ OPTION_TYPES = { '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): @@ -29,14 +40,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None): _LOGGER.error("Timezone is not set in Home Assistant config") return False - dev = [] - for variable in config['display_options']: - if variable not in OPTION_TYPES: - _LOGGER.error('Option type: "%s" does not exist', variable) - else: - dev.append(TimeDateSensor(variable)) + devices = [] + for variable in config[CONF_DISPLAY_OPTIONS]: + devices.append(TimeDateSensor(variable)) - add_devices(dev) + add_devices(devices) # pylint: disable=too-few-public-methods diff --git a/homeassistant/components/sensor/worldclock.py b/homeassistant/components/sensor/worldclock.py index 0cfc4598cd0..4eed783e510 100644 --- a/homeassistant/components/sensor/worldclock.py +++ b/homeassistant/components/sensor/worldclock.py @@ -6,31 +6,32 @@ https://home-assistant.io/components/sensor.worldclock/ """ 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 from homeassistant.helpers.entity import Entity +import homeassistant.helpers.config_validation as cv -_LOGGER = logging.getLogger(__name__) DEFAULT_NAME = "Worldclock Sensor" ICON = 'mdi:clock' 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): """Setup the Worldclock sensor.""" - 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 + name = config.get(CONF_NAME) + time_zone = dt_util.get_time_zone(config.get(CONF_TIME_ZONE)) - 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) - )]) + add_devices([WorldClockSensor(time_zone, name)]) class WorldClockSensor(Entity): diff --git a/homeassistant/const.py b/homeassistant/const.py index 61de02041b2..4197cfbf650 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -25,10 +25,11 @@ CONF_ALIAS = 'alias' CONF_API_KEY = 'api_key' CONF_BEFORE = 'before' CONF_BELOW = 'below' -CONF_CONDITION = 'condition' CONF_CODE = 'code' +CONF_CONDITION = 'condition' CONF_CUSTOMIZE = 'customize' CONF_DISARM_AFTER_TRIGGER = 'disarm_after_trigger' +CONF_DISPLAY_OPTIONS = 'display_options' CONF_ELEVATION = 'elevation' CONF_ENTITY_ID = 'entity_id' CONF_ENTITY_NAMESPACE = 'entity_namespace'