44
55from collections .abc import Callable
66from functools import partial
7+ import logging
78import threading
89from typing import Any
910
2930ERR_NOT_CONNECTED = 21
3031ERR_FETCH_CONFIG_FAILED = 22
3132
33+ _LOGGER = logging .getLogger (__name__ )
34+
3235
3336class HomeAssistantController (BaseController ):
3437 """Controller to interact with Home Assistant."""
@@ -45,6 +48,7 @@ def __init__(
4548 app_id : str = APP_HOMEASSISTANT_LOVELACE ,
4649 hass_connect_timeout : float = DEFAULT_HASS_CONNECT_TIMEOUT ,
4750 ) -> None :
51+ _LOGGER .debug ("HomeAssistantController.__init__" )
4852 super ().__init__ (app_namespace , app_id )
4953 self .hass_url = hass_url
5054 self .hass_uuid = hass_uuid
@@ -77,16 +81,19 @@ def hass_connected(self) -> bool:
7781 def channel_connected (self ) -> None :
7882 """Called when a channel has been openend that supports the
7983 namespace of this controller."""
84+ _LOGGER .debug ("HomeAssistantController.channel_connected" )
8085 self .get_status ()
8186
8287 def channel_disconnected (self ) -> None :
8388 """Called when a channel is disconnected."""
89+ _LOGGER .debug ("HomeAssistantController.channel_disconnected" )
8490 self .status = None
8591 self ._hass_connecting_event .set ()
8692
8793 def receive_message (self , _message : CastMessage , data : dict ) -> bool :
8894 """Called when a message is received."""
8995 if data .get ("type" ) == "receiver_status" :
96+ _LOGGER .debug ("HomeAssistantController.receive_message %s" , data )
9097 if data ["hassUrl" ] != self .hass_url or data ["hassUUID" ] != self .hass_uuid :
9198 self .logger .info ("Received status for another instance" )
9299 self .unregister ()
@@ -96,6 +103,9 @@ def receive_message(self, _message: CastMessage, data: dict) -> bool:
96103 self .status = data
97104
98105 if was_connected or not self .hass_connected :
106+ _LOGGER .debug (
107+ "HomeAssistantController.receive_message already connected"
108+ )
99109 return True
100110
101111 # We just got connected, call the callbacks.
@@ -104,6 +114,7 @@ def receive_message(self, _message: CastMessage, data: dict) -> bool:
104114 return True
105115
106116 if data .get ("type" ) == "receiver_error" :
117+ _LOGGER .debug ("HomeAssistantController.receive_message %s" , data )
107118 if data .get ("error_code" ) == ERR_WRONG_INSTANCE :
108119 self .logger .info ("Received ERR_WRONG_INSTANCE" )
109120 self .unregister ()
@@ -113,14 +124,19 @@ def receive_message(self, _message: CastMessage, data: dict) -> bool:
113124
114125 def _call_on_connect_callbacks (self , msg_sent : bool ) -> None :
115126 """Call on connect callbacks."""
127+ _LOGGER .debug ("HomeAssistantController._call_on_connect_callbacks %s" , msg_sent )
116128 while self ._on_connect :
117129 self ._on_connect .pop ()(msg_sent , None )
118130
119131 def _connect_hass (self , callback_function : CallbackType ) -> None :
120132 """Connect to Home Assistant and call the provided callback."""
133+ _LOGGER .debug ("HomeAssistantController._connect_hass" )
121134 self ._on_connect .append (callback_function )
122135
123136 if not self ._hass_connecting_event .is_set ():
137+ _LOGGER .debug (
138+ "HomeAssistantController._connect_hass _hass_connecting_event not set"
139+ )
124140 return
125141
126142 self ._hass_connecting_event .clear ()
@@ -135,6 +151,9 @@ def _connect_hass(self, callback_function: CallbackType) -> None:
135151 }
136152 )
137153 except Exception : # pylint: disable=broad-except
154+ _LOGGER .debug (
155+ "HomeAssistantController._connect_hass failed to send connect message"
156+ )
138157 self ._hass_connecting_event .set ()
139158 self ._call_on_connect_callbacks (False )
140159 raise
@@ -154,6 +173,7 @@ def show_demo(self) -> None:
154173
155174 def get_status (self , * , callback_function : CallbackType | None = None ) -> None :
156175 """Get status of Home Assistant Cast."""
176+ _LOGGER .debug ("HomeAssistantController.get_status" )
157177 self ._send_connected_message (
158178 {
159179 "type" : "get_status" ,
@@ -171,6 +191,7 @@ def show_lovelace_view(
171191 callback_function : CallbackType | None = None ,
172192 ) -> None :
173193 """Show a Lovelace UI."""
194+ _LOGGER .debug ("HomeAssistantController.show_lovelace_view" )
174195 self ._send_connected_message (
175196 {
176197 "type" : "show_lovelace_view" ,
@@ -186,10 +207,17 @@ def _send_connected_message(
186207 self , data : dict [str , Any ], callback_function : CallbackType | None
187208 ) -> None :
188209 """Send a message to a connected Home Assistant Cast"""
210+ _LOGGER .debug ("HomeAssistantController._send_connected_message %s" , data )
189211 if self .hass_connected :
212+ _LOGGER .debug (
213+ "HomeAssistantController._send_connected_message already connected"
214+ )
190215 self .send_message_nocheck (data , callback_function = callback_function )
191216 return
192217
218+ _LOGGER .debug (
219+ "HomeAssistantController._send_connected_message not yet connected"
220+ )
193221 self ._connect_hass (
194222 chain_on_success (
195223 partial (self .send_message_nocheck , data ), callback_function
0 commit comments