Integration
App-declared Home Assistant entities: self.integration and the handles it returns (spec 157).
CommandDispatchResult
Bases: StrEnum
What IntegrationService.dispatch_command did with a press or toggle.
Source code in src/hassette/integration/declarations.py
47 48 49 50 51 52 53 54 55 56 57 58 | |
DISPATCHED = 'dispatched'
class-attribute
instance-attribute
The command event was published. Not proof the handler ran: its execution mode may drop it.
NOT_RUNNING = 'not_running'
class-attribute
instance-attribute
The instance has no live Integration owner (it hasn't committed yet, or has stopped), so nothing was
published.
NOT_DECLARED = 'not_declared'
class-attribute
instance-attribute
The instance's committed declarations have no such key on that platform, so nothing was published.
IntegrationPlatform
Bases: StrEnum
The Home Assistant platform a declaration creates an entity on.
Source code in src/hassette/integration/declarations.py
39 40 41 42 43 44 | |
IntegrationCommandRejectedError
Bases: HassetteError
Raised when AppTestHarness simulates a button press or switch toggle that production would drop.
Raised by simulate_button_press and simulate_switch_toggle. Home Assistant's press or toggle is dropped
when the app instance isn't running (it hasn't started, or has stopped) or when the instance's committed
declarations have no such key on that platform.
Attributes:
| Name | Type | Description |
|---|---|---|
reason |
Why the command was rejected: |
|
instance_name |
The app instance the command targeted. |
|
key |
The key the command targeted. |
|
platform |
The platform the command targeted. |
Source code in src/hassette/integration/errors.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
IntegrationDeclarationClosedError
Bases: HassetteError
Raised by add_button, add_switch or add_sensor after the instance's declarations were committed.
An instance declares its entities while it initializes. The window closes when hassette commits the declared set, just before the instance is marked running, so the list Home Assistant sees is always complete.
Attributes:
| Name | Type | Description |
|---|---|---|
key |
The key the late call tried to declare. |
|
platform |
The platform it tried to declare it on. |
Source code in src/hassette/integration/errors.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
IntegrationHandleClosedError
Bases: HassetteError
Raised by a handle's set() or state read once the instance that declared it has stopped.
Also raised when the instance's declarations failed to commit. Stops a task that outlived its instance (for example, across a reload) from reading or overwriting the state of the instance that replaced it.
Attributes:
| Name | Type | Description |
|---|---|---|
instance_name |
The instance that declared the handle. |
|
key |
The handle's key. |
Source code in src/hassette/integration/errors.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
IntegrationButton
Bases: IntegrationEntityHandle
A button an app instance declared. Pressing it in Home Assistant runs its on_press handler.
Source code in src/hassette/integration/handles.py
42 43 44 45 46 47 48 49 50 | |
subscription: Subscription = subscription
instance-attribute
The bus listener that runs on_press; it shows in the app's listeners view.
IntegrationSensor
Bases: IntegrationStatefulHandle
A sensor an app instance declared. The app sets its value; Home Assistant shows it.
Source code in src/hassette/integration/handles.py
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 | |
value: EntityValue
property
The current value; None is unknown.
set(value: object, attributes: Mapping[str, Any] | None = None) -> None
async
Set the sensor's value, replacing its attributes.
The value must suit the sensor's declaration, as Home Assistant requires: a number for a numeric sensor,
one of its options for an enum sensor, an aware whenever time for a timestamp sensor (stored as an
Instant), a whenever.Date for a date sensor, and a string otherwise. None makes it unknown.
During initialization the value is held and written when the instance's declarations are committed. After that it is written before it is published; setting the state it already has writes nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
The new value. |
required |
attributes
|
Mapping[str, Any] | None
|
The new attributes, a JSON-serializable mapping. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
The declaration rejects |
IntegrationHandleClosedError
|
The instance has stopped, or its declarations failed to commit. |
Source code in src/hassette/integration/handles.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
IntegrationSwitch
Bases: IntegrationStatefulHandle
A switch an app instance declared.
Toggling it in Home Assistant runs its on_toggle handler with the commanded value (D.SwitchCommand).
Its state changes only when the app calls set(), so a handler that doesn't call it leaves the switch as it
was.
Source code in src/hassette/integration/handles.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
subscription: Subscription = subscription
instance-attribute
The bus listener that runs on_toggle; it shows in the app's listeners view.
is_on: bool
property
Whether the switch is on.
set(on: bool, attributes: Mapping[str, Any] | None = None) -> None
async
Set the switch on or off, replacing its attributes.
During initialization the value is held and written when the instance's declarations are committed. After that it is written before it is published; setting the state it already has writes nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on
|
bool
|
|
required |
attributes
|
Mapping[str, Any] | None
|
The new attributes, a JSON-serializable mapping. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
|
IntegrationHandleClosedError
|
The instance has stopped, or its declarations failed to commit. |
Source code in src/hassette/integration/handles.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
Integration
Bases: Resource
Declares an app instance's buttons, switches and sensors, which the hass-hassette integration shows in HA.
Declare every entity in on_initialize. hassette commits the declared set just before the instance is
marked running, replacing the set from its last run: an entity no longer declared is removed with its state.
A switch or sensor keeps its state across restarts for as long as it stays declared.
Source code in src/hassette/integration/integration.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 | |
sync: IntegrationSyncFacade = self.add_child(IntegrationSyncFacade, integration=self)
instance-attribute
Synchronous versions of add_* and set(), for AppSync apps.
config_log_level: LogLevel
property
Return the app handler's configured log level (logging.app_handler).
app: App[Any]
property
The app instance that owns this integration.
app_key: str
property
The owning app's key.
instance_name: str
property
The owning instance's name.
instance_key: tuple[str, str]
property
(app_key, instance_name): the instance this resource declares for.
on_initialize() -> None
async
Mark ready; declarations happen during the app's own initialization, before this runs.
Source code in src/hassette/integration/integration.py
89 90 91 | |
add_button(key: str, *, name: str, on_press: HandlerType, icon: str | None = None, mode: ExecutionMode | str | None = None, entity_category: str | None = None, default_entity_id: str | None = None) -> Coroutine[Any, Any, IntegrationButton]
Declare a button. Pressing it in Home Assistant runs on_press.
Must be awaited, during initialization. on_press is registered as a bus listener named
integration.button.<key>, so it gets the bus's execution modes, error handling and telemetry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Identifies the button within this instance: lowercase letters, digits and |
required |
name
|
str
|
The button's display name in Home Assistant. |
required |
on_press
|
HandlerType
|
The handler to run on a press. |
required |
icon
|
str | None
|
The button's icon, an icon name such as |
None
|
mode
|
ExecutionMode | str | None
|
What a press does while the previous one is still running; the bus's |
None
|
entity_category
|
str | None
|
|
None
|
default_entity_id
|
str | None
|
A full entity id ( |
None
|
Returns:
| Type | Description |
|---|---|
Coroutine[Any, Any, IntegrationButton]
|
The button's handle. |
Raises:
| Type | Description |
|---|---|
ValueError
|
An invalid or duplicate key, an invalid name, an invalid |
IntegrationDeclarationClosedError
|
The instance's declarations were already committed. |
Source code in src/hassette/integration/integration.py
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 | |
add_switch(key: str, *, name: str, on_toggle: HandlerType, initial: bool = False, icon: str | None = None, entity_category: str | None = None, mode: ExecutionMode | str | None = None, default_entity_id: str | None = None) -> Coroutine[Any, Any, IntegrationSwitch]
Declare a switch. Toggling it in Home Assistant runs on_toggle.
Must be awaited, during initialization. on_toggle is registered as a bus listener named
integration.switch.<key>; it reads the commanded value with D.SwitchCommand. The switch's state
changes only when the app calls set(), so call it first thing in the handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Identifies the switch within this instance: lowercase letters, digits and |
required |
name
|
str
|
The switch's display name in Home Assistant. |
required |
on_toggle
|
HandlerType
|
The handler to run on a toggle. |
required |
initial
|
bool
|
The state of a switch that has never been stored. Changing it later has no effect on a switch that already has a stored state. |
False
|
icon
|
str | None
|
The switch's icon, an icon name such as |
None
|
entity_category
|
str | None
|
|
None
|
mode
|
ExecutionMode | str | None
|
What a toggle does while the previous one is still running; the bus's |
None
|
default_entity_id
|
str | None
|
A full entity id ( |
None
|
Returns:
| Type | Description |
|---|---|
Coroutine[Any, Any, IntegrationSwitch]
|
The switch's handle. |
Raises:
| Type | Description |
|---|---|
ValueError
|
An invalid or duplicate key, an invalid name, a non-bool |
IntegrationDeclarationClosedError
|
The instance's declarations were already committed. |
Source code in src/hassette/integration/integration.py
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 | |
add_sensor(key: str, *, name: str, icon: str | None = None, unit: str | None = None, device_class: str | None = None, state_class: str | None = None, options: list[str] | None = None, entity_category: str | None = None, default_entity_id: str | None = None) -> Coroutine[Any, Any, IntegrationSensor]
Declare a sensor, whose value the app sets.
Must be awaited, during initialization. The device class, unit and state class decide which values
set() accepts, following Home Assistant's own rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Identifies the sensor within this instance: lowercase letters, digits and |
required |
name
|
str
|
The sensor's display name in Home Assistant. |
required |
icon
|
str | None
|
The sensor's icon, an icon name such as |
None
|
unit
|
str | None
|
The unit of measurement; a sensor with a unit is numeric. |
None
|
device_class
|
str | None
|
One of Home Assistant's sensor device classes ( |
None
|
state_class
|
str | None
|
|
None
|
options
|
list[str] | None
|
The allowed values of an enum sensor ( |
None
|
entity_category
|
str | None
|
|
None
|
default_entity_id
|
str | None
|
A full entity id ( |
None
|
Returns:
| Type | Description |
|---|---|
Coroutine[Any, Any, IntegrationSensor]
|
The sensor's handle. |
Raises:
| Type | Description |
|---|---|
ValueError
|
An invalid or duplicate key, an invalid name, an unknown device or state class, an enum
sensor without options or with a unit or state class, options on another sensor, an invalid
|
IntegrationDeclarationClosedError
|
The instance's declarations were already committed. |
Source code in src/hassette/integration/integration.py
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 | |
on_shutdown() -> None
async
Close this resource's handles: later reads and writes raise IntegrationHandleClosedError.
Source code in src/hassette/integration/integration.py
516 517 518 | |
IntegrationSyncFacade
Bases: Resource
Synchronous versions of self.integration's add_* methods and of each handle's set().
For code running outside the event loop, such as an AppSync hook in a worker thread. Calling these from
the event loop raises RuntimeError; use the async methods there.
Source code in src/hassette/integration/sync.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 | |
config_log_level: LogLevel
property
Return the app handler's configured log level, shared with Integration.
on_initialize() -> None
async
Mark the facade ready; it wraps Integration and needs no setup of its own.
Source code in src/hassette/integration/sync.py
31 32 33 | |
add_button(key: str, *, name: str, on_press: HandlerType, icon: str | None = None, mode: ExecutionMode | str | None = None, entity_category: str | None = None, default_entity_id: str | None = None) -> IntegrationButton
Declare a button; see Integration.add_button.
Source code in src/hassette/integration/sync.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
add_switch(key: str, *, name: str, on_toggle: HandlerType, initial: bool = False, icon: str | None = None, entity_category: str | None = None, mode: ExecutionMode | str | None = None, default_entity_id: str | None = None) -> IntegrationSwitch
Declare a switch; see Integration.add_switch.
Source code in src/hassette/integration/sync.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
add_sensor(key: str, *, name: str, icon: str | None = None, unit: str | None = None, device_class: str | None = None, state_class: str | None = None, options: list[str] | None = None, entity_category: str | None = None, default_entity_id: str | None = None) -> IntegrationSensor
Declare a sensor; see Integration.add_sensor.
Source code in src/hassette/integration/sync.py
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 | |
set(handle: IntegrationSwitch | IntegrationSensor, value: Any, attributes: Mapping[str, Any] | None = None) -> None
Set a switch's or sensor's state; see IntegrationSwitch.set and IntegrationSensor.set.
Source code in src/hassette/integration/sync.py
118 119 120 121 122 123 124 125 | |