State Manager
Type stub file for States resource.
This stub provides static type hints for all known domain properties on the States class, enabling IDE autocomplete and type checking. At runtime, these properties are handled by getattr in states.py, which queries the state registry dynamically.
For custom domains not listed here, users should use:
app.states[CustomStateClass]
As this will ensure proper type annotations of the return type.
DomainStates
DomainStates provides access to all states within a specific domain, with automatic type validation and caching.
This class accesses the StateProxy under the hood to provide access to the current states from HomeAssistant, without needing to make direct calls to the Home Assistant API.
Accessed states are automatically validated against the provided model and cached for efficient repeated access.
Examples:
# if you know the entity exists
light_state = self.states.light["bedroom"]
# to safely access an entity that may not exist
light_state = self.states.light.get("bedroom")
if light_state is not None:
self.logger.info("Light state: %s", light_state.value)
# or you can check existence ahead of time
if "bedroom" in self.states.light:
light_state = self.states.light["bedroom"]
self.logger.info("Light state: %s", light_state.value)
# if you are working with a state that isn't defined in Hassette
custom_states = self.states[CustomStateClass]
for entity_id, state in custom_states:
self.logger.info("%s: %s", entity_id, state.value)
Source code in src/hassette/state_manager/state_manager.py
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 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 | |
get(entity_id: str) -> StateT | None
Get a specific entity state by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str
|
The full entity ID (e.g., "light.bedroom") or just the entity name (e.g., "bedroom"). |
required |
Returns:
| Type | Description |
|---|---|
StateT | None
|
The typed state if found and matches domain, None otherwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the entity ID does not belong to this domain. |
Source code in src/hassette/state_manager/state_manager.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
items() -> Iterator[tuple[str, StateT]]
Iterate (entity_id, typed state) pairs lazily.
Source code in src/hassette/state_manager/state_manager.py
110 111 112 | |
keys() -> list[str]
Return a list of entity IDs for this domain.
Source code in src/hassette/state_manager/state_manager.py
114 115 116 | |
iterkeys() -> Iterator[str]
Returns an iterator over entity IDs for this domain.
Source code in src/hassette/state_manager/state_manager.py
118 119 120 121 | |
values() -> list[StateT]
Return a list of typed states for this domain.
This returns an eagerly evaluated list of all typed states in this domain.
Note
This method will iterate over all states in the domain and validate them,
which may be expensive for large domains. Consider using the iterator
returned by __iter__ for lazy evaluation if performance is a concern.
Source code in src/hassette/state_manager/state_manager.py
123 124 125 126 127 128 129 130 131 132 133 | |
itervalues() -> Iterator[StateT]
Returns an iterator over typed states for this domain.
Source code in src/hassette/state_manager/state_manager.py
135 136 137 138 | |
to_dict() -> dict[str, StateT]
Return a dictionary of entity_id to typed state for this domain.
This returns an eagerly evaluated dictionary of all typed states in this domain.
Note
This method will iterate over all states in the domain and validate them,
which may be expensive for large domains. Consider using the iterator
returned by __iter__ for lazy evaluation if performance is a concern.
Source code in src/hassette/state_manager/state_manager.py
140 141 142 143 144 145 146 147 148 149 150 | |
__iter__() -> typing.Generator[tuple[str, StateT], typing.Any]
Iterate over all states in this domain.
Source code in src/hassette/state_manager/state_manager.py
152 153 154 155 156 157 158 159 160 161 | |
__len__() -> int
Return the number of entities in this domain.
Source code in src/hassette/state_manager/state_manager.py
163 164 165 | |
__contains__(entity_id: str) -> bool
Check if a specific entity ID exists in this domain.
Source code in src/hassette/state_manager/state_manager.py
167 168 169 170 171 172 173 | |
__getitem__(entity_id: str) -> StateT
Get a specific entity state by ID, raising if not found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str
|
The full entity ID (e.g., "light.bedroom") or just the entity name (e.g., "bedroom"). |
required |
Raises:
| Type | Description |
|---|---|
EntityNotFoundError
|
If the entity is not found. |
Returns:
| Type | Description |
|---|---|
StateT
|
The typed state. |
Source code in src/hassette/state_manager/state_manager.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
__repr__() -> str
Return a string representation of the DomainStates container.
Source code in src/hassette/state_manager/state_manager.py
192 193 194 | |
__bool__() -> bool
Return True if there are any entities in this domain.
Source code in src/hassette/state_manager/state_manager.py
196 197 198 | |
StateManager
Bases: Resource
Resource for managing Home Assistant states.
Provides typed access to entity states by domain through dynamic properties.
Examples:
```python # Iterate over all lights for entity_id, light_state in self.states.light: self.logger.info("%s: %s", entity_id, light_state.value)
# Get specific entity
bedroom_light = self.states.light.get("light.bedroom")
if bedroom_light and bedroom_light.attributes.brightness:
self.logger.info("Brightness: %s", bedroom_light.attributes.brightness)
# Check count
self.logger.info("Total lights: %d", len(self.states.light))
Source code in src/hassette/state_manager/state_manager.py
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 | |
config_log_level: LOG_LEVEL_TYPE
property
Return the log level from the config for this resource.
__getattr__(domain: str) -> DomainStates[BaseState]
Dynamically access domain states by property name.
This method provides dynamic access to domain states at runtime while
maintaining type safety through the companion .pyi stub file. For known
domains (defined in the stub), IDEs will provide full type hints. For
custom/unknown domains, use get_states(CustomStateClass) directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain
|
str
|
The domain name (e.g., "light", "switch", "custom_domain"). |
required |
Returns:
| Type | Description |
|---|---|
DomainStates[BaseState]
|
DomainStates container for the requested domain. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the attribute name matches a reserved name or if the domain is not registered in the state registry. |
Example
# Known domain (typed via .pyi stub)
for entity_id, light in self.states.light:
print(light.attributes.brightness)
# Custom domain (fallback to BaseState at runtime)
custom_states = self.states.custom_domain
for entity_id, state in custom_states:
print(state.value)
Source code in src/hassette/state_manager/state_manager.py
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 | |
__getitem__(model: type[StateT]) -> DomainStates[StateT]
Access domain states using the indexing syntax. This is required if you need to access domain states for a state model class that is not known by the StateRegistry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[StateT]
|
The state model class representing the domain. |
required |
Returns:
| Type | Description |
|---|---|
DomainStates[StateT]
|
DomainStates container for the specified domain. |
Example
my_state_instance = self.states[MyStateClass].get("custom_entity")
Source code in src/hassette/state_manager/state_manager.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
get(entity_id: str) -> states.BaseState | None
Get a state by entity ID, returning the most specific type available.
This method provides generic access to any entity state, regardless of whether a domain-specific state class is registered. If a specific class is registered (e.g., LightState for domain "light"), it will be used. Otherwise, the state is returned as a BaseState instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str
|
Full entity ID (e.g., "light.bedroom" or "test.test_entity") |
required |
Returns:
| Type | Description |
|---|---|
BaseState | None
|
Typed state object (domain-specific or BaseState), or None if not found. |
Examples:
# Get a registered domain (returns LightState)
light = self.states.get("light.bedroom")
# Get an unregistered domain (returns BaseState)
test_entity = self.states.get("test.test_entity")
if test_entity:
print(f"Domain: {test_entity.domain}, Value: {test_entity.value}")
Source code in src/hassette/state_manager/state_manager.py
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 | |
__contains__(model: type[StateT]) -> bool
Check the global STATE_REGISTRY, not this proxy's cached instances.
Source code in src/hassette/state_manager/state_manager.py
355 356 357 | |
__iter__() -> Iterator[tuple[StateKey, DomainStates[states.BaseState]]]
Iterate over all registered state classes with their keys.
Source code in src/hassette/state_manager/state_manager.py
359 360 361 362 | |
items() -> Iterator[tuple[StateKey, DomainStates[states.BaseState]]]
Iterate over all registered state classes with their keys.
Source code in src/hassette/state_manager/state_manager.py
364 365 366 | |
values() -> Iterator[DomainStates[states.BaseState]]
Iterate over all registered state classes.
Returns:
| Type | Description |
|---|---|
Iterator[DomainStates[BaseState]]
|
An iterator over all registered DomainStates instances. |
Source code in src/hassette/state_manager/state_manager.py
368 369 370 371 372 373 374 375 | |
keys() -> Iterator[StateKey]
Iterate over all registered state keys.
Returns:
| Type | Description |
|---|---|
Iterator[StateKey]
|
An iterator over all registered state keys. |
Source code in src/hassette/state_manager/state_manager.py
377 378 379 380 381 382 383 | |