Websocket Service
AUTHENTICATION_TIMEOUT_SECONDS = 10
module-attribute
Seconds to wait for Home Assistant to answer the auth handshake.
TOTAL_TIMEOUT_SECONDS = 30
module-attribute
Total seconds aiohttp allows for one WebSocket operation (ClientTimeout.total).
CLEANUP_TIMEOUT_SECONDS = 2.0
module-attribute
Seconds to wait for the receive task to finish cancelling during partial cleanup.
WebsocketService
Bases: WebsocketMessageIOMixin, Service
Source code in src/hassette/core/websocket_service.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | |
url: str = self.hassette.ws_url
instance-attribute
WebSocket URL to connect to.
ha_core_config: HaCoreConfig | None = None
instance-attribute
Home Assistant's location and time zone from its last good get_config read; None until the first load.
Set by the RUNNING gate on every connection, before CONNECTED, and replaced on core_config_updated.
Kept across a disconnect, and kept unchanged when a read or parse fails.
config_log_level: LogLevel
property
Return the logging.websocket level from the config.
connection_state: ConnectionState
property
Return the current WebSocket connection state (read-only).
has_ever_connected: bool
property
True once the connection has reached CONNECTED at least once; never reverts.
connection_timeout_seconds: int
property
Seconds aiohttp allows to open the WebSocket connection, from the websocket config.
heartbeat_interval_seconds: int
property
Seconds between aiohttp keep-alive pings on the WebSocket, from the websocket config.
is_connected: bool
property
True while the connection state is CONNECTED, i.e. externally ready for HA traffic.
on_initialize() -> None
async
Mark the service lifecycle-ready unconditionally, independent of HA reachability.
This separates "service lifecycle ready" (the service is running and will attempt
connections) from "HA connected" (the WebSocket handshake succeeded). Without this,
WebsocketService's wave in run_forever() times out when HA is unreachable, triggering
a fatal shutdown before later waves (e.g. WebApiService) ever start. serve() begins
the actual connection loop afterward. Readiness then stays set until shutdown; the HA
connection's status is shown through the ready reason (report_connection_status).
Both resolved Home Assistant URLs are logged here, before any connection attempt, so a
base_url whose path is wrong shows up in the log even when every connection fails.
Source code in src/hassette/core/websocket_service.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
set_connection_state(new: ConnectionState) -> None
Transition to a new connection state with validation.
Validates the transition against WS_VALID_TRANSITIONS. An invalid transition logs a WARNING and is still applied. Logs every transition at DEBUG with the previous state.
Only the first CONNECTED is logged at INFO here, so the Home Assistant connection is
visible at the default log level. That is the line the quickstart tells a new user to
look for to confirm their URL and token work. Reconnects and drops are logged by
OutageLog.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new
|
ConnectionState
|
The new connection state to transition to. |
required |
Source code in src/hassette/core/websocket_service.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
wait_connected(*, timeout: float | None = None) -> bool
async
Wait until the Home Assistant WebSocket is connected and subscribed.
Resource readiness only means this service is running and attempting to connect; callers that need HA data should wait on the connection state explicitly.
Source code in src/hassette/core/websocket_service.py
263 264 265 266 267 268 269 270 271 272 273 274 275 | |
get_connected_generation() -> int | None
Return the active externally ready connection generation, if any.
Source code in src/hassette/core/websocket_service.py
277 278 279 280 281 | |
wait_connected_generation(*, timeout: float | None = None) -> int | None
async
Wait for an externally ready connection and return its active generation.
Source code in src/hassette/core/websocket_service.py
283 284 285 286 287 288 289 290 | |
before_shutdown() -> None
async
Notify disconnect observers and send the connection-lost event before teardown begins.
Source code in src/hassette/core/websocket_service.py
292 293 294 295 | |
serve() -> None
async
Connect to Home Assistant and keep reconnecting in place until shutdown.
This is the connection's only retry loop: no Home Assistant-side failure leaves it, so an
HA restart or outage is never reported as this required service's failure.
ReconnectPolicy classifies each failure and paces the next attempt; a rejected token
parks the loop instead. Only the exceptions in PROPAGATED_EXCEPTIONS (and
cancellation, at shutdown) escape.
Source code in src/hassette/core/websocket_service.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
run_connection(session: aiohttp.ClientSession) -> None
async
Run one connection from handshake to drop; always raises whatever ended it.
Source code in src/hassette/core/websocket_service.py
334 335 336 337 338 339 340 341 342 343 | |
handle_connection_failure(exc: Exception) -> RetryPlan
async
Tear down the failed connection, notify listeners once, and decide the next step.
Each disconnected signal fires only if its connected counterpart did (_observers_announced,
_bus_announced).
Source code in src/hassette/core/websocket_service.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
park() -> None
async
Stay up without reconnecting until shutdown cancels serve() (D3, D10).
Source code in src/hassette/core/websocket_service.py
372 373 374 375 376 | |
report_connection_status(reason: str) -> None
async
Show the HA connection's status as this service's ready reason (D10).
Lifecycle readiness is untouched: it stays set from on_initialize to shutdown, so the
connection never gates a startup wave. A status event is emitted only when the reason
changes, so retries with an unchanged cause emit nothing (D11). Shutdown's reason wins.
Source code in src/hassette/core/websocket_service.py
378 379 380 381 382 383 384 385 386 387 388 | |
connect_ws(session: aiohttp.ClientSession) -> None
async
Open the WebSocket connection and authenticate.
Sets self._ws. Messages larger than WS_MAX_MSG_SIZE close the socket with code 1009.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
ClientSession
|
The aiohttp ClientSession to use for the WebSocket connection. |
required |
Source code in src/hassette/core/websocket_service.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
start_recv_and_subscribe() -> asyncio.Task
async
Spawn the recv loop, open the send gate, and subscribe to Home Assistant events.
Returns:
| Type | Description |
|---|---|
Task
|
The recv loop task. |
Source code in src/hassette/core/websocket_service.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
wait_for_core_running(recv_task: asyncio.Task) -> None
async
Hold the connection in CONNECTING until Home Assistant's core state is RUNNING (D12).
Reads get_config over the auth-level send gate (Api.get_config refuses anything
short of CONNECTED). While HA is starting, waits for homeassistant_started (caught by
dispatch_hass_event) and re-reads get_config every CORE_RUNNING_RECHECK_SECONDS
in case that event was missed. There is no deadline: HA bounds STARTING itself.
Raises:
| Type | Description |
|---|---|
CoreShuttingDownError
|
If HA reports a shutting-down core state. |
Source code in src/hassette/core/websocket_service.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
store_ha_core_config(config: dict[str, typing.Any]) -> None
Replace ha_core_config from a get_config response; a malformed one keeps the previous value.
Source code in src/hassette/core/websocket_service.py
459 460 461 462 463 464 465 466 | |
refresh_ha_core_config() -> None
async
Re-read get_config and replace ha_core_config; a failed read keeps the previous value.
Source code in src/hassette/core/websocket_service.py
468 469 470 471 472 473 474 475 476 477 | |
mark_connected(recv_task: asyncio.Task) -> None
async
Advertise external readiness: CONNECTED, a new generation, observers, and the public event.
Source code in src/hassette/core/websocket_service.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | |
partial_cleanup() -> None
async
Cancel recv task, close WebSocket, clear futures and subscriptions.
Does NOT close self._session — that is owned by serve()'s async with block. Suppresses all exceptions so cleanup never prevents retry.
Source code in src/hassette/core/websocket_service.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |
cleanup() -> None
async
Cleanup resources after the WebSocket connection is closed.
Source code in src/hassette/core/websocket_service.py
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | |
authenticate() -> None
async
Authenticate with the Home Assistant WebSocket API.
Source code in src/hassette/core/websocket_service.py
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | |
send_connection_lost_event() -> None
async
Send a connection lost event to the event bus, once per connection that announced itself.
Gated on websocket_connected having reached the bus (is_ready() never tracks the connection), so failed
attempts, retries and before_shutdown emit no spurious or duplicate event. Bus errors are
swallowed, so a bus failure cannot mask a network error.
Source code in src/hassette/core/websocket_service.py
618 619 620 621 622 623 624 625 626 627 628 629 630 | |
send_connection_established_event() -> None
async
Send a connection established event to the event bus.
Source code in src/hassette/core/websocket_service.py
632 633 634 635 636 637 638 639 640 641 642 | |