Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions homeassistant/components/template/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,25 @@ def update_brightness(self):
return
try:
brightness = self._level_template.async_render()
if brightness in ("None", ""):
self._brightness = None
return
if 0 <= int(brightness) <= 255:
Comment thread
alistairg marked this conversation as resolved.
self._brightness = int(brightness)
else:
_LOGGER.error(
"Received invalid brightness : %s. Expected: 0-255", brightness
)
self._brightness = None
except TemplateError as ex:
_LOGGER.error(ex)
self._state = None
except ValueError:
_LOGGER.error(
"Template must supply an integer brightness from 0-255, or 'None'",
exc_info=True,
)
self._brightness = None
except TemplateError:
_LOGGER.error("Invalid template", exc_info=True)
self._brightness = None

@callback
def update_state(self):
Expand Down Expand Up @@ -415,7 +424,11 @@ def update_temperature(self):
if self._temperature_template is None:
return
try:
temperature = int(self._temperature_template.async_render())
render = self._temperature_template.async_render()
if render in ("None", ""):
self._temperature = None
return
Comment thread
alistairg marked this conversation as resolved.
temperature = int(render)
Comment thread
alistairg marked this conversation as resolved.
if self.min_mireds <= temperature <= self.max_mireds:
self._temperature = temperature
else:
Expand All @@ -425,6 +438,12 @@ def update_temperature(self):
self.max_mireds,
)
self._temperature = None
except ValueError:
_LOGGER.error(
"Template must supply an integer temperature within the range for this light, or 'None'",
exc_info=True,
)
self._temperature = None
except TemplateError:
_LOGGER.error("Cannot evaluate temperature template", exc_info=True)
self._temperature = None
Expand All @@ -435,10 +454,11 @@ def update_color(self):
if self._color_template is None:
return

self._color = None

try:
render = self._color_template.async_render()
if render in ("None", ""):
self._color = None
return
Comment thread
alistairg marked this conversation as resolved.
h_str, s_str = map(
float, render.replace("(", "").replace(")", "").split(",", 1)
)
Expand All @@ -455,7 +475,10 @@ def update_color(self):
h_str,
s_str,
)
self._color = None
else:
_LOGGER.error("Received invalid hs_color : (%s)", render)
except TemplateError as ex:
_LOGGER.error(ex)
self._color = None
except TemplateError:
_LOGGER.error("Cannot evaluate hs_color template", exc_info=True)
self._color = None
19 changes: 17 additions & 2 deletions tests/components/template/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,13 @@ def test_level_action_no_template(self):

@pytest.mark.parametrize(
"expected_level,template",
[(255, "{{255}}"), (None, "{{256}}"), (None, "{{x - 12}}")],
[
(255, "{{255}}"),
(None, "{{256}}"),
(None, "{{x - 12}}"),
(None, "{{ none }}"),
(None, ""),
],
)
def test_level_template(self, expected_level, template):
"""Test the template for the level."""
Expand Down Expand Up @@ -588,7 +594,14 @@ def test_level_template(self, expected_level, template):

@pytest.mark.parametrize(
"expected_temp,template",
[(500, "{{500}}"), (None, "{{501}}"), (None, "{{x - 12}}")],
[
(500, "{{500}}"),
(None, "{{501}}"),
(None, "{{x - 12}}"),
(None, "None"),
(None, "{{ none }}"),
(None, ""),
],
)
def test_temperature_template(self, expected_temp, template):
"""Test the template for the temperature."""
Expand Down Expand Up @@ -894,6 +907,8 @@ def test_color_action_no_template(self):
(None, "{{(361, 100)}}"),
(None, "{{(360, 101)}}"),
(None, "{{x - 12}}"),
(None, ""),
(None, "{{ none }}"),
],
)
def test_color_template(self, expected_hs, template):
Expand Down