Skip to content

Commit

Permalink
Use constants and update ordering (#3261)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabaff authored Sep 8, 2016
1 parent 24aa3b3 commit 94e3986
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
30 changes: 13 additions & 17 deletions homeassistant/components/binary_sensor/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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"):
Expand Down
11 changes: 5 additions & 6 deletions homeassistant/components/sensor/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
18 changes: 8 additions & 10 deletions homeassistant/components/switch/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 94e3986

Please sign in to comment.