mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 04:07:08 +00:00
Fix string representation of template result wrappers (#42494)
This commit is contained in:
parent
ce51286c53
commit
e21e28e49f
@ -39,7 +39,6 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
from homeassistant.helpers.entity import async_generate_entity_id
|
from homeassistant.helpers.entity import async_generate_entity_id
|
||||||
from homeassistant.helpers.reload import async_setup_reload_service
|
from homeassistant.helpers.reload import async_setup_reload_service
|
||||||
from homeassistant.helpers.script import Script
|
from homeassistant.helpers.script import Script
|
||||||
from homeassistant.helpers.template import ResultWrapper
|
|
||||||
|
|
||||||
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
|
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
|
||||||
from .template_entity import TemplateEntity
|
from .template_entity import TemplateEntity
|
||||||
@ -259,9 +258,6 @@ class CoverTemplate(TemplateEntity, CoverEntity):
|
|||||||
self._position = None
|
self._position = None
|
||||||
return
|
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 _VALID_STATES:
|
||||||
|
@ -34,7 +34,6 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
from homeassistant.helpers.entity import async_generate_entity_id
|
from homeassistant.helpers.entity import async_generate_entity_id
|
||||||
from homeassistant.helpers.reload import async_setup_reload_service
|
from homeassistant.helpers.reload import async_setup_reload_service
|
||||||
from homeassistant.helpers.script import Script
|
from homeassistant.helpers.script import Script
|
||||||
from homeassistant.helpers.template import ResultWrapper
|
|
||||||
|
|
||||||
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
|
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
|
||||||
from .template_entity import TemplateEntity
|
from .template_entity import TemplateEntity
|
||||||
@ -368,9 +367,6 @@ class TemplateFan(TemplateEntity, FanEntity):
|
|||||||
@callback
|
@callback
|
||||||
def _update_speed(self, speed):
|
def _update_speed(self, speed):
|
||||||
# Validate speed
|
# Validate speed
|
||||||
if isinstance(speed, ResultWrapper):
|
|
||||||
speed = speed.render_result
|
|
||||||
else:
|
|
||||||
speed = str(speed)
|
speed = str(speed)
|
||||||
|
|
||||||
if speed in self._speed_list:
|
if speed in self._speed_list:
|
||||||
|
@ -33,7 +33,6 @@ from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
|
|||||||
from homeassistant.helpers.entity import async_generate_entity_id
|
from homeassistant.helpers.entity import async_generate_entity_id
|
||||||
from homeassistant.helpers.reload import async_setup_reload_service
|
from homeassistant.helpers.reload import async_setup_reload_service
|
||||||
from homeassistant.helpers.script import Script
|
from homeassistant.helpers.script import Script
|
||||||
from homeassistant.helpers.template import ResultWrapper
|
|
||||||
|
|
||||||
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
|
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN, PLATFORMS
|
||||||
from .template_entity import TemplateEntity
|
from .template_entity import TemplateEntity
|
||||||
@ -406,7 +405,7 @@ class LightTemplate(TemplateEntity, LightEntity):
|
|||||||
def _update_state(self, result):
|
def _update_state(self, result):
|
||||||
"""Update the state from the template."""
|
"""Update the state from the template."""
|
||||||
|
|
||||||
if isinstance(result, (TemplateError, ResultWrapper)):
|
if isinstance(result, TemplateError):
|
||||||
# This behavior is legacy
|
# This behavior is legacy
|
||||||
self._state = False
|
self._state = False
|
||||||
if not self._availability_template:
|
if not self._availability_template:
|
||||||
|
@ -140,6 +140,16 @@ def gen_result_wrapper(kls):
|
|||||||
super().__init__(*args)
|
super().__init__(*args)
|
||||||
self.render_result = render_result
|
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
|
return Wrapper
|
||||||
|
|
||||||
|
|
||||||
@ -160,6 +170,13 @@ class TupleWrapper(tuple, ResultWrapper):
|
|||||||
"""Initialize a new tuple class."""
|
"""Initialize a new tuple class."""
|
||||||
self.render_result = render_result
|
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] = {
|
RESULT_WRAPPERS: Dict[Type, Type] = {
|
||||||
kls: gen_result_wrapper(kls) for kls in (list, dict, set)
|
kls: gen_result_wrapper(kls) for kls in (list, dict, set)
|
||||||
|
@ -2698,6 +2698,12 @@ async def test_result_wrappers(hass):
|
|||||||
assert result == native
|
assert result == native
|
||||||
assert result.render_result == text
|
assert result.render_result == text
|
||||||
schema(result) # should not raise
|
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):
|
async def test_parse_result(hass):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user