Send removed attributes and entities to ESPHome as None (#195189)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Balloob Bot
2026-12-01 15:42:24 +13:00
committed by GitHub
co-authored by Paulus Schoutsen Claude Opus 5.5
parent 0c2c53fb33
commit cf05315a71
2 changed files with 39 additions and 2 deletions
@@ -554,7 +554,15 @@ class ESPHomeManager:
new_state = event_data["new_state"]
old_state = event_data["old_state"]
# ESPHome keeps the last value it received, so tell it when the entity
# or the attribute is removed. It treats "None" as an unset value.
if new_state is None:
if old_state is not None and (
not attribute or attribute in old_state.attributes
):
self.cli.send_home_assistant_state(
event_data["entity_id"], attribute, "None"
)
return
# Only communicate changes to the state or attribute tracked, an entity
@@ -569,6 +577,13 @@ class ESPHomeManager:
):
return
if attribute and attribute not in new_state.attributes:
if old_state is not None:
self.cli.send_home_assistant_state(
event_data["entity_id"], attribute, "None"
)
return
self._send_home_assistant_state(event.data["entity_id"], attribute, new_state)
@callback
+24 -2
View File
@@ -1855,10 +1855,32 @@ async def test_state_subscription(
mock_client.send_home_assistant_state.reset_mock()
hass.states.async_set("binary_sensor.test", "on", {})
await hass.async_block_till_done()
assert mock_client.send_home_assistant_state.mock_calls == []
assert mock_client.send_home_assistant_state.mock_calls == [
call("binary_sensor.test", "bool", "None"),
call("binary_sensor.test", "float", "None"),
]
mock_client.send_home_assistant_state.reset_mock()
hass.states.async_set("binary_sensor.test", "off", {})
await hass.async_block_till_done()
assert mock_client.send_home_assistant_state.mock_calls == [
call("binary_sensor.test", None, "off")
]
mock_client.send_home_assistant_state.reset_mock()
hass.states.async_remove("binary_sensor.test")
await hass.async_block_till_done()
assert mock_client.send_home_assistant_state.mock_calls == []
assert mock_client.send_home_assistant_state.mock_calls == [
call("binary_sensor.test", None, "None")
]
mock_client.send_home_assistant_state.reset_mock()
hass.states.async_set("binary_sensor.test", "on", {"bool": True})
await hass.async_block_till_done()
mock_client.send_home_assistant_state.reset_mock()
hass.states.async_remove("binary_sensor.test")
await hass.async_block_till_done()
assert mock_client.send_home_assistant_state.mock_calls == [
call("binary_sensor.test", None, "None"),
call("binary_sensor.test", "bool", "None"),
]
async def test_state_subscription_entity_added_later(