Skip to content

Commit

Permalink
Fix TCP sensor to correctly use value_template (home-assistant#5211)
Browse files Browse the repository at this point in the history
* Fix TCP sensor to correctly use value_template

* Fix TCP component tests

* Update tcp.py
  • Loading branch information
nikdoof authored and balloob committed Jan 11, 2017
1 parent 3f3a3bc commit 1cf9ae5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
3 changes: 1 addition & 2 deletions homeassistant/components/sensor/tcp.py
Expand Up @@ -16,7 +16,6 @@
CONF_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE)
from homeassistant.exceptions import TemplateError
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.template import Template
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -57,7 +56,7 @@ def __init__(self, hass, config):
value_template = config.get(CONF_VALUE_TEMPLATE)

if value_template is not None:
value_template = Template(value_template, hass)
value_template.hass = hass

self._hass = hass
self._config = {
Expand Down
7 changes: 4 additions & 3 deletions tests/components/sensor/test_tcp.py
Expand Up @@ -9,6 +9,7 @@
from homeassistant.bootstrap import setup_component
from homeassistant.components.sensor import tcp
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.template import Template

TEST_CONFIG = {
'sensor': {
Expand All @@ -19,7 +20,7 @@
tcp.CONF_TIMEOUT: tcp.DEFAULT_TIMEOUT + 1,
tcp.CONF_PAYLOAD: 'test_payload',
tcp.CONF_UNIT_OF_MEASUREMENT: 'test_unit',
tcp.CONF_VALUE_TEMPLATE: 'test_template',
tcp.CONF_VALUE_TEMPLATE: Template('test_template'),
tcp.CONF_VALUE_ON: 'test_on',
tcp.CONF_BUFFER_SIZE: tcp.DEFAULT_BUFFER_SIZE + 1
},
Expand Down Expand Up @@ -252,7 +253,7 @@ def test_update_renders_value_in_template(self, mock_select, mock_socket):
mock_socket = mock_socket().__enter__()
mock_socket.recv.return_value = test_value.encode()
config = copy(TEST_CONFIG['sensor'])
config[tcp.CONF_VALUE_TEMPLATE] = '{{ value }} {{ 1+1 }}'
config[tcp.CONF_VALUE_TEMPLATE] = Template('{{ value }} {{ 1+1 }}')
sensor = tcp.TcpSensor(self.hass, config)
assert sensor._state == '%s 2' % test_value

Expand All @@ -265,6 +266,6 @@ def test_update_returns_if_template_render_fails(
mock_socket = mock_socket().__enter__()
mock_socket.recv.return_value = test_value.encode()
config = copy(TEST_CONFIG['sensor'])
config[tcp.CONF_VALUE_TEMPLATE] = "{{ this won't work"
config[tcp.CONF_VALUE_TEMPLATE] = Template("{{ this won't work")
sensor = tcp.TcpSensor(self.hass, config)
assert sensor.update() is None

0 comments on commit 1cf9ae5

Please sign in to comment.