Conversion
AnnotationConverter
Converts runtime values to match rich annotations (including nested containers).
Source code in src/hassette/conversion/annotation_converter.py
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 | |
StateKey
dataclass
Source code in src/hassette/models/states/catalog.py
20 21 22 23 24 25 26 | |
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').
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
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 203 204 205 206 207 208 209 210 211 212 | |
registry: dict[StateKey, type[BaseState]]
property
Read accessor over the catalog leaf dict — for validation.py and other readers.
try_convert_state(data: HassStateDict, entity_id: str | None = None) -> BaseState
Convert a raw HA state dict to the most specific registered state class, falling back to 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 an event payload. |
InvalidEntityIdError
|
If the entity_id is missing or malformed. |
UnableToConvertStateError
|
If conversion to the resolved state class fails. |
Source code in src/hassette/conversion/state_registry.py
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 | |
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"). |
None
|
device_class
|
Hashable | None
|
The device class (e.g., "temperature", "motion"). |
None
|
Source code in src/hassette/conversion/state_registry.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
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
131 132 133 134 | |
coerce_and_construct(state_class: type[BaseState], data: HassStateDict, entity_id: str) -> BaseState
Coerce a raw HA state dict to a typed model using a known target class.
Applies domain extraction and unknown/unavailable normalization before coercing the value, so the pipeline never attempts to coerce "unknown" or "unavailable" against a non-string value_type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_class
|
type[BaseState]
|
The target state model class (e.g., LightState, SensorState). |
required |
data
|
HassStateDict
|
Raw state dict from Home Assistant. |
required |
entity_id
|
str
|
The entity ID for domain extraction and error reporting. |
required |
Returns:
| Type | Description |
|---|---|
BaseState
|
The typed state model instance. |
Raises:
| Type | Description |
|---|---|
UnableToConvertStateError
|
If coercion or validation fails. |
Source code in src/hassette/conversion/state_registry.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
conversion_with_error_handling(state_class: type[BaseState], data: HassStateDict, entity_id: str, domain: str) -> BaseState
Convert state data, logging and re-raising as UnableToConvertStateError on failure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_class
|
type[BaseState]
|
The target state model class. |
required |
data
|
HassStateDict
|
Raw state dict from Home Assistant. |
required |
entity_id
|
str
|
The entity ID for error reporting. |
required |
domain
|
str
|
The HA domain string (e.g., "light", "sensor"). |
required |
Returns:
| Type | Description |
|---|---|
BaseState
|
The typed state model instance. |
Raises:
| Type | Description |
|---|---|
UnableToConvertStateError
|
If conversion fails for any reason. |
Source code in src/hassette/conversion/state_registry.py
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 | |
__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
197 198 199 | |
__iter__() -> Iterator[tuple[StateKey, type[BaseState]]]
Iterate over all registered state classes with their keys.
Source code in src/hassette/conversion/state_registry.py
201 202 203 | |
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 | |
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 | |
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.func.__name__}")
Source code in src/hassette/conversion/type_registry.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
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 HA state dict to a typed state model via preprocessing + value coercion.
Applies the same preprocessing order as the old model validator: domain extraction, then unknown/unavailable normalization (setting state to None before coercion touches it), then TYPE_REGISTRY.convert for the model's value_type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The raw state dict from Home Assistant (or an already-validated model instance). |
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
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 | |
register_state_converter(state_class: type[BaseState], domain: Hashable, device_class: Hashable | None = None) -> None
Register a state class for a specific domain and optional device class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_class
|
type[BaseState]
|
The state class to register. Must be a subclass of BaseState. |
required |
domain
|
Hashable
|
The Home Assistant domain (e.g., "light", "sensor"). |
required |
device_class
|
Hashable | None
|
The device class (e.g., "temperature", "motion"). |
None
|
Source code in src/hassette/models/states/catalog.py
29 30 31 32 33 34 35 36 37 38 39 40 | |
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 | |