From e21e28e49fabbbb78e0fc7b868f0b0d0709c9373 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 28 Oct 2020 09:11:08 +0100 Subject: [PATCH] Fix string representation of template result wrappers (#42494) --- homeassistant/components/template/cover.py | 6 +----- homeassistant/components/template/fan.py | 6 +----- homeassistant/components/template/light.py | 3 +-- homeassistant/helpers/template.py | 17 +++++++++++++++++ tests/helpers/test_template.py | 6 ++++++ 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/template/cover.py b/homeassistant/components/template/cover.py index cc91bdabd7d..93ffd2fd988 100644 --- a/homeassistant/components/template/cover.py +++ b/homeassistant/components/template/cover.py @@ -39,7 +39,6 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.script import Script -from homeassistant.helpers.template import ResultWrapper from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .template_entity import TemplateEntity @@ -259,10 +258,7 @@ class CoverTemplate(TemplateEntity, CoverEntity): self._position = None return - if isinstance(result, ResultWrapper): - state = result.render_result.lower() - else: - state = str(result).lower() + state = str(result).lower() if state in _VALID_STATES: if state in ("true", STATE_OPEN): diff --git a/homeassistant/components/template/fan.py b/homeassistant/components/template/fan.py index e5b3569e14b..fa67f60dac0 100644 --- a/homeassistant/components/template/fan.py +++ b/homeassistant/components/template/fan.py @@ -34,7 +34,6 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.script import Script -from homeassistant.helpers.template import ResultWrapper from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .template_entity import TemplateEntity @@ -368,10 +367,7 @@ class TemplateFan(TemplateEntity, FanEntity): @callback def _update_speed(self, speed): # Validate speed - if isinstance(speed, ResultWrapper): - speed = speed.render_result - else: - speed = str(speed) + speed = str(speed) if speed in self._speed_list: self._speed = speed diff --git a/homeassistant/components/template/light.py b/homeassistant/components/template/light.py index 84939beecad..42493136b48 100644 --- a/homeassistant/components/template/light.py +++ b/homeassistant/components/template/light.py @@ -33,7 +33,6 @@ from homeassistant.helpers.config_validation import PLATFORM_SCHEMA from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.script import Script -from homeassistant.helpers.template import ResultWrapper from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS from .template_entity import TemplateEntity @@ -406,7 +405,7 @@ class LightTemplate(TemplateEntity, LightEntity): def _update_state(self, result): """Update the state from the template.""" - if isinstance(result, (TemplateError, ResultWrapper)): + if isinstance(result, TemplateError): # This behavior is legacy self._state = False if not self._availability_template: diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 0614b3c3b4f..d6c89f28e6e 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -140,6 +140,16 @@ def gen_result_wrapper(kls): super().__init__(*args) self.render_result = render_result + def __str__(self) -> str: + if self.render_result is None: + # Can't get set repr to work + if kls is set: + return str(set(self)) + + return kls.__str__(self) + + return self.render_result + return Wrapper @@ -160,6 +170,13 @@ class TupleWrapper(tuple, ResultWrapper): """Initialize a new tuple class.""" self.render_result = render_result + def __str__(self) -> str: + """Return string representation.""" + if self.render_result is None: + return super().__str__() + + return self.render_result + RESULT_WRAPPERS: Dict[Type, Type] = { kls: gen_result_wrapper(kls) for kls in (list, dict, set) diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 9ac546c6a75..e3eafda52f1 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -2698,6 +2698,12 @@ async def test_result_wrappers(hass): assert result == native assert result.render_result == text schema(result) # should not raise + # Result with render text stringifies to original text + assert str(result) == text + # Result without render text stringifies same as original type + assert str(template.RESULT_WRAPPERS[orig_type](native)) == str( + orig_type(native) + ) async def test_parse_result(hass):