Skip to content

Commit 943fb99

Browse files
authored
Adjust logic related to entity platform state (home-assistant#147882)
* Adjust logic related to entity platform state * Break up hard to read if-statement * Add and improve tests
1 parent 7447cf3 commit 943fb99

File tree

2 files changed

+251
-24
lines changed

2 files changed

+251
-24
lines changed

homeassistant/helpers/entity.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,19 @@ class StateInfo(TypedDict):
215215
class EntityPlatformState(Enum):
216216
"""The platform state of an entity."""
217217

218-
# Not Added: Not yet added to a platform, polling updates
219-
# are written to the state machine.
218+
# Not Added: Not yet added to a platform, states are not written to the
219+
# state machine.
220220
NOT_ADDED = auto()
221221

222-
# Added: Added to a platform, polling updates
223-
# are written to the state machine.
222+
# Adding: Preparing for adding to a platform, states are not written to the
223+
# state machine.
224+
ADDING = auto()
225+
226+
# Added: Added to a platform, states are written to the state machine.
224227
ADDED = auto()
225228

226-
# Removed: Removed from a platform, polling updates
227-
# are not written to the state machine.
229+
# Removed: Removed from a platform, states are not written to the
230+
# state machine.
228231
REMOVED = auto()
229232

230233

@@ -1122,21 +1125,24 @@ def __async_calculate_state(
11221125
@callback
11231126
def _async_write_ha_state(self) -> None:
11241127
"""Write the state to the state machine."""
1125-
if self._platform_state is EntityPlatformState.REMOVED:
1126-
# Polling returned after the entity has already been removed
1127-
return
1128-
1129-
if (entry := self.registry_entry) and entry.disabled_by:
1130-
if not self._disabled_reported:
1131-
self._disabled_reported = True
1132-
_LOGGER.warning(
1133-
(
1134-
"Entity %s is incorrectly being triggered for updates while it"
1135-
" is disabled. This is a bug in the %s integration"
1136-
),
1137-
self.entity_id,
1138-
self.platform.platform_name,
1139-
)
1128+
# The check for self.platform guards against integrations not using an
1129+
# EntityComponent (which has not been allowed since HA Core 2024.1)
1130+
if not self.platform:
1131+
if self._platform_state is EntityPlatformState.REMOVED:
1132+
# Don't write state if the entity is not added to the platform.
1133+
return
1134+
elif self._platform_state is not EntityPlatformState.ADDED:
1135+
if (entry := self.registry_entry) and entry.disabled_by:
1136+
if not self._disabled_reported:
1137+
self._disabled_reported = True
1138+
_LOGGER.warning(
1139+
(
1140+
"Entity %s is incorrectly being triggered for updates while it"
1141+
" is disabled. This is a bug in the %s integration"
1142+
),
1143+
self.entity_id,
1144+
self.platform.platform_name,
1145+
)
11401146
return
11411147

11421148
state_calculate_start = timer()
@@ -1145,7 +1151,7 @@ def _async_write_ha_state(self) -> None:
11451151
)
11461152
time_now = timer()
11471153

1148-
if entry:
1154+
if entry := self.registry_entry:
11491155
# Make sure capabilities in the entity registry are up to date. Capabilities
11501156
# include capability attributes, device class and supported features
11511157
supported_features = supported_features or 0
@@ -1346,7 +1352,7 @@ def add_to_platform_start(
13461352
self.hass = hass
13471353
self.platform = platform
13481354
self.parallel_updates = parallel_updates
1349-
self._platform_state = EntityPlatformState.ADDED
1355+
self._platform_state = EntityPlatformState.ADDING
13501356

13511357
def _call_on_remove_callbacks(self) -> None:
13521358
"""Call callbacks registered by async_on_remove."""
@@ -1370,6 +1376,7 @@ async def add_to_platform_finish(self) -> None:
13701376
"""Finish adding an entity to a platform."""
13711377
await self.async_internal_added_to_hass()
13721378
await self.async_added_to_hass()
1379+
self._platform_state = EntityPlatformState.ADDED
13731380
self.async_write_ha_state()
13741381

13751382
@final

0 commit comments

Comments
 (0)