Accessors
Accessors are combined with predicates and annotations to easily and cleanly extract values from events. Instead of
writing a lambda like lambda e: e.payload.data.old_state_value, you can use the accessor get_state_value_old
or use get_path("payload.data.old_state_value") for a more generic solution.
You generally will not need to use these directly - the main bus helpers use them under the hood to provide
the relevant data to predicates. For example, on_state_change uses get_state_value_old and
get_state_value_new to provide the old and new state values to predicates like StateFrom and StateTo.
Examples:
Extracting a specific key from service_data
from hassette import A
from hassette import P
value_is = P.ValueIs(source=A.get_service_data_key("entity_id"), condition="light.living_room")
await self.bus.on_call_service(
"light.turn_on",
handler=handler,
where=value_is,
name="living_room_turn_on",
)
Extracting a nested value using a glom path
from hassette import A
from hassette import P
value_is = P.ValueIs(
source=A.get_path("payload.data.new_state.attributes.geolocation.locality"),
condition="San Francisco",
)
await self.bus.on_state_change(
"sensor.my_device_location",
handler=handler,
changed_to=value_is,
name="device_location",
)
get_path(path: str) -> Callable[..., Any | FalseySentinel]
Return a callable that extracts a nested value, returning MISSING_VALUE on failure.
Source code in src/hassette/event_handling/accessors.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
get_state_value_old(event: RawStateChangeEvent) -> Any | FalseySentinel
Get the old state value from a RawStateChangeEvent, or MISSING_VALUE if old_state is None.
Source code in src/hassette/event_handling/accessors.py
81 82 83 | |
get_state_value_new(event: RawStateChangeEvent) -> Any | FalseySentinel
Get the new state value from a RawStateChangeEvent, or MISSING_VALUE if new_state is None.
Source code in src/hassette/event_handling/accessors.py
86 87 88 | |
get_state_value_old_new(event: RawStateChangeEvent) -> tuple[Any, Any]
Get a tuple of (old_state_value, new_state_value) from a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
91 92 93 | |
get_state_object_old(event: RawStateChangeEvent) -> HassStateDict | None
Get the old state object from a RawStateChangeEvent, or None if old_state does not exist.
Source code in src/hassette/event_handling/accessors.py
96 97 98 | |
get_state_object_new(event: RawStateChangeEvent) -> HassStateDict | None
Get the new state object from a RawStateChangeEvent, or None if new_state does not exist.
Source code in src/hassette/event_handling/accessors.py
101 102 103 | |
get_state_object_old_new(event: RawStateChangeEvent) -> tuple[HassStateDict | None, HassStateDict | None]
Get a tuple of (old_state_object, new_state_object) from a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
106 107 108 | |
get_attr_old(name: str) -> Callable[[RawStateChangeEvent], Any]
Get a specific attribute from the old state in a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
111 112 113 114 115 116 117 118 119 | |
get_attr_new(name: str) -> Callable[[RawStateChangeEvent], Any]
Get a specific attribute from the new state in a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
122 123 124 125 126 127 128 129 130 | |
get_attr_old_new(name: str) -> Callable[[RawStateChangeEvent], tuple[Any, Any]]
Get a specific attribute from the old and new state in a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
133 134 135 136 137 138 139 140 141 142 143 144 | |
get_attrs_new(names: list[str]) -> Callable[[RawStateChangeEvent], dict[str, Any]]
Get specific attributes from the new state in a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
147 148 149 150 151 152 153 154 155 | |
get_attrs_old(names: list[str]) -> Callable[[RawStateChangeEvent], dict[str, Any]]
Get specific attributes from the old state in a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
158 159 160 161 162 163 164 165 166 | |
get_attrs_old_new(names: list[str]) -> Callable[[RawStateChangeEvent], tuple[dict[str, Any], dict[str, Any]]]
Get specific attributes from the old and new state in a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
get_all_attrs_old(event: RawStateChangeEvent) -> dict[str, Any] | FalseySentinel
Get all attributes from the old state in a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
185 186 187 188 189 190 191 | |
get_all_attrs_new(event: RawStateChangeEvent) -> dict[str, Any] | FalseySentinel
Get all attributes from the new state in a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
194 195 196 197 198 199 200 | |
get_all_attrs_old_new(event: RawStateChangeEvent) -> tuple[dict[str, Any] | FalseySentinel, dict[str, Any] | FalseySentinel]
Get all attributes from the old and new state in a RawStateChangeEvent.
Source code in src/hassette/event_handling/accessors.py
203 204 205 206 207 208 209 | |
get_domain(event: HassEvent) -> str | FalseySentinel
Get the domain from the event payload.
Source code in src/hassette/event_handling/accessors.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
get_entity_id(event: HassEvent) -> str | FalseySentinel
Get the entity_id from the event payload.
Source code in src/hassette/event_handling/accessors.py
228 229 230 231 232 233 234 235 236 237 | |
get_context(event: HassEvent) -> HassContext
Get the context dict from the event payload.
Source code in src/hassette/event_handling/accessors.py
240 241 242 | |
get_all_changes(exclude: Sequence[str] = DEFAULT_EXCLUDE) -> Callable[[RawStateChangeEvent], dict[str, dict[str, Any]]]
Get a dict of changed state and attribute values between old and new states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exclude
|
Sequence[str]
|
List of attribute names to exclude from the changes. Defaults to fields
that change every state update (e.g., |
DEFAULT_EXCLUDE
|
Returns:
| Type | Description |
|---|---|
Callable[[RawStateChangeEvent], dict[str, dict[str, Any]]]
|
dict[str, dict[str, Any]]: A nested dict mapping names to tuples of (old_value, new_value). |
Example return value
{
"state": ("on", "off"),
"attributes": {
"rgb_color": ([255, 255, 0], [255, 255, 255]),
"brightness": (200, 255)
},
}
Source code in src/hassette/event_handling/accessors.py
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 | |
get_service(event: CallServiceEvent) -> str | FalseySentinel
Return the service name being called.
Source code in src/hassette/event_handling/accessors.py
301 302 303 | |
get_service_data(event: CallServiceEvent) -> dict[str, Any] | FalseySentinel
Return the service_data dict (or empty dict if missing).
Returns:
| Type | Description |
|---|---|
dict[str, Any] | FalseySentinel
|
dict[str, Any] | FalseySentinel: The service_data dict, or MISSING_VALUE if not present. |
Source code in src/hassette/event_handling/accessors.py
306 307 308 309 310 311 312 | |
get_service_data_key(key: str) -> Callable[[CallServiceEvent], Any]
Return an accessor that extracts a specific key from service_data.
Examples
Basic equality against a literal
ValueIs(source=get_service_data_key("entity_id"), condition="light.living_room")
Callable condition (value must be an int > 200)
ValueIs(source=get_service_data_key("brightness"), condition=lambda v: isinstance(v, int) and v > 200)
Source code in src/hassette/event_handling/accessors.py
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 | |