22
33from __future__ import annotations
44
5- from typing import Any
5+ import logging
66
77from homeassistant .config_entries import ConfigEntry
8- from homeassistant .const import ATTR_ICON , CONF_NAME , CONF_UNIQUE_ID , STATE_UNAVAILABLE
8+ from homeassistant .const import (
9+ ATTR_ICON ,
10+ CONF_NAME ,
11+ CONF_UNIQUE_ID ,
12+ STATE_UNAVAILABLE ,
13+ STATE_UNKNOWN ,
14+ )
915from homeassistant .core import State , callback
1016from homeassistant .helpers .dispatcher import async_dispatcher_connect
1117from homeassistant .helpers .restore_state import RestoreEntity
1824 ATTR_SENSOR_ICON ,
1925 ATTR_SENSOR_STATE ,
2026 ATTR_SENSOR_STATE_CLASS ,
27+ ATTR_SENSOR_TYPE ,
28+ DATA_PENDING_UPDATES ,
29+ DOMAIN ,
2130 SIGNAL_SENSOR_UPDATE ,
2231)
2332from .helpers import device_info
2433
34+ _LOGGER = logging .getLogger (__name__ )
35+
2536
2637class MobileAppEntity (RestoreEntity ):
2738 """Representation of a mobile app entity."""
@@ -56,11 +67,14 @@ async def async_added_to_hass(self) -> None:
5667 self .async_on_remove (
5768 async_dispatcher_connect (
5869 self .hass ,
59- f"{ SIGNAL_SENSOR_UPDATE } -{ self ._attr_unique_id } " ,
70+ f"{ SIGNAL_SENSOR_UPDATE } -{ self ._config [ ATTR_SENSOR_TYPE ] } - { self . _attr_unique_id } " ,
6071 self ._handle_update ,
6172 )
6273 )
6374
75+ # Apply any pending updates
76+ self ._handle_update ()
77+
6478 if (state := await self .async_get_last_state ()) is None :
6579 return
6680
@@ -69,22 +83,38 @@ async def async_added_to_hass(self) -> None:
6983 async def async_restore_last_state (self , last_state : State ) -> None :
7084 """Restore previous state."""
7185 config = self ._config
72- config [ATTR_SENSOR_STATE ] = last_state .state
73- config [ATTR_SENSOR_ATTRIBUTES ] = {
74- ** last_state .attributes ,
75- ** self ._config [ATTR_SENSOR_ATTRIBUTES ],
76- }
77- if ATTR_ICON in last_state .attributes :
78- config [ATTR_SENSOR_ICON ] = last_state .attributes [ATTR_ICON ]
86+
87+ # Only restore state if we don't have one already, since it can be set by a pending update
88+ if config [ATTR_SENSOR_STATE ] in (None , STATE_UNKNOWN ):
89+ config [ATTR_SENSOR_STATE ] = last_state .state
90+ config [ATTR_SENSOR_ATTRIBUTES ] = {
91+ ** last_state .attributes ,
92+ ** self ._config [ATTR_SENSOR_ATTRIBUTES ],
93+ }
94+ if ATTR_ICON in last_state .attributes :
95+ config [ATTR_SENSOR_ICON ] = last_state .attributes [ATTR_ICON ]
7996
8097 @property
8198 def device_info (self ):
8299 """Return device registry information for this entity."""
83100 return device_info (self ._registration )
84101
85102 @callback
86- def _handle_update (self , data : dict [ str , Any ] ) -> None :
103+ def _handle_update (self ) -> None :
87104 """Handle async event updates."""
88- self ._config . update ( data )
105+ self ._apply_pending_update ( )
89106 self ._async_update_attr_from_config ()
90107 self .async_write_ha_state ()
108+
109+ def _apply_pending_update (self ) -> None :
110+ """Restore any pending update for this entity."""
111+ entity_type = self ._config [ATTR_SENSOR_TYPE ]
112+ pending_updates = self .hass .data [DOMAIN ][DATA_PENDING_UPDATES ][entity_type ]
113+ if update := pending_updates .pop (self ._attr_unique_id , None ):
114+ _LOGGER .debug (
115+ "Applying pending update for %s: %s" ,
116+ self ._attr_unique_id ,
117+ update ,
118+ )
119+ # Apply the pending update
120+ self ._config .update (update )
0 commit comments