def log_events(event: Event) -> None:
print(event)
async def main():
logging.basicConfig(level=logging.DEBUG)
async with ClientSession() as session:
async with HomeAssistantClient("wss://xxx/api/websocket", get_secret("xxx"), session) as client:
await client.subscribe_events(log_events)
await client.start_listening()
DEBUG:hass_client:Connecting to Home Assistant Websocket API on wss://xxx/api/websocket
INFO:hass_client:Connected to Home Assistant xxx (version 2024.6.2)
DEBUG:hass_client:Publishing message:
{'event_type': '*', 'id': 2, 'type': 'subscribe_events'}
### no messages at all ###
### CTRL-C ###
DEBUG:hass_client:Closing client connection
async def main():
async with ClientSession() as session:
socket = await session.ws_connect("wss://xxx/api/websocket", heartbeat=55, max_msg_size=1024 * 1024)
version_msg = await socket.receive_json()
version = version_msg["ha_version"]
print(version)
auth_command = {
"type": "auth",
"access_token": get_secret("xxx"),
}
await socket.send_json(auth_command)
auth_result = await socket.receive_json()
if auth_result["type"] != "auth_ok":
raise AuthenticationError(auth_result.get("message", "Authentication failed"))
await socket.send_json({'id': 1, 'type': 'subscribe_events', 'event_type': '*'})
while True:
message = await socket.receive_json()
if message is None:
break
print(message)