From 94e3986d54e45e0c5615076c3e52f673dea6ddf8 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 8 Sep 2016 16:26:54 +0200 Subject: [PATCH] Use constants and update ordering (#3261) --- .../components/binary_sensor/template.py | 30 ++++++++----------- homeassistant/components/sensor/template.py | 11 ++++--- homeassistant/components/switch/template.py | 18 +++++------ 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/binary_sensor/template.py b/homeassistant/components/binary_sensor/template.py index e87594e625cd01..e0b748bbbbe22e 100644 --- a/homeassistant/components/binary_sensor/template.py +++ b/homeassistant/components/binary_sensor/template.py @@ -5,22 +5,22 @@ https://home-assistant.io/components/binary_sensor.template/ """ import logging -import voluptuous as vol -import homeassistant.helpers.config_validation as cv -from homeassistant.components.binary_sensor import (BinarySensorDevice, - ENTITY_ID_FORMAT, - PLATFORM_SCHEMA, - SENSOR_CLASSES_SCHEMA) +import voluptuous as vol -from homeassistant.const import (ATTR_FRIENDLY_NAME, ATTR_ENTITY_ID, MATCH_ALL, - CONF_VALUE_TEMPLATE, CONF_SENSOR_CLASS) +from homeassistant.components.binary_sensor import ( + BinarySensorDevice, ENTITY_ID_FORMAT, PLATFORM_SCHEMA, + SENSOR_CLASSES_SCHEMA) +from homeassistant.const import ( + ATTR_FRIENDLY_NAME, ATTR_ENTITY_ID, MATCH_ALL, CONF_VALUE_TEMPLATE, + CONF_SENSOR_CLASS, CONF_SENSORS) from homeassistant.exceptions import TemplateError -from homeassistant.helpers.entity import generate_entity_id from homeassistant.helpers import template +from homeassistant.helpers.entity import generate_entity_id from homeassistant.helpers.event import track_state_change +import homeassistant.helpers.config_validation as cv -CONF_SENSORS = 'sensors' +_LOGGER = logging.getLogger(__name__) SENSOR_SCHEMA = vol.Schema({ vol.Required(CONF_VALUE_TEMPLATE): cv.template, @@ -33,15 +33,12 @@ vol.Required(CONF_SENSORS): vol.Schema({cv.slug: SENSOR_SCHEMA}), }) -_LOGGER = logging.getLogger(__name__) - def setup_platform(hass, config, add_devices, discovery_info=None): """Setup template binary sensors.""" sensors = [] for device, device_config in config[CONF_SENSORS].items(): - value_template = device_config[CONF_VALUE_TEMPLATE] entity_ids = device_config[ATTR_ENTITY_ID] friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device) @@ -85,8 +82,7 @@ def template_bsensor_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) - track_state_change(hass, entity_ids, - template_bsensor_state_listener) + track_state_change(hass, entity_ids, template_bsensor_state_listener) @property def name(self): @@ -111,8 +107,8 @@ def should_poll(self): def update(self): """Get the latest data and update the state.""" try: - self._state = template.render(self.hass, - self._template).lower() == 'true' + self._state = template.render( + self.hass, self._template).lower() == 'true' except TemplateError as ex: if ex.args and ex.args[0].startswith( "UndefinedError: 'None' has no attribute"): diff --git a/homeassistant/components/sensor/template.py b/homeassistant/components/sensor/template.py index 961b6f39c177f2..743a1909ea5111 100644 --- a/homeassistant/components/sensor/template.py +++ b/homeassistant/components/sensor/template.py @@ -5,20 +5,20 @@ https://home-assistant.io/components/sensor.template/ """ import logging + import voluptuous as vol -import homeassistant.helpers.config_validation as cv from homeassistant.components.sensor import ENTITY_ID_FORMAT, PLATFORM_SCHEMA from homeassistant.const import ( ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE, - ATTR_ENTITY_ID, MATCH_ALL) + ATTR_ENTITY_ID, MATCH_ALL, CONF_SENSORS) from homeassistant.exceptions import TemplateError -from homeassistant.helpers.entity import Entity, generate_entity_id from homeassistant.helpers import template +from homeassistant.helpers.entity import Entity, generate_entity_id from homeassistant.helpers.event import track_state_change +import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) -CONF_SENSORS = 'sensors' SENSOR_SCHEMA = vol.Schema({ vol.Required(CONF_VALUE_TEMPLATE): cv.template, @@ -80,8 +80,7 @@ def template_sensor_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) - track_state_change(hass, entity_ids, - template_sensor_state_listener) + track_state_change(hass, entity_ids, template_sensor_state_listener) @property def name(self): diff --git a/homeassistant/components/switch/template.py b/homeassistant/components/switch/template.py index 6778315843e86b..2b043c110a497d 100644 --- a/homeassistant/components/switch/template.py +++ b/homeassistant/components/switch/template.py @@ -5,28 +5,27 @@ https://home-assistant.io/components/switch.template/ """ import logging + import voluptuous as vol -import homeassistant.helpers.config_validation as cv from homeassistant.components.switch import ( ENTITY_ID_FORMAT, SwitchDevice, PLATFORM_SCHEMA) from homeassistant.const import ( ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE, STATE_OFF, STATE_ON, - ATTR_ENTITY_ID, MATCH_ALL) + ATTR_ENTITY_ID, MATCH_ALL, CONF_SWITCHES) from homeassistant.exceptions import TemplateError -from homeassistant.helpers.entity import generate_entity_id -from homeassistant.helpers.script import Script from homeassistant.helpers import template +from homeassistant.helpers.entity import generate_entity_id from homeassistant.helpers.event import track_state_change +from homeassistant.helpers.script import Script +import homeassistant.helpers.config_validation as cv -CONF_SWITCHES = 'switches' +_LOGGER = logging.getLogger(__name__) +_VALID_STATES = [STATE_ON, STATE_OFF, 'true', 'false'] ON_ACTION = 'turn_on' OFF_ACTION = 'turn_off' -_LOGGER = logging.getLogger(__name__) -_VALID_STATES = [STATE_ON, STATE_OFF, 'true', 'false'] - SWITCH_SCHEMA = vol.Schema({ vol.Required(CONF_VALUE_TEMPLATE): cv.template, vol.Required(ON_ACTION): cv.SCRIPT_SCHEMA, @@ -91,8 +90,7 @@ def template_switch_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) - track_state_change(hass, entity_ids, - template_switch_state_listener) + track_state_change(hass, entity_ids, template_switch_state_listener) @property def name(self):