88
99import voluptuous as vol
1010
11- from homeassistant .components .sensor import SensorEntity
11+ from homeassistant .components .sensor import RestoreSensor
1212from homeassistant .core import HomeAssistant , callback
1313from homeassistant .helpers import entity_platform
1414import homeassistant .helpers .config_validation as cv
@@ -46,7 +46,7 @@ async def async_setup_entry(
4646 async_add_entities (sensors )
4747
4848
49- class ViewAssistSensor (SensorEntity ):
49+ class ViewAssistSensor (RestoreSensor ):
5050 """Representation of a View Assist Sensor."""
5151
5252 _attr_should_poll = False
@@ -72,6 +72,83 @@ def __init__(
7272 async def async_added_to_hass (self ) -> None :
7373 """Run when entity is about to be added to hass."""
7474
75+ # Restore previous sensor data if available
76+ last_sensor_data = await self .async_get_last_sensor_data ()
77+
78+ if last_sensor_data :
79+ # Get the last state to access attributes
80+ last_state = await self .async_get_last_state ()
81+
82+ if last_state and last_state .attributes :
83+ # FIRST: Restore status_icons and menu_items for MenuManager
84+ # These contain runtime additions from add_status_item service
85+ # Store them in extra_data so MenuManager can access them during async_setup
86+ restored_status_icons = last_state .attributes .get ("status_icons" )
87+ restored_menu_items = last_state .attributes .get ("menu_items" )
88+
89+ if restored_status_icons is not None :
90+ self .config .runtime_data .extra_data ["restored_status_icons" ] = restored_status_icons
91+ _LOGGER .debug (
92+ "Saved %d restored status icons for MenuManager: %s" ,
93+ len (restored_status_icons ),
94+ self .entity_id
95+ )
96+
97+ if restored_menu_items is not None :
98+ self .config .runtime_data .extra_data ["restored_menu_items" ] = restored_menu_items
99+ _LOGGER .debug (
100+ "Saved %d restored menu items for MenuManager: %s" ,
101+ len (restored_menu_items ),
102+ self .entity_id
103+ )
104+
105+ # Restore extra_data attributes
106+ # extra_data is used to store dynamic attributes set via view_assist.set_state
107+ restored_extra_data = {}
108+
109+ # Define attributes that are system-managed and should NOT be restored
110+ # These are rebuilt fresh on startup by their respective managers
111+ system_managed_attrs = {
112+ # Core entity properties (from config/runtime_data)
113+ "name" , "type" , "mic_device" , "mic_device_id" , "mute_switch" ,
114+ "display_device" , "intent_device" , "orientation_sensor" ,
115+ "mediaplayer_device" , "musicplayer_device" , "voice_device_id" ,
116+
117+ # Managed by MenuManager (now restored via extra_data above)
118+ "status_icons" , "menu_items" , "menu_active" ,
119+
120+ # Managed by TimerManager (has its own storage)
121+ "timers" ,
122+
123+ # From configuration/runtime_data
124+ "status_icons_size" , "menu_config" , "font_style" ,
125+ "use_24_hour_time" , "background" , "mode" , "view_timeout" ,
126+ "weather_entity" , "screen_mode" , "do_not_disturb" ,
127+ "use_announce" ,
128+
129+ # Generated/ephemeral
130+ "last_updated" , "active_overrides" ,
131+
132+ # Standard entity attributes
133+ "friendly_name" , "icon" , "device_class" ,
134+ "unit_of_measurement" , "state_class"
135+ }
136+
137+ # Restore user/automation-set attributes
138+ # These include: alert_data, title, message, image, message_font_size, etc.
139+ for attr_name , attr_value in last_state .attributes .items ():
140+ if attr_name not in system_managed_attrs :
141+ restored_extra_data [attr_name ] = attr_value
142+
143+ # Update extra_data with restored values
144+ if restored_extra_data :
145+ self .config .runtime_data .extra_data .update (restored_extra_data )
146+ _LOGGER .info (
147+ "Restored %d extra attributes for %s: %s" ,
148+ len (restored_extra_data ),
149+ self .entity_id ,
150+ list (restored_extra_data .keys ())
151+ )
75152 # Add internal event listeners
76153 self .async_on_remove (
77154 async_dispatcher_connect (
0 commit comments