From cf05315a711b19ced9482e133cdd71fb7c5d5588 Mon Sep 17 00:00:00 2001 From: Balloob Bot Date: Mon, 30 Nov 2026 21:42:24 -0500 Subject: [PATCH] Send removed attributes and entities to ESPHome as None (#195189) Co-authored-by: Paulus Schoutsen Co-authored-by: Claude Opus 5.5 --- homeassistant/components/esphome/manager.py | 15 ++++++++++++ tests/components/esphome/test_manager.py | 26 +++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/esphome/manager.py b/homeassistant/components/esphome/manager.py index a1fba21a5285..fd3078271933 100644 --- a/homeassistant/components/esphome/manager.py +++ b/homeassistant/components/esphome/manager.py @@ -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 diff --git a/tests/components/esphome/test_manager.py b/tests/components/esphome/test_manager.py index 27f8891d813a..2149650c09e5 100644 --- a/tests/components/esphome/test_manager.py +++ b/tests/components/esphome/test_manager.py @@ -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(