Skip to content

Commit

Permalink
Fix template light returning NULL in color or temperature (home-assis…
Browse files Browse the repository at this point in the history
…tant#33498)

* Support for returning NULL in color or temperature. Fixes home-assistant#33469

* Added further support for ‘None’ returns in level template

* Removed assumption that template render may not be a string

* Streamlined code per cloud pylint

* Updates per code review suggestions

* Added improved error handling and logging for brightness

* Additional exception handling for temperature
  • Loading branch information
alistairg authored Apr 2, 2020
1 parent 8fbdc70 commit 3db9d6a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
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:
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
temperature = int(render)
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
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

0 comments on commit 3db9d6a

Please sign in to comment.