Skip to content

Commit 702b0d4

Browse files
bdracoptarjan
authored andcommitted
cleanup
1 parent 91305a4 commit 702b0d4

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

homeassistant/components/doorbird/device.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,23 @@ def token(self) -> str:
102102
"""Get token for device."""
103103
return self._token
104104

105+
def _get_hass_url(self) -> str:
106+
"""Get the Home Assistant URL for this device."""
107+
if custom_url := self.custom_url:
108+
return custom_url
109+
return get_url(self._hass, prefer_external=False)
110+
105111
async def async_register_events(self) -> None:
106112
"""Register events on device."""
107113
if not self.door_station_events:
108114
# The config entry might not have any events configured yet
109115
return
110-
# Override url if another is specified in the configuration
111-
if custom_url := self.custom_url:
112-
hass_url = custom_url
113-
else:
114-
# Get the URL of this server
115-
hass_url = get_url(self._hass, prefer_external=False)
116-
http_fav = await self._async_register_events(hass_url)
117-
event_config = await self._async_get_event_config(http_fav, hass_url)
116+
http_fav = await self._async_register_events()
117+
event_config = await self._async_get_event_config(http_fav)
118118
_LOGGER.debug("%s: Event config: %s", self.name, event_config)
119119
if event_config.unconfigured_favorites:
120120
await self._configure_unconfigured_favorites(event_config)
121-
event_config = await self._async_get_event_config(http_fav, hass_url)
121+
event_config = await self._async_get_event_config(http_fav)
122122
self.event_descriptions = event_config.events
123123

124124
async def _configure_unconfigured_favorites(
@@ -150,8 +150,9 @@ async def _configure_unconfigured_favorites(
150150
code,
151151
)
152152

153-
async def _async_register_events(self, hass_url: str) -> dict[str, Any]:
153+
async def _async_register_events(self) -> dict[str, Any]:
154154
"""Register events on device."""
155+
hass_url = self._get_hass_url()
155156
http_fav = await self._async_get_http_favorites()
156157
if any(
157158
# Note that a list comp is used here to ensure all
@@ -167,7 +168,7 @@ async def _async_register_events(self, hass_url: str) -> dict[str, Any]:
167168
return http_fav
168169

169170
async def _async_get_event_config(
170-
self, http_fav: dict[str, dict[str, Any]], hass_url: str
171+
self, http_fav: dict[str, dict[str, Any]]
171172
) -> DoorbirdEventConfig:
172173
"""Get events and unconfigured favorites from http favorites."""
173174
device = self.device
@@ -190,6 +191,7 @@ async def _async_get_event_config(
190191
self._get_event_name(event): event_type
191192
for event, event_type in DEFAULT_EVENT_TYPES
192193
}
194+
hass_url = self._get_hass_url()
193195
for identifier, data in http_fav.items():
194196
title: str | None = data.get("title")
195197
if not title or not title.startswith("Home Assistant"):

tests/components/doorbird/test_device.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,24 @@
33
from copy import deepcopy
44
from http import HTTPStatus
55
from typing import Any
6+
from unittest.mock import patch
67

78
from doorbirdpy import DoorBirdScheduleEntry
89
import pytest
910

10-
from homeassistant.components.doorbird.const import CONF_EVENTS
11+
from homeassistant.components.doorbird.const import (
12+
CONF_EVENTS,
13+
DEFAULT_DOORBELL_EVENT,
14+
DEFAULT_MOTION_EVENT,
15+
DOMAIN,
16+
)
1117
from homeassistant.core import HomeAssistant
1218

19+
from . import VALID_CONFIG, get_mock_doorbird_api
1320
from .conftest import DoorbirdMockerType
1421

22+
from tests.common import MockConfigEntry
23+
1524

1625
@pytest.fixture
1726
def doorbird_favorites_with_stale() -> dict[str, dict[str, Any]]:
@@ -95,6 +104,54 @@ async def test_stale_favorites_filtered_by_url(
95104
assert len(event_entities) == 2
96105

97106

107+
async def test_custom_url_used_for_favorites(
108+
hass: HomeAssistant,
109+
doorbird_info: dict[str, Any],
110+
doorbird_schedule: list[DoorBirdScheduleEntry],
111+
) -> None:
112+
"""Test that custom URL override is used instead of get_url."""
113+
custom_url = "https://my-custom-url.example.com:8443"
114+
favorites = {
115+
"http": {
116+
"1": {
117+
"title": "Home Assistant (mydoorbird_doorbell)",
118+
"value": f"{custom_url}/api/doorbird/mydoorbird_doorbell?token=test-token",
119+
},
120+
"2": {
121+
"title": "Home Assistant (mydoorbird_motion)",
122+
"value": f"{custom_url}/api/doorbird/mydoorbird_motion?token=test-token",
123+
},
124+
}
125+
}
126+
config_with_custom_url = {
127+
**VALID_CONFIG,
128+
"hass_url_override": custom_url,
129+
}
130+
entry = MockConfigEntry(
131+
domain=DOMAIN,
132+
unique_id="1CCAE3AAAAAA",
133+
data=config_with_custom_url,
134+
options={CONF_EVENTS: [DEFAULT_DOORBELL_EVENT, DEFAULT_MOTION_EVENT]},
135+
)
136+
api = get_mock_doorbird_api(
137+
info=doorbird_info,
138+
schedule=doorbird_schedule,
139+
favorites=favorites,
140+
)
141+
entry.add_to_hass(hass)
142+
# Don't patch get_url - we want to verify custom_url takes precedence
143+
with patch(
144+
"homeassistant.components.doorbird.DoorBird",
145+
return_value=api,
146+
):
147+
await hass.config_entries.async_setup(entry.entry_id)
148+
await hass.async_block_till_done()
149+
150+
# Should have 2 event entities using the custom URL
151+
event_entities = hass.states.async_all("event")
152+
assert len(event_entities) == 2
153+
154+
98155
async def test_no_configured_events(
99156
hass: HomeAssistant,
100157
doorbird_mocker: DoorbirdMockerType,

0 commit comments

Comments
 (0)