Use constants and update ordering (#3261)
This commit is contained in:
parent
24aa3b3c97
commit
94e3986d54
3 changed files with 30 additions and 37 deletions
|
@ -5,22 +5,22 @@ For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/binary_sensor.template/
|
https://home-assistant.io/components/binary_sensor.template/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
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 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
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (BinarySensorDevice,
|
_LOGGER = logging.getLogger(__name__)
|
||||||
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)
|
|
||||||
from homeassistant.exceptions import TemplateError
|
|
||||||
from homeassistant.helpers.entity import generate_entity_id
|
|
||||||
from homeassistant.helpers import template
|
|
||||||
from homeassistant.helpers.event import track_state_change
|
|
||||||
|
|
||||||
CONF_SENSORS = 'sensors'
|
|
||||||
|
|
||||||
SENSOR_SCHEMA = vol.Schema({
|
SENSOR_SCHEMA = vol.Schema({
|
||||||
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
||||||
|
@ -33,15 +33,12 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_SENSORS): vol.Schema({cv.slug: SENSOR_SCHEMA}),
|
vol.Required(CONF_SENSORS): vol.Schema({cv.slug: SENSOR_SCHEMA}),
|
||||||
})
|
})
|
||||||
|
|
||||||
_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 template binary sensors."""
|
"""Setup template binary sensors."""
|
||||||
sensors = []
|
sensors = []
|
||||||
|
|
||||||
for device, device_config in config[CONF_SENSORS].items():
|
for device, device_config in config[CONF_SENSORS].items():
|
||||||
|
|
||||||
value_template = device_config[CONF_VALUE_TEMPLATE]
|
value_template = device_config[CONF_VALUE_TEMPLATE]
|
||||||
entity_ids = device_config[ATTR_ENTITY_ID]
|
entity_ids = device_config[ATTR_ENTITY_ID]
|
||||||
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
||||||
|
@ -85,8 +82,7 @@ class BinarySensorTemplate(BinarySensorDevice):
|
||||||
"""Called when the target device changes state."""
|
"""Called when the target device changes state."""
|
||||||
self.update_ha_state(True)
|
self.update_ha_state(True)
|
||||||
|
|
||||||
track_state_change(hass, entity_ids,
|
track_state_change(hass, entity_ids, template_bsensor_state_listener)
|
||||||
template_bsensor_state_listener)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -111,8 +107,8 @@ class BinarySensorTemplate(BinarySensorDevice):
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data and update the state."""
|
"""Get the latest data and update the state."""
|
||||||
try:
|
try:
|
||||||
self._state = template.render(self.hass,
|
self._state = template.render(
|
||||||
self._template).lower() == 'true'
|
self.hass, self._template).lower() == 'true'
|
||||||
except TemplateError as ex:
|
except TemplateError as ex:
|
||||||
if ex.args and ex.args[0].startswith(
|
if ex.args and ex.args[0].startswith(
|
||||||
"UndefinedError: 'None' has no attribute"):
|
"UndefinedError: 'None' has no attribute"):
|
||||||
|
|
|
@ -5,20 +5,20 @@ For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/sensor.template/
|
https://home-assistant.io/components/sensor.template/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
|
|
||||||
from homeassistant.components.sensor import ENTITY_ID_FORMAT, PLATFORM_SCHEMA
|
from homeassistant.components.sensor import ENTITY_ID_FORMAT, PLATFORM_SCHEMA
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE,
|
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.exceptions import TemplateError
|
||||||
from homeassistant.helpers.entity import Entity, generate_entity_id
|
|
||||||
from homeassistant.helpers import template
|
from homeassistant.helpers import template
|
||||||
|
from homeassistant.helpers.entity import Entity, generate_entity_id
|
||||||
from homeassistant.helpers.event import track_state_change
|
from homeassistant.helpers.event import track_state_change
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
CONF_SENSORS = 'sensors'
|
|
||||||
|
|
||||||
SENSOR_SCHEMA = vol.Schema({
|
SENSOR_SCHEMA = vol.Schema({
|
||||||
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
||||||
|
@ -80,8 +80,7 @@ class SensorTemplate(Entity):
|
||||||
"""Called when the target device changes state."""
|
"""Called when the target device changes state."""
|
||||||
self.update_ha_state(True)
|
self.update_ha_state(True)
|
||||||
|
|
||||||
track_state_change(hass, entity_ids,
|
track_state_change(hass, entity_ids, template_sensor_state_listener)
|
||||||
template_sensor_state_listener)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
|
|
@ -5,28 +5,27 @@ For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/switch.template/
|
https://home-assistant.io/components/switch.template/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
|
|
||||||
from homeassistant.components.switch import (
|
from homeassistant.components.switch import (
|
||||||
ENTITY_ID_FORMAT, SwitchDevice, PLATFORM_SCHEMA)
|
ENTITY_ID_FORMAT, SwitchDevice, PLATFORM_SCHEMA)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE, STATE_OFF, STATE_ON,
|
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.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 import template
|
||||||
|
from homeassistant.helpers.entity import generate_entity_id
|
||||||
from homeassistant.helpers.event import track_state_change
|
from homeassistant.helpers.event import track_state_change
|
||||||
|
from homeassistant.helpers.script import Script
|
||||||
CONF_SWITCHES = 'switches'
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
ON_ACTION = 'turn_on'
|
|
||||||
OFF_ACTION = 'turn_off'
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
_VALID_STATES = [STATE_ON, STATE_OFF, 'true', 'false']
|
_VALID_STATES = [STATE_ON, STATE_OFF, 'true', 'false']
|
||||||
|
|
||||||
|
ON_ACTION = 'turn_on'
|
||||||
|
OFF_ACTION = 'turn_off'
|
||||||
|
|
||||||
SWITCH_SCHEMA = vol.Schema({
|
SWITCH_SCHEMA = vol.Schema({
|
||||||
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
||||||
vol.Required(ON_ACTION): cv.SCRIPT_SCHEMA,
|
vol.Required(ON_ACTION): cv.SCRIPT_SCHEMA,
|
||||||
|
@ -91,8 +90,7 @@ class SwitchTemplate(SwitchDevice):
|
||||||
"""Called when the target device changes state."""
|
"""Called when the target device changes state."""
|
||||||
self.update_ha_state(True)
|
self.update_ha_state(True)
|
||||||
|
|
||||||
track_state_change(hass, entity_ids,
|
track_state_change(hass, entity_ids, template_switch_state_listener)
|
||||||
template_switch_state_listener)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
|
Loading…
Add table
Reference in a new issue