@@ -36,27 +36,34 @@ async def async_setup_entry(
3636 """Set up Automower message event entities.
3737
3838 Entities are created dynamically based on messages received from the API,
39- but only for mowers that support message events.
39+ but only for mowers that support message events after the WebSocket connection
40+ is ready.
4041 """
4142 coordinator = config_entry .runtime_data
4243 entity_registry = er .async_get (hass )
4344
44- restored_mowers = {
45+ restored_mowers : set [ str ] = {
4546 entry .unique_id .removesuffix ("_message" )
4647 for entry in er .async_entries_for_config_entry (
4748 entity_registry , config_entry .entry_id
4849 )
4950 if entry .domain == EVENT_DOMAIN
5051 }
5152
52- async_add_entities (
53- AutomowerMessageEventEntity (mower_id , coordinator )
54- for mower_id in restored_mowers
55- if mower_id in coordinator .data
56- )
53+ @callback
54+ def _on_ws_ready () -> None :
55+ async_add_entities (
56+ AutomowerMessageEventEntity (mower_id , coordinator , websocket_alive = True )
57+ for mower_id in restored_mowers
58+ if mower_id in coordinator .data
59+ )
60+ coordinator .api .unregister_ws_ready_callback (_on_ws_ready )
61+
62+ coordinator .api .register_ws_ready_callback (_on_ws_ready )
5763
5864 @callback
5965 def _handle_message (msg : SingleMessageData ) -> None :
66+ """Add entity dynamically if a new mower sends messages."""
6067 if msg .id in restored_mowers :
6168 return
6269
@@ -78,11 +85,17 @@ def __init__(
7885 self ,
7986 mower_id : str ,
8087 coordinator : AutomowerDataUpdateCoordinator ,
88+ * ,
89+ websocket_alive : bool | None = None ,
8190 ) -> None :
8291 """Initialize Automower message event entity."""
8392 super ().__init__ (mower_id , coordinator )
8493 self ._attr_unique_id = f"{ mower_id } _message"
85- self .websocket_alive : bool = coordinator .websocket_alive
94+ self .websocket_alive : bool = (
95+ websocket_alive
96+ if websocket_alive is not None
97+ else coordinator .websocket_alive
98+ )
8699
87100 @property
88101 def available (self ) -> bool :
0 commit comments