Fix correct handling in ManualTriggerEntity (#130135)

This commit is contained in:
G Johansson 2024-11-21 20:46:03 +01:00 committed by GitHub
parent 1ab2bbe3b0
commit 797eb606fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 19 deletions

View File

@ -187,13 +187,11 @@ class CommandSensor(ManualTriggerSensorEntity):
SensorDeviceClass.TIMESTAMP,
}:
self._attr_native_value = value
self._process_manual_data(value)
return
if value is not None:
elif value is not None:
self._attr_native_value = async_parse_date_datetime(
value, self.entity_id, self.device_class
)
self._process_manual_data(value)
self.async_write_ha_state()

View File

@ -30,7 +30,7 @@ from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads
from . import config_validation as cv
from .entity import Entity
from .template import render_complex
from .template import TemplateStateFromEntityId, render_complex
from .typing import ConfigType
CONF_AVAILABILITY = "availability"
@ -231,16 +231,14 @@ class ManualTriggerEntity(TriggerBaseEntity):
Ex: self._process_manual_data(payload)
"""
self.async_write_ha_state()
this = None
if state := self.hass.states.get(self.entity_id):
this = state.as_dict()
run_variables: dict[str, Any] = {"value": value}
# Silently try if variable is a json and store result in `value_json` if it is.
with contextlib.suppress(*JSON_DECODE_EXCEPTIONS):
run_variables["value_json"] = json_loads(run_variables["value"])
variables = {"this": this, **(run_variables or {})}
variables = {
"this": TemplateStateFromEntityId(self.hass, self.entity_id),
**(run_variables or {}),
}
self._render_templates(variables)

View File

@ -87,7 +87,7 @@ async def test_setup_platform_yaml(hass: HomeAssistant) -> None:
"payload_off": "0",
"value_template": "{{ value | multiply(0.1) }}",
"icon": (
'{% if this.state=="on" %} mdi:on {% else %} mdi:off {% endif %}'
'{% if this.attributes.icon=="mdi:icon2" %} mdi:icon1 {% else %} mdi:icon2 {% endif %}'
),
}
}
@ -101,7 +101,15 @@ async def test_template(hass: HomeAssistant, load_yaml_integration: None) -> Non
entity_state = hass.states.get("binary_sensor.test")
assert entity_state
assert entity_state.state == STATE_ON
assert entity_state.attributes.get("icon") == "mdi:on"
assert entity_state.attributes.get("icon") == "mdi:icon2"
async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=30))
await hass.async_block_till_done(wait_background_tasks=True)
entity_state = hass.states.get("binary_sensor.test")
assert entity_state
assert entity_state.state == STATE_ON
assert entity_state.attributes.get("icon") == "mdi:icon1"
@pytest.mark.parametrize(

View File

@ -422,13 +422,19 @@ async def test_icon_template(hass: HomeAssistant) -> None:
"command_close": f"echo 0 > {path}",
"command_stop": f"echo 0 > {path}",
"name": "Test",
"icon": "{% if this.state=='open' %} mdi:open {% else %} mdi:closed {% endif %}",
"icon": '{% if this.attributes.icon=="mdi:icon2" %} mdi:icon1 {% else %} mdi:icon2 {% endif %}',
}
}
]
},
)
await hass.async_block_till_done()
await hass.services.async_call(
COVER_DOMAIN,
SERVICE_OPEN_COVER,
{ATTR_ENTITY_ID: "cover.test"},
blocking=True,
)
await hass.services.async_call(
COVER_DOMAIN,
@ -438,7 +444,7 @@ async def test_icon_template(hass: HomeAssistant) -> None:
)
entity_state = hass.states.get("cover.test")
assert entity_state
assert entity_state.attributes.get("icon") == "mdi:closed"
assert entity_state.attributes.get("icon") == "mdi:icon1"
await hass.services.async_call(
COVER_DOMAIN,
@ -448,4 +454,4 @@ async def test_icon_template(hass: HomeAssistant) -> None:
)
entity_state = hass.states.get("cover.test")
assert entity_state
assert entity_state.attributes.get("icon") == "mdi:open"
assert entity_state.attributes.get("icon") == "mdi:icon2"

View File

@ -552,7 +552,7 @@ async def test_templating(hass: HomeAssistant) -> None:
"command_off": f"echo 0 > {path}",
"value_template": '{{ value=="1" }}',
"icon": (
'{% if this.state=="on" %} mdi:on {% else %} mdi:off {% endif %}'
'{% if this.attributes.icon=="mdi:icon2" %} mdi:icon1 {% else %} mdi:icon2 {% endif %}'
),
"name": "Test",
}
@ -564,7 +564,7 @@ async def test_templating(hass: HomeAssistant) -> None:
"command_off": f"echo 0 > {path}",
"value_template": '{{ value=="1" }}',
"icon": (
'{% if states("switch.test2")=="on" %} mdi:on {% else %} mdi:off {% endif %}'
'{% if states("switch.test")=="off" %} mdi:off {% else %} mdi:on {% endif %}'
),
"name": "Test2",
},
@ -595,7 +595,7 @@ async def test_templating(hass: HomeAssistant) -> None:
entity_state = hass.states.get("switch.test")
entity_state2 = hass.states.get("switch.test2")
assert entity_state.state == STATE_ON
assert entity_state.attributes.get("icon") == "mdi:on"
assert entity_state.attributes.get("icon") == "mdi:icon2"
assert entity_state2.state == STATE_ON
assert entity_state2.attributes.get("icon") == "mdi:on"