Predicates
Predicates combine accessors and conditions to form reusable boolean functions.
A predicate takes a source callable that extracts a value from an event, and a condition that
tests the extracted value. The condition may be a literal value, a callable, or a more complex condition object.
Conditions can be composed of other conditions to form complex logic.
Examples:
Basic value comparison
ValueIs(source=get_entity_id, condition="light.kitchen")
With a callable condition
def is_kitchen_light(entity_id: str) -> bool:
return entity_id == "light.kitchen"
ValueIs(source=get_entity_id, condition=is_kitchen_light)
With a condition object
ValueIs(
source=get_entity_id,
condition=C.IsIn(collection=["light.kitchen", "light.living"]),
)
Combining multiple predicates
P.AllOf(predicates=[
P.DomainMatches("light"),
P.EntityMatches("light.kitchen"),
P.StateTo("on"),
])
Guard
dataclass
Wraps a predicate function to be used in combinators.
Allows for passing any callable as a predicate. Generic over EventT to allow type checkers to understand the expected event type.
Source code in src/hassette/event_handling/predicates.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
AllOf
dataclass
Predicate that evaluates to True if all of the contained predicates evaluate to True.
Source code in src/hassette/event_handling/predicates.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
predicates: tuple[Predicate, ...]
instance-attribute
The predicates to evaluate.
AnyOf
dataclass
Predicate that evaluates to True if any of the contained predicates evaluate to True.
Source code in src/hassette/event_handling/predicates.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
predicates: tuple[Predicate, ...]
instance-attribute
The predicates to evaluate.
Not
dataclass
Negates the result of the predicate.
Source code in src/hassette/event_handling/predicates.py
198 199 200 201 202 203 204 205 206 207 208 | |
ValueIs
dataclass
Checks whether a value extracted from an event satisfies a condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Callable[[EventT], V]
|
Callable that extracts the value to compare from the event. |
required |
condition
|
ChangeType
|
A literal or callable tested against the extracted value. If ANY_VALUE, always True. |
ANY_VALUE
|
Source code in src/hassette/event_handling/predicates.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
DidChange
dataclass
Predicate that is True when two extracted values differ.
Typical use is an accessor that returns (old_value, new_value).
Source code in src/hassette/event_handling/predicates.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
IsPresent
dataclass
Checks if a value extracted from an event is present (not MISSING_VALUE).
This will generally be used when comparing state changes, where either the old or new state may be missing.
Source code in src/hassette/event_handling/predicates.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
IsMissing
dataclass
Checks if a value extracted from an event is missing (MISSING_VALUE).
This will generally be used when comparing state changes, where either the old or new state may be missing.
Source code in src/hassette/event_handling/predicates.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
StateFrom
dataclass
Checks if a value extracted from a RawStateChangeEvent satisfies a condition on the 'old' value.
Source code in src/hassette/event_handling/predicates.py
287 288 289 290 291 292 293 294 295 296 297 | |
StateTo
dataclass
Checks if a value extracted from a RawStateChangeEvent satisfies a condition on the 'new' value.
Source code in src/hassette/event_handling/predicates.py
300 301 302 303 304 305 306 307 308 309 310 | |
StateComparison
dataclass
Checks if a comparison between from_state and to_state satisfies a condition.
Source code in src/hassette/event_handling/predicates.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
AttrFrom
dataclass
Checks if a specific attribute changed in a RawStateChangeEvent.
Source code in src/hassette/event_handling/predicates.py
331 332 333 334 335 336 337 338 339 340 341 342 | |
AttrTo
dataclass
Checks if a specific attribute changed in a RawStateChangeEvent.
Source code in src/hassette/event_handling/predicates.py
345 346 347 348 349 350 351 352 353 354 355 356 | |
AttrComparison
dataclass
Checks if a comparison between from_attr and to_attr satisfies a condition.
Source code in src/hassette/event_handling/predicates.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
StateDidChange
dataclass
Checks if the state changed in a RawStateChangeEvent.
Source code in src/hassette/event_handling/predicates.py
380 381 382 383 384 385 386 387 388 | |
AttrDidChange
dataclass
Checks if a specific attribute changed in a RawStateChangeEvent.
When old_state is None, the attribute is treated as having changed
if it is present on new_state. This applies to synthetic
immediate-fire/bootstrap events, the cancel handler's old_state stripping
in DurationTimer, and real state-change events where there is no prior
state yet (first observation / first-time state set).
Source code in src/hassette/event_handling/predicates.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
DomainMatches
dataclass
Checks if the event domain matches a specific value.
Source code in src/hassette/event_handling/predicates.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
EntityMatches
dataclass
Checks if the event entity_id matches a specific value.
Source code in src/hassette/event_handling/predicates.py
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
ServiceMatches
dataclass
Checks if the event service matches a specific value.
Source code in src/hassette/event_handling/predicates.py
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
ServiceDataWhere
dataclass
Predicate that applies a mapping of service_data conditions to a CallServiceEvent.
Examples
Exact matches only
ServiceDataWhere({"entity_id": "light.kitchen", "transition": 1})
With a callable condition
ServiceDataWhere({"brightness": lambda v: isinstance(v, int) and v >= 150})
With globs (auto-wrapped)
ServiceDataWhere({"entity_id": "light.*"})
Using conditions
ServiceDataWhere({"entity_id": Glob("switch.*")})
ServiceDataWhere({"brightness": IsIn([100, 200, 255])})
Source code in src/hassette/event_handling/predicates.py
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 529 530 531 532 533 534 535 536 | |
from_kwargs(*, auto_glob: bool = True, **spec: ChangeType) -> ServiceDataWhere
classmethod
Ergonomic constructor for literal kwargs.
Example
ServiceDataWhere.from_kwargs(entity_id="light.*", brightness=200)
Source code in src/hassette/event_handling/predicates.py
528 529 530 531 532 533 534 535 536 | |
summarize_top_level(predicate: Predicate) -> str
Return a human-readable summary suitable for display as a top-level label.
Calls summarize() on the predicate, then strips balanced outer
parentheses so top-level combinators don't produce redundant wrapping.
Source code in src/hassette/event_handling/predicates.py
147 148 149 150 151 152 153 | |
compare_value(actual: Any, condition: ChangeType) -> bool
Compare an actual value against a condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actual
|
Any
|
The actual value to compare. |
required |
condition
|
ChangeType
|
The condition to compare against. Can be a literal value or a callable. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the actual value matches the condition, False otherwise. |
Behavior
- If condition is NOT_PROVIDED, treat as 'no constraint' (True).
- If condition is a non-callable, compare for equality only.
- If condition is a callable, call and ensure bool.
- Async/coroutine predicates are explicitly disallowed (raise).
Note
This function does not handle collections any differently than other literals — it compares them for equality only. Use specific conditions like IsIn/NotIn/Intersects for collection membership tests.
Source code in src/hassette/event_handling/predicates.py
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | |
ensure_tuple(where: Predicate | Sequence[Predicate]) -> tuple[Predicate, ...]
Ensure the 'where' is a flat tuple of predicates, flattening only predicate collections.
Recurses into list/tuple/set/frozenset; leaves Mapping, strings/bytes, and callables intact.
Source code in src/hassette/event_handling/predicates.py
583 584 585 586 587 588 589 590 591 592 593 594 595 | |
is_predicate_collection(obj: Any) -> TypeGuard[Sequence[Predicate]]
Return True for predicate collections we want to recurse into.
We treat only list/tuple/set/frozenset-like things as collections of predicates. We explicitly DO NOT recurse into: - mappings (those feed ServiceDataWhere elsewhere), - strings/bytes, - callables (predicates are callables; don't explode them), - None.
Source code in src/hassette/event_handling/predicates.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | |
normalize_where(where: Predicate | Sequence[Predicate] | None) -> Predicate | None
Normalize a 'where' clause into a single Predicate (usually AllOf.ensure_iterable), or None.
- If where is None → None
- If where is a predicate collection (list/tuple/set/...) → AllOf.ensure_iterable(where)
- Otherwise (single predicate or mapping handled elsewhere) → where
Source code in src/hassette/event_handling/predicates.py
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | |