Commit ddf09c0
authored
refactor(models)!: clarify States value accessors and widen accessor typing (#2108)
## Summary
Two related, breaking v2 ergonomics fixes to the device accessor
surface, before the 2.0 freeze.
### #2105 — `States.has()`/`has_any()` conflate existence with value
`has()` returned `True` only when the state existed **and** had a
non-`None` value, which pushed call sites toward a double lookup
(`has(x)` then `get_value(x)`). The two concepts are now separated:
- `has()` → **`has_value()`**, `has_any()` → **`has_any_value()`** —
explicit about the "present AND non-None value" semantic.
- Pure existence is `name in states` (already provided by
`Mapping.__contains__`).
Many consumers don't even need `has_value` anymore: `get_value(x) == ON`
already returns `False` for a missing state, collapsing the double
lookup to one call.
### #2106 — Inconsistent accessor typing
State accessors were typed `str`-only even though
`OverkizState`/`OverkizAttribute` are `StrEnum` and worked at runtime.
Signatures now advertise enum support, matching the command helpers:
- `States.{get_value, first, first_value, has_value, has_any_value}` →
`str | OverkizState | OverkizAttribute` (the `States` class backs both
`device.states` and `device.attributes`).
- `StateDefinitions.{first, has_any}` → `str | OverkizState`.
- `CommandDefinitions` and `device.supports_command()` already accepted
enums — unchanged.
Scope deliberately excludes new device-level wrappers (e.g.
`device.has_state()`): `device.states` is a first-class container read
constantly in consumers, while `device.supports_command()` only exists
as a shortcut to the buried `device.definition.commands`. Mirroring
would duplicate the container API for no gain.
## Changes
- `pyoverkiz/models.py`: rename + widen typing (new `StateName` alias).
- `docs/device-control.md`, `docs/migration-v2.md`: updated to new
names; document `in` for pure existence.
- `tests/test_models.py`: renamed tests + new test exercising enum keys.
## Breaking changes
- `States.has()` → `States.has_value()`
- `States.has_any()` → `States.has_any_value()`
## Test plan
- [x] `ruff check` / `ruff format` / `mypy` / `ty` clean
- [x] Full suite green (502 passed)
Closes #2105
Closes #21061 parent ac8e458 commit ddf09c0
4 files changed
Lines changed: 84 additions & 37 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
57 | | - | |
| 56 | + | |
| 57 | + | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
61 | | - | |
| 60 | + | |
| 61 | + | |
62 | 62 | | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
63 | 67 | | |
64 | 68 | | |
65 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
191 | | - | |
192 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
193 | 194 | | |
194 | 195 | | |
195 | 196 | | |
| |||
198 | 199 | | |
199 | 200 | | |
200 | 201 | | |
201 | | - | |
202 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
203 | 208 | | |
204 | 209 | | |
205 | 210 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
132 | 133 | | |
133 | 134 | | |
134 | 135 | | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
135 | 142 | | |
136 | 143 | | |
137 | 144 | | |
| |||
176 | 183 | | |
177 | 184 | | |
178 | 185 | | |
179 | | - | |
| 186 | + | |
180 | 187 | | |
181 | 188 | | |
182 | 189 | | |
183 | 190 | | |
184 | 191 | | |
185 | 192 | | |
186 | | - | |
| 193 | + | |
187 | 194 | | |
188 | 195 | | |
189 | 196 | | |
190 | 197 | | |
191 | 198 | | |
192 | 199 | | |
193 | 200 | | |
194 | | - | |
| 201 | + | |
195 | 202 | | |
196 | 203 | | |
197 | 204 | | |
198 | 205 | | |
199 | 206 | | |
200 | | - | |
201 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
202 | 212 | | |
203 | 213 | | |
204 | 214 | | |
205 | | - | |
206 | | - | |
| 215 | + | |
| 216 | + | |
207 | 217 | | |
208 | 218 | | |
209 | 219 | | |
| |||
317 | 327 | | |
318 | 328 | | |
319 | 329 | | |
320 | | - | |
| 330 | + | |
321 | 331 | | |
322 | 332 | | |
323 | 333 | | |
324 | 334 | | |
325 | 335 | | |
326 | 336 | | |
327 | | - | |
| 337 | + | |
328 | 338 | | |
329 | 339 | | |
330 | 340 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
348 | 349 | | |
349 | 350 | | |
350 | 351 | | |
351 | | - | |
352 | | - | |
| 352 | + | |
| 353 | + | |
353 | 354 | | |
354 | | - | |
355 | | - | |
| 355 | + | |
| 356 | + | |
356 | 357 | | |
357 | | - | |
358 | | - | |
| 358 | + | |
| 359 | + | |
359 | 360 | | |
360 | | - | |
361 | | - | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
362 | 390 | | |
363 | 391 | | |
364 | 392 | | |
| |||
500 | 528 | | |
501 | 529 | | |
502 | 530 | | |
503 | | - | |
504 | | - | |
| 531 | + | |
| 532 | + | |
505 | 533 | | |
506 | | - | |
| 534 | + | |
507 | 535 | | |
508 | | - | |
509 | | - | |
| 536 | + | |
| 537 | + | |
510 | 538 | | |
511 | | - | |
| 539 | + | |
512 | 540 | | |
513 | | - | |
514 | | - | |
| 541 | + | |
| 542 | + | |
515 | 543 | | |
516 | | - | |
| 544 | + | |
517 | 545 | | |
518 | | - | |
519 | | - | |
| 546 | + | |
| 547 | + | |
520 | 548 | | |
521 | | - | |
| 549 | + | |
522 | 550 | | |
523 | 551 | | |
524 | 552 | | |
| |||
0 commit comments