|
2 | 2 |
|
3 | 3 | from copy import deepcopy |
4 | 4 | from http import HTTPStatus |
| 5 | +from typing import Any |
5 | 6 |
|
6 | 7 | from doorbirdpy import DoorBirdScheduleEntry |
7 | 8 | import pytest |
8 | 9 |
|
9 | | -from homeassistant.components.doorbird.const import CONF_EVENTS |
| 10 | +from homeassistant.components.doorbird.const import ( |
| 11 | + CONF_EVENTS, |
| 12 | + DEFAULT_DOORBELL_EVENT, |
| 13 | + DEFAULT_MOTION_EVENT, |
| 14 | + DOMAIN, |
| 15 | +) |
10 | 16 | from homeassistant.core import HomeAssistant |
11 | 17 |
|
| 18 | +from . import VALID_CONFIG |
12 | 19 | from .conftest import DoorbirdMockerType |
13 | 20 |
|
| 21 | +from tests.common import MockConfigEntry |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def doorbird_favorites_with_stale() -> dict[str, dict[str, Any]]: |
| 26 | + """Return favorites fixture with stale favorites from another HA instance. |
| 27 | +
|
| 28 | + Creates favorites where identifier "2" has the same event name as "0" |
| 29 | + (mydoorbird_doorbell) but points to a different HA instance URL. |
| 30 | + These stale favorites should be filtered out. |
| 31 | + """ |
| 32 | + return { |
| 33 | + "http": { |
| 34 | + "0": { |
| 35 | + "title": "Home Assistant (mydoorbird_doorbell)", |
| 36 | + "value": "http://127.0.0.1:8123/api/doorbird/mydoorbird_doorbell?token=test-token", |
| 37 | + }, |
| 38 | + # Stale favorite from a different HA instance - should be filtered out |
| 39 | + "2": { |
| 40 | + "title": "Home Assistant (mydoorbird_doorbell)", |
| 41 | + "value": "http://old-ha-instance:8123/api/doorbird/mydoorbird_doorbell?token=old-token", |
| 42 | + }, |
| 43 | + "5": { |
| 44 | + "title": "Home Assistant (mydoorbird_motion)", |
| 45 | + "value": "http://127.0.0.1:8123/api/doorbird/mydoorbird_motion?token=test-token", |
| 46 | + }, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +def doorbird_schedule_with_stale() -> list[DoorBirdScheduleEntry]: |
| 53 | + """Return schedule fixture with outputs referencing stale favorites. |
| 54 | +
|
| 55 | + Both param "0" and "2" map to doorbell input, but "2" is a stale favorite. |
| 56 | + """ |
| 57 | + schedule_data = [ |
| 58 | + { |
| 59 | + "input": "doorbell", |
| 60 | + "param": "1", |
| 61 | + "output": [ |
| 62 | + { |
| 63 | + "event": "http", |
| 64 | + "param": "0", |
| 65 | + "schedule": {"weekdays": [{"to": "107999", "from": "108000"}]}, |
| 66 | + }, |
| 67 | + { |
| 68 | + "event": "http", |
| 69 | + "param": "2", |
| 70 | + "schedule": {"weekdays": [{"to": "107999", "from": "108000"}]}, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + { |
| 75 | + "input": "motion", |
| 76 | + "param": "", |
| 77 | + "output": [ |
| 78 | + { |
| 79 | + "event": "http", |
| 80 | + "param": "5", |
| 81 | + "schedule": {"weekdays": [{"to": "107999", "from": "108000"}]}, |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + ] |
| 86 | + return DoorBirdScheduleEntry.parse_all(schedule_data) |
| 87 | + |
| 88 | + |
| 89 | +async def test_stale_favorites_filtered_by_url( |
| 90 | + hass: HomeAssistant, |
| 91 | + doorbird_mocker: DoorbirdMockerType, |
| 92 | + doorbird_favorites_with_stale: dict[str, dict[str, Any]], |
| 93 | + doorbird_schedule_with_stale: list[DoorBirdScheduleEntry], |
| 94 | +) -> None: |
| 95 | + """Test that stale favorites from other HA instances are filtered out.""" |
| 96 | + await doorbird_mocker( |
| 97 | + favorites=doorbird_favorites_with_stale, |
| 98 | + schedule=doorbird_schedule_with_stale, |
| 99 | + ) |
| 100 | + # Should have 2 event entities - stale favorite "2" is filtered out |
| 101 | + # because its URL doesn't match the current HA instance |
| 102 | + event_entities = hass.states.async_all("event") |
| 103 | + assert len(event_entities) == 2 |
| 104 | + |
| 105 | + |
| 106 | +async def test_custom_url_used_for_favorites( |
| 107 | + hass: HomeAssistant, |
| 108 | + doorbird_mocker: DoorbirdMockerType, |
| 109 | +) -> None: |
| 110 | + """Test that custom URL override is used instead of get_url.""" |
| 111 | + custom_url = "https://my-custom-url.example.com:8443" |
| 112 | + favorites = { |
| 113 | + "http": { |
| 114 | + "1": { |
| 115 | + "title": "Home Assistant (mydoorbird_doorbell)", |
| 116 | + "value": f"{custom_url}/api/doorbird/mydoorbird_doorbell?token=test-token", |
| 117 | + }, |
| 118 | + "2": { |
| 119 | + "title": "Home Assistant (mydoorbird_motion)", |
| 120 | + "value": f"{custom_url}/api/doorbird/mydoorbird_motion?token=test-token", |
| 121 | + }, |
| 122 | + } |
| 123 | + } |
| 124 | + config_with_custom_url = { |
| 125 | + **VALID_CONFIG, |
| 126 | + "hass_url_override": custom_url, |
| 127 | + } |
| 128 | + entry = MockConfigEntry( |
| 129 | + domain=DOMAIN, |
| 130 | + unique_id="1CCAE3AAAAAA", |
| 131 | + data=config_with_custom_url, |
| 132 | + options={CONF_EVENTS: [DEFAULT_DOORBELL_EVENT, DEFAULT_MOTION_EVENT]}, |
| 133 | + ) |
| 134 | + await doorbird_mocker(entry=entry, favorites=favorites) |
| 135 | + |
| 136 | + # Should have 2 event entities using the custom URL |
| 137 | + event_entities = hass.states.async_all("event") |
| 138 | + assert len(event_entities) == 2 |
| 139 | + |
14 | 140 |
|
15 | 141 | async def test_no_configured_events( |
16 | 142 | hass: HomeAssistant, |
|
0 commit comments