mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Tidy.
This commit is contained in:
parent
f8072aae68
commit
c615272c06
@ -12,9 +12,13 @@ import logging
|
|||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.core import EVENT_STATE_CHANGED
|
from homeassistant.core import EVENT_STATE_CHANGED
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE, ATTR_UNIT_OF_MEASUREMENT)
|
STATE_UNKNOWN,
|
||||||
|
ATTR_FRIENDLY_NAME,
|
||||||
|
CONF_VALUE_TEMPLATE,
|
||||||
|
ATTR_UNIT_OF_MEASUREMENT)
|
||||||
|
|
||||||
from homeassistant.util import template
|
from homeassistant.util import template
|
||||||
|
from homeassistant.exceptions import TemplateError
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -30,9 +34,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
_LOGGER.error("Missing configuration data for sensor platfoprm")
|
_LOGGER.error("Missing configuration data for sensor platfoprm")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
for device in config[CONF_SENSORS]:
|
for device, device_config in config[CONF_SENSORS].items():
|
||||||
device_config = config[CONF_SENSORS].get(device)
|
if not isinstance(device_config, dict):
|
||||||
if device_config is None:
|
|
||||||
_LOGGER.error("Missing configuration data for sensor %s", device)
|
_LOGGER.error("Missing configuration data for sensor %s", device)
|
||||||
continue
|
continue
|
||||||
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
||||||
@ -45,7 +48,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
sensors.append(
|
sensors.append(
|
||||||
SensorTemplate(
|
SensorTemplate(
|
||||||
hass,
|
hass,
|
||||||
device,
|
|
||||||
friendly_name,
|
friendly_name,
|
||||||
unit_of_measurement,
|
unit_of_measurement,
|
||||||
state_template)
|
state_template)
|
||||||
@ -63,17 +65,15 @@ class SensorTemplate(Entity):
|
|||||||
# pylint: disable=too-many-arguments
|
# pylint: disable=too-many-arguments
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
hass,
|
hass,
|
||||||
entity_name,
|
|
||||||
friendly_name,
|
friendly_name,
|
||||||
unit_of_measurement,
|
unit_of_measurement,
|
||||||
state_template):
|
state_template):
|
||||||
|
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._name = entity_name
|
self._name = friendly_name
|
||||||
self._friendly_name = friendly_name
|
|
||||||
self._unit_of_measurement = unit_of_measurement
|
self._unit_of_measurement = unit_of_measurement
|
||||||
self._template = state_template
|
self._template = state_template
|
||||||
self._state = ''
|
self.update()
|
||||||
|
|
||||||
def _update_callback(_event):
|
def _update_callback(_event):
|
||||||
""" Called when the target device changes state. """
|
""" Called when the target device changes state. """
|
||||||
@ -101,18 +101,8 @@ class SensorTemplate(Entity):
|
|||||||
""" Tells Home Assistant not to poll this entity. """
|
""" Tells Home Assistant not to poll this entity. """
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@property
|
|
||||||
def state_attributes(self):
|
|
||||||
attr = {}
|
|
||||||
|
|
||||||
if self._friendly_name:
|
|
||||||
attr[ATTR_FRIENDLY_NAME] = self._friendly_name
|
|
||||||
|
|
||||||
return attr
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self._state = self._renderer()
|
try:
|
||||||
|
self._state = template.render(self.hass, self._template)
|
||||||
def _renderer(self):
|
except TemplateError:
|
||||||
"""Render sensor value."""
|
self._state = STATE_UNKNOWN
|
||||||
return template.render(self.hass, self._template)
|
|
||||||
|
@ -12,33 +12,28 @@ import homeassistant.core as ha
|
|||||||
import homeassistant.components.sensor as sensor
|
import homeassistant.components.sensor as sensor
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures('betamax_session')
|
|
||||||
class TestSensorYr:
|
class TestSensorYr:
|
||||||
""" Test the Yr sensor. """
|
""" Test the Yr sensor. """
|
||||||
|
|
||||||
def setup_method(self, method):
|
def setup_method(self, method):
|
||||||
self.hass = ha.HomeAssistant()
|
self.hass = ha.HomeAssistant()
|
||||||
self.hass.config.latitude = 32.87336
|
|
||||||
self.hass.config.longitude = 117.22743
|
|
||||||
|
|
||||||
def teardown_method(self, method):
|
def teardown_method(self, method):
|
||||||
""" Stop down stuff we started. """
|
""" Stop down stuff we started. """
|
||||||
self.hass.stop()
|
self.hass.stop()
|
||||||
|
|
||||||
def test_template(self, betamax_session):
|
def test_template(self, betamax_session):
|
||||||
with patch('homeassistant.components.sensor.yr.requests.Session',
|
assert sensor.setup(self.hass, {
|
||||||
return_value=betamax_session):
|
'sensor': {
|
||||||
assert sensor.setup(self.hass, {
|
'platform': 'template',
|
||||||
'sensor': {
|
'sensors': {
|
||||||
'platform': 'template',
|
'test_template_sensor': {
|
||||||
'sensors': {
|
'value_template':
|
||||||
'test_template_sensor': {
|
'{{ states.sensor.test_state.state }}'
|
||||||
'value_template':
|
|
||||||
'{{ states.sensor.test_state.state }}'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
})
|
||||||
|
|
||||||
state = self.hass.states.get('sensor.test_template_sensor')
|
state = self.hass.states.get('sensor.test_template_sensor')
|
||||||
assert state.state == ''
|
assert state.state == ''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user