4
4
5
5
from collections .abc import Callable
6
6
from functools import partial
7
+ import logging
7
8
import threading
8
9
from typing import Any
9
10
29
30
ERR_NOT_CONNECTED = 21
30
31
ERR_FETCH_CONFIG_FAILED = 22
31
32
33
+ _LOGGER = logging .getLogger (__name__ )
34
+
32
35
33
36
class HomeAssistantController (BaseController ):
34
37
"""Controller to interact with Home Assistant."""
@@ -45,6 +48,7 @@ def __init__(
45
48
app_id : str = APP_HOMEASSISTANT_LOVELACE ,
46
49
hass_connect_timeout : float = DEFAULT_HASS_CONNECT_TIMEOUT ,
47
50
) -> None :
51
+ _LOGGER .debug ("HomeAssistantController.__init__" )
48
52
super ().__init__ (app_namespace , app_id )
49
53
self .hass_url = hass_url
50
54
self .hass_uuid = hass_uuid
@@ -77,16 +81,19 @@ def hass_connected(self) -> bool:
77
81
def channel_connected (self ) -> None :
78
82
"""Called when a channel has been openend that supports the
79
83
namespace of this controller."""
84
+ _LOGGER .debug ("HomeAssistantController.channel_connected" )
80
85
self .get_status ()
81
86
82
87
def channel_disconnected (self ) -> None :
83
88
"""Called when a channel is disconnected."""
89
+ _LOGGER .debug ("HomeAssistantController.channel_disconnected" )
84
90
self .status = None
85
91
self ._hass_connecting_event .set ()
86
92
87
93
def receive_message (self , _message : CastMessage , data : dict ) -> bool :
88
94
"""Called when a message is received."""
89
95
if data .get ("type" ) == "receiver_status" :
96
+ _LOGGER .debug ("HomeAssistantController.receive_message %s" , data )
90
97
if data ["hassUrl" ] != self .hass_url or data ["hassUUID" ] != self .hass_uuid :
91
98
self .logger .info ("Received status for another instance" )
92
99
self .unregister ()
@@ -96,6 +103,9 @@ def receive_message(self, _message: CastMessage, data: dict) -> bool:
96
103
self .status = data
97
104
98
105
if was_connected or not self .hass_connected :
106
+ _LOGGER .debug (
107
+ "HomeAssistantController.receive_message already connected"
108
+ )
99
109
return True
100
110
101
111
# We just got connected, call the callbacks.
@@ -104,6 +114,7 @@ def receive_message(self, _message: CastMessage, data: dict) -> bool:
104
114
return True
105
115
106
116
if data .get ("type" ) == "receiver_error" :
117
+ _LOGGER .debug ("HomeAssistantController.receive_message %s" , data )
107
118
if data .get ("error_code" ) == ERR_WRONG_INSTANCE :
108
119
self .logger .info ("Received ERR_WRONG_INSTANCE" )
109
120
self .unregister ()
@@ -113,14 +124,19 @@ def receive_message(self, _message: CastMessage, data: dict) -> bool:
113
124
114
125
def _call_on_connect_callbacks (self , msg_sent : bool ) -> None :
115
126
"""Call on connect callbacks."""
127
+ _LOGGER .debug ("HomeAssistantController._call_on_connect_callbacks %s" , msg_sent )
116
128
while self ._on_connect :
117
129
self ._on_connect .pop ()(msg_sent , None )
118
130
119
131
def _connect_hass (self , callback_function : CallbackType ) -> None :
120
132
"""Connect to Home Assistant and call the provided callback."""
133
+ _LOGGER .debug ("HomeAssistantController._connect_hass" )
121
134
self ._on_connect .append (callback_function )
122
135
123
136
if not self ._hass_connecting_event .is_set ():
137
+ _LOGGER .debug (
138
+ "HomeAssistantController._connect_hass _hass_connecting_event not set"
139
+ )
124
140
return
125
141
126
142
self ._hass_connecting_event .clear ()
@@ -135,6 +151,9 @@ def _connect_hass(self, callback_function: CallbackType) -> None:
135
151
}
136
152
)
137
153
except Exception : # pylint: disable=broad-except
154
+ _LOGGER .debug (
155
+ "HomeAssistantController._connect_hass failed to send connect message"
156
+ )
138
157
self ._hass_connecting_event .set ()
139
158
self ._call_on_connect_callbacks (False )
140
159
raise
@@ -154,6 +173,7 @@ def show_demo(self) -> None:
154
173
155
174
def get_status (self , * , callback_function : CallbackType | None = None ) -> None :
156
175
"""Get status of Home Assistant Cast."""
176
+ _LOGGER .debug ("HomeAssistantController.get_status" )
157
177
self ._send_connected_message (
158
178
{
159
179
"type" : "get_status" ,
@@ -171,6 +191,7 @@ def show_lovelace_view(
171
191
callback_function : CallbackType | None = None ,
172
192
) -> None :
173
193
"""Show a Lovelace UI."""
194
+ _LOGGER .debug ("HomeAssistantController.show_lovelace_view" )
174
195
self ._send_connected_message (
175
196
{
176
197
"type" : "show_lovelace_view" ,
@@ -186,10 +207,17 @@ def _send_connected_message(
186
207
self , data : dict [str , Any ], callback_function : CallbackType | None
187
208
) -> None :
188
209
"""Send a message to a connected Home Assistant Cast"""
210
+ _LOGGER .debug ("HomeAssistantController._send_connected_message %s" , data )
189
211
if self .hass_connected :
212
+ _LOGGER .debug (
213
+ "HomeAssistantController._send_connected_message already connected"
214
+ )
190
215
self .send_message_nocheck (data , callback_function = callback_function )
191
216
return
192
217
218
+ _LOGGER .debug (
219
+ "HomeAssistantController._send_connected_message not yet connected"
220
+ )
193
221
self ._connect_hass (
194
222
chain_on_success (
195
223
partial (self .send_message_nocheck , data ), callback_function
0 commit comments