Conversion
AnnotationConverter
Converts runtime values to match rich annotations (including nested containers).
Source code in src/hassette/conversion/annotation_converter.py
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 | |
StateKey
dataclass
Source code in src/hassette/conversion/state_registry.py
22 23 24 25 26 27 28 | |
domain: Hashable | None = None
class-attribute
instance-attribute
The domain of the entity (e.g., 'light', 'sensor').
device_class: Hashable | None = None
class-attribute
instance-attribute
Optional device class of the entity (e.g., 'temperature', 'humidity'). Not yet being used.
StateRegistry
Registry for mapping domains to their state classes.
This class maintains a mapping of Home Assistant domains to their corresponding
BaseState subclasses. State classes get registered during the after_initialize phase
by scanning all subclasses of BaseState.
Source code in src/hassette/conversion/state_registry.py
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 199 200 201 202 | |
try_convert_state(data: HassStateDict, entity_id: str | None = None) -> BaseState
Convert a dictionary representation of a state into a specific state type.
This function uses the state registry to look up the appropriate state class based on the entity's domain. If no specific class is registered for the domain, it falls back to the generic BaseState.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
HassStateDict
|
Dictionary containing state data from Home Assistant. |
required |
entity_id
|
str | None
|
Optional entity ID to assist in domain determination. |
None
|
Returns:
| Type | Description |
|---|---|
BaseState
|
A properly typed state object (e.g., LightState, SensorState) or BaseState |
BaseState
|
for unknown domains. |
Raises:
| Type | Description |
|---|---|
InvalidDataForStateConversionError
|
If the provided data is invalid or malformed. |
InvalidEntityIdError
|
If the entity_id is invalid or malformed. |
UnableToConvertStateError
|
If conversion to the determined state class fails. |
Example
state_dict = {"entity_id": "light.bedroom", "state": "on", ...}
light_state = try_convert_state(state_dict) # Returns LightState instance
Source code in src/hassette/conversion/state_registry.py
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 | |
register(state_class: type[BaseState], *, domain: Hashable | None = None, device_class: Hashable | None = None) -> None
classmethod
Register a state class for a given domain and optional device_class combination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_class
|
type[BaseState]
|
The state class to register. Must be a subclass of BaseState. |
required |
domain
|
Hashable | None
|
The Home Assistant domain (e.g., "light", "sensor"). If None, matches any domain. |
None
|
device_class
|
Hashable | None
|
The device class (e.g., "temperature", "motion"). If None, matches any device class. |
None
|
Source code in src/hassette/conversion/state_registry.py
123 124 125 126 127 128 129 130 131 132 133 134 135 | |
resolve(*, domain: Hashable | None = None, device_class: Hashable | None = None) -> type[BaseState] | None
classmethod
Resolve a state class from the registry based on domain and device_class.
Source code in src/hassette/conversion/state_registry.py
137 138 139 140 141 142 143 144 145 146 147 148 149 | |
__contains__(model: type[BaseState]) -> bool
Check if the registry contains a state class for the given model.
Source code in src/hassette/conversion/state_registry.py
177 178 179 | |
__iter__() -> Iterator[tuple[StateKey, type[BaseState]]]
Iterate over all registered state classes with their keys.
Source code in src/hassette/conversion/state_registry.py
181 182 183 | |
snapshot() -> dict[StateKey, type[BaseState]]
classmethod
Return a shallow copy of the current registry state.
Source code in src/hassette/conversion/state_registry.py
194 195 196 197 | |
restore(snapshot: dict[StateKey, type[BaseState]]) -> None
classmethod
Replace the registry with a previously captured snapshot.
Source code in src/hassette/conversion/state_registry.py
199 200 201 202 | |
TypeMatcher
Runtime matcher for checking if values already satisfy nested type annotations.
Source code in src/hassette/conversion/type_matcher.py
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 | |
TypeConverterEntry
dataclass
Bases: Generic[T, R]
Represents a type conversion function and its associated metadata.
Source code in src/hassette/conversion/type_registry.py
40 41 42 43 44 45 46 47 48 | |
TypeRegistry
Registry for converting between types, used by State models and the Dependency Injection system.
Source code in src/hassette/conversion/type_registry.py
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 | |
register(type_converter: TypeConverterEntry[Any, Any]) -> None
classmethod
Register a type converter in the registry.
Source code in src/hassette/conversion/type_registry.py
147 148 149 150 151 152 153 154 155 | |
convert(value: Any, to_type: type[Any] | tuple[type[Any], ...]) -> Any
Convert a StateValue to a target Python type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The StateValue instance to convert. |
required |
to_type
|
type[Any] | tuple[type[Any], ...]
|
The target Python type. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The converted value. |
Source code in src/hassette/conversion/type_registry.py
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 | |
list_conversions() -> list[tuple[type, type, TypeConverterEntry]]
List all registered type conversions.
Returns a sorted list of all registered type conversions with their metadata. Useful for debugging and inspection of available converters.
Returns:
| Type | Description |
|---|---|
list[tuple[type, type, TypeConverterEntry]]
|
List of (from_type, to_type, entry) tuples sorted by from_type name then to_type name. |
Example
from hassette import TYPE_REGISTRY
conversions = TYPE_REGISTRY.list_conversions()
for from_type, to_type, entry in conversions:
print(f"{from_type.__name__} → {to_type.__name__}: {entry.description}")
Source code in src/hassette/conversion/type_registry.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
snapshot() -> dict[tuple[type[Any], type[Any]], TypeConverterEntry[Any, Any]]
classmethod
Return a shallow copy of the current conversion map.
Source code in src/hassette/conversion/type_registry.py
266 267 268 269 | |
restore(snapshot: dict[tuple[type[Any], type[Any]], TypeConverterEntry[Any, Any]]) -> None
classmethod
Replace the conversion map with a previously captured snapshot.
Source code in src/hassette/conversion/type_registry.py
271 272 273 274 | |
RegistryValidationIssue
dataclass
A single issue found during registry validation.
Attributes:
| Name | Type | Description |
|---|---|---|
registry |
str
|
Which registry produced the issue — |
severity |
str
|
|
message |
str
|
Human-readable description of the issue. |
Source code in src/hassette/conversion/validation.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
convert_state_dict_to_model(value: typing.Any, model: type[BaseState]) -> BaseState
Convert a raw Home Assistant state dict to a typed state model.
This converter is used by state object extractors (StateNew, StateOld, etc.) to transform the raw state dictionary from Home Assistant into a strongly-typed Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The raw state dict from Home Assistant |
required |
model
|
type[BaseState]
|
The target state model class (e.g., LightState, SensorState) |
required |
Returns:
| Type | Description |
|---|---|
BaseState
|
The typed state model instance |
Raises:
| Type | Description |
|---|---|
TypeError
|
If value is not a dict or model instance |
ValidationError
|
If the state dict doesn't match the model schema |
Source code in src/hassette/conversion/state_registry.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
register_state_converter(state_class: type[BaseState], domain: Hashable, device_class: Hashable | None = None) -> None
Register a state converter class for a specific domain and optional device class.
Source code in src/hassette/conversion/state_registry.py
31 32 33 34 35 | |
register_simple_type_converter(from_type: type[T], to_type: type[R], fn: Callable[[T], R] | None = None, error_message: str | None = None, error_types: tuple[type[BaseException], ...] = (ValueError,))
Register a simple type conversion function from a non-user defined function, such as a constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_type
|
type[T]
|
The source type to convert from. |
required |
to_type
|
type[R]
|
The target type to convert to. |
required |
fn
|
Callable[[T], R] | None
|
The function to use for conversion. If None, the target type constructor is used. |
None
|
error_message
|
str | None
|
Optional custom error message if conversion fails. |
None
|
error_types
|
tuple[type[BaseException], ...]
|
Tuple of exception types to catch and wrap in UnableToConvertValueError. |
(ValueError,)
|
Example
register_simple_type_converter(int, float, error_message="Failed to convert int to float") register_simple_type_converter(ZonedDateTime, str, fn=ZonedDateTime.format_iso)
Source code in src/hassette/conversion/type_registry.py
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 | |
register_type_converter_fn(fn: Callable[[T], R] | None = None, *, error_message: str | None = None, error_types: tuple[type[BaseException], ...] = (ValueError,)) -> Callable[[T], R] | Callable[[Callable[[T], R]], Callable[[T], R]]
register_type_converter_fn(
fn: Callable[[T], R],
) -> Callable[[T], R]
register_type_converter_fn(
fn: None = None,
*,
error_message: str | None = None,
error_types: tuple[type[BaseException], ...] = (
ValueError,
),
) -> Callable[[Callable[[T], R]], Callable[[T], R]]
Register a type conversion function with the TypeRegistry.
Can be used as:
@register_type_converter
def convert_x(value: T) -> R: ...
or:
@register_type_converter(error_message="failed to convert X")
def convert_x(value: T) -> R: ...
Source code in src/hassette/conversion/type_registry.py
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 | |
validate_registries(state_registry: StateRegistry, type_registry: TypeRegistry, *, strict: bool = False) -> list[RegistryValidationIssue]
Validate STATE_REGISTRY and TYPE_REGISTRY contents at startup.
Collects ALL issues before raising or logging — never fail-fast.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_registry
|
StateRegistry
|
The |
required |
type_registry
|
TypeRegistry
|
The |
required |
strict
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
list[RegistryValidationIssue]
|
A list of |
list[RegistryValidationIssue]
|
is healthy). |
Raises:
| Type | Description |
|---|---|
RegistryValidationError
|
In strict mode when at least one error-level issue is present. |
Source code in src/hassette/conversion/validation.py
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 | |