|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import asyncio |
5 | 6 | from typing import Any |
6 | 7 |
|
7 | 8 | import pytest |
@@ -318,3 +319,82 @@ async def test_timer_state_properties() -> None: |
318 | 319 | assert t.finishes_at is None |
319 | 320 | finally: |
320 | 321 | await ha.close() |
| 322 | + |
| 323 | + |
| 324 | +async def test_scene_activate(client: HAClient, fake_ha: FakeHA) -> None: |
| 325 | + sc = client.scene("romantic") |
| 326 | + await sc.activate() |
| 327 | + await sc.activate(transition=2.5) |
| 328 | + calls = fake_ha.ws_service_calls |
| 329 | + assert [c["service"] for c in calls] == ["turn_on", "turn_on"] |
| 330 | + assert "service_data" not in calls[0] or "transition" not in calls[0].get("service_data", {}) |
| 331 | + assert calls[1]["service_data"]["transition"] == 2.5 |
| 332 | + |
| 333 | + |
| 334 | +async def test_scene_state_properties() -> None: |
| 335 | + ha = HAClient("http://x", "t") |
| 336 | + try: |
| 337 | + sc = ha.scene("romantic") |
| 338 | + sc._apply_state( |
| 339 | + { |
| 340 | + "state": "2024-06-15T20:30:00+00:00", |
| 341 | + "attributes": { |
| 342 | + "friendly_name": "Romantic", |
| 343 | + "icon": "mdi:candle", |
| 344 | + "entity_id": ["light.ceiling", "light.lamp"], |
| 345 | + }, |
| 346 | + } |
| 347 | + ) |
| 348 | + assert sc.last_activated == "2024-06-15T20:30:00+00:00" |
| 349 | + assert sc.name == "Romantic" |
| 350 | + assert sc.icon == "mdi:candle" |
| 351 | + assert sc.entity_ids == ["light.ceiling", "light.lamp"] |
| 352 | + finally: |
| 353 | + await ha.close() |
| 354 | + |
| 355 | + |
| 356 | +async def test_scene_unavailable_state() -> None: |
| 357 | + ha = HAClient("http://x", "t") |
| 358 | + try: |
| 359 | + sc = ha.scene("broken") |
| 360 | + sc._apply_state({"state": "unavailable", "attributes": {}}) |
| 361 | + assert sc.last_activated is None |
| 362 | + sc._apply_state({"state": "unknown", "attributes": {}}) |
| 363 | + assert sc.last_activated is None |
| 364 | + finally: |
| 365 | + await ha.close() |
| 366 | + |
| 367 | + |
| 368 | +async def test_scene_empty_attributes() -> None: |
| 369 | + ha = HAClient("http://x", "t") |
| 370 | + try: |
| 371 | + sc = ha.scene("minimal") |
| 372 | + sc._apply_state( |
| 373 | + { |
| 374 | + "state": "2024-01-01T00:00:00+00:00", |
| 375 | + "attributes": {}, |
| 376 | + } |
| 377 | + ) |
| 378 | + assert sc.entity_ids == [] |
| 379 | + assert sc.name is None |
| 380 | + assert sc.icon is None |
| 381 | + finally: |
| 382 | + await ha.close() |
| 383 | + |
| 384 | + |
| 385 | +async def test_scene_on_activate_listener(client: HAClient, fake_ha: FakeHA) -> None: |
| 386 | + sc = client.scene("romantic") |
| 387 | + fired: list[tuple[Any, Any]] = [] |
| 388 | + |
| 389 | + @sc.on_activate |
| 390 | + def _listener(old: Any, new: Any) -> None: |
| 391 | + fired.append((old, new)) |
| 392 | + |
| 393 | + await fake_ha.push_state_changed( |
| 394 | + "scene.romantic", |
| 395 | + old_state={"state": "2024-06-15T20:00:00+00:00", "attributes": {}}, |
| 396 | + new_state={"state": "2024-06-15T20:30:00+00:00", "attributes": {}}, |
| 397 | + ) |
| 398 | + await asyncio.sleep(0.05) |
| 399 | + assert len(fired) == 1 |
| 400 | + assert fired[0][1] == "2024-06-15T20:30:00+00:00" |
0 commit comments