Skip to content

Commit 298b9b9

Browse files
zxdavbjoostlekCopilotMartinHjelmare
authored
Use fixtures for all entity IDs in Evohome tests (#167479)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
1 parent f6fb6f4 commit 298b9b9

2 files changed

Lines changed: 53 additions & 25 deletions

File tree

tests/components/evohome/conftest.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import Any
99
from unittest.mock import MagicMock, patch
1010

11-
from evohomeasync2 import EvohomeClient
11+
from evohomeasync2 import EvohomeClient, HotWater
1212
from evohomeasync2.auth import AbstractTokenManager, Auth
1313
from evohomeasync2.control_system import ControlSystem
1414
from evohomeasync2.zone import Zone
@@ -18,8 +18,9 @@
1818
from homeassistant.components.evohome.const import DOMAIN
1919
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
2020
from homeassistant.core import HomeAssistant
21+
from homeassistant.helpers import entity_registry as er
2122
from homeassistant.setup import async_setup_component
22-
from homeassistant.util import dt as dt_util, slugify
23+
from homeassistant.util import dt as dt_util
2324
from homeassistant.util.json import JsonArrayType, JsonObjectType
2425

2526
from .const import ACCESS_TOKEN, REFRESH_TOKEN, SESSION_ID, USERNAME
@@ -210,20 +211,52 @@ async def evohome(
210211

211212

212213
@pytest.fixture
213-
def ctl_id(evohome: MagicMock) -> str:
214-
"""Return the entity_id of the evohome integration's controller."""
214+
def ctl_id(evohome: MagicMock, entity_id: Callable[[Platform, str], str]) -> str:
215+
"""Return the entity_id of evohome's controller (a Climate entity)."""
215216

216217
evo: EvohomeClient = evohome.return_value
217-
ctl: ControlSystem = evo.tcs
218+
tcs: ControlSystem = evo.tcs
218219

219-
return f"{Platform.CLIMATE}.{slugify(ctl.location.name)}"
220+
return entity_id(Platform.CLIMATE, tcs.id)
220221

221222

222223
@pytest.fixture
223-
def zone_id(evohome: MagicMock) -> str:
224-
"""Return the entity_id of the evohome integration's first zone."""
224+
def zone_id(evohome: MagicMock, entity_id: Callable[[Platform, str], str]) -> str:
225+
"""Return the entity_id of evohome's first zone (a Climate entity)."""
225226

226227
evo: EvohomeClient = evohome.return_value
228+
ctl: ControlSystem = evo.tcs
229+
227230
zone: Zone = evo.tcs.zones[0]
228231

229-
return f"{Platform.CLIMATE}.{slugify(zone.name)}"
232+
return entity_id(Platform.CLIMATE, f"{zone.id}z" if zone.id == ctl.id else zone.id)
233+
234+
235+
@pytest.fixture
236+
def dhw_id(evohome: MagicMock, entity_id: Callable[[Platform, str], str]) -> str:
237+
"""Return the entity_id of Evohome's DHW controller (a WaterHeater entity)."""
238+
239+
evo: EvohomeClient = evohome.return_value
240+
dhw: HotWater | None = evo.tcs.hotwater
241+
242+
assert dhw is not None, "Fixture has no DHW zone"
243+
244+
return entity_id(Platform.WATER_HEATER, dhw.id)
245+
246+
247+
@pytest.fixture
248+
def entity_id(
249+
entity_registry: er.EntityRegistry,
250+
) -> Callable[[Platform, str], str]:
251+
"""Return a helper to lookup an entity_id from platform and unique_id."""
252+
253+
def get_entity_id(platform: Platform, unique_id: str) -> str:
254+
"""Return an entity_id from the entity registry."""
255+
256+
entity = entity_registry.async_get_entity_id(platform, DOMAIN, unique_id)
257+
assert entity is not None, (
258+
f"Entity not found for platform={platform}: {unique_id}"
259+
)
260+
return entity
261+
262+
return get_entity_id

tests/components/evohome/test_water_heater.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
from .conftest import setup_evohome
2525
from .const import TEST_INSTALLS_WITH_DHW
2626

27-
DHW_ENTITY_ID = "water_heater.domestic_hot_water"
28-
2927

3028
@pytest.mark.parametrize("install", TEST_INSTALLS_WITH_DHW)
3129
async def test_setup_platform(
@@ -49,9 +47,9 @@ async def test_setup_platform(
4947

5048

5149
@pytest.mark.parametrize("install", TEST_INSTALLS_WITH_DHW)
52-
@pytest.mark.usefixtures("evohome")
5350
async def test_set_operation_mode(
5451
hass: HomeAssistant,
52+
dhw_id: str,
5553
freezer: FrozenDateTimeFactory,
5654
snapshot: SnapshotAssertion,
5755
) -> None:
@@ -66,7 +64,7 @@ async def test_set_operation_mode(
6664
WATER_HEATER_DOMAIN,
6765
SERVICE_SET_OPERATION_MODE,
6866
{
69-
ATTR_ENTITY_ID: DHW_ENTITY_ID,
67+
ATTR_ENTITY_ID: dhw_id,
7068
ATTR_OPERATION_MODE: "auto",
7169
},
7270
blocking=True,
@@ -80,7 +78,7 @@ async def test_set_operation_mode(
8078
WATER_HEATER_DOMAIN,
8179
SERVICE_SET_OPERATION_MODE,
8280
{
83-
ATTR_ENTITY_ID: DHW_ENTITY_ID,
81+
ATTR_ENTITY_ID: dhw_id,
8482
ATTR_OPERATION_MODE: "off",
8583
},
8684
blocking=True,
@@ -100,7 +98,7 @@ async def test_set_operation_mode(
10098
WATER_HEATER_DOMAIN,
10199
SERVICE_SET_OPERATION_MODE,
102100
{
103-
ATTR_ENTITY_ID: DHW_ENTITY_ID,
101+
ATTR_ENTITY_ID: dhw_id,
104102
ATTR_OPERATION_MODE: "on",
105103
},
106104
blocking=True,
@@ -118,8 +116,7 @@ async def test_set_operation_mode(
118116

119117

120118
@pytest.mark.parametrize("install", TEST_INSTALLS_WITH_DHW)
121-
@pytest.mark.usefixtures("evohome")
122-
async def test_set_away_mode(hass: HomeAssistant) -> None:
119+
async def test_set_away_mode(hass: HomeAssistant, dhw_id: str) -> None:
123120
"""Test SERVICE_SET_AWAY_MODE of an evohome DHW zone."""
124121

125122
# set_away_mode: off
@@ -128,7 +125,7 @@ async def test_set_away_mode(hass: HomeAssistant) -> None:
128125
WATER_HEATER_DOMAIN,
129126
SERVICE_SET_AWAY_MODE,
130127
{
131-
ATTR_ENTITY_ID: DHW_ENTITY_ID,
128+
ATTR_ENTITY_ID: dhw_id,
132129
ATTR_AWAY_MODE: "off",
133130
},
134131
blocking=True,
@@ -142,7 +139,7 @@ async def test_set_away_mode(hass: HomeAssistant) -> None:
142139
WATER_HEATER_DOMAIN,
143140
SERVICE_SET_AWAY_MODE,
144141
{
145-
ATTR_ENTITY_ID: DHW_ENTITY_ID,
142+
ATTR_ENTITY_ID: dhw_id,
146143
ATTR_AWAY_MODE: "on",
147144
},
148145
blocking=True,
@@ -152,8 +149,7 @@ async def test_set_away_mode(hass: HomeAssistant) -> None:
152149

153150

154151
@pytest.mark.parametrize("install", TEST_INSTALLS_WITH_DHW)
155-
@pytest.mark.usefixtures("evohome")
156-
async def test_turn_off(hass: HomeAssistant) -> None:
152+
async def test_turn_off(hass: HomeAssistant, dhw_id: str) -> None:
157153
"""Test SERVICE_TURN_OFF of an evohome DHW zone."""
158154

159155
# turn_off
@@ -162,7 +158,7 @@ async def test_turn_off(hass: HomeAssistant) -> None:
162158
WATER_HEATER_DOMAIN,
163159
SERVICE_TURN_OFF,
164160
{
165-
ATTR_ENTITY_ID: DHW_ENTITY_ID,
161+
ATTR_ENTITY_ID: dhw_id,
166162
},
167163
blocking=True,
168164
)
@@ -171,8 +167,7 @@ async def test_turn_off(hass: HomeAssistant) -> None:
171167

172168

173169
@pytest.mark.parametrize("install", TEST_INSTALLS_WITH_DHW)
174-
@pytest.mark.usefixtures("evohome")
175-
async def test_turn_on(hass: HomeAssistant) -> None:
170+
async def test_turn_on(hass: HomeAssistant, dhw_id: str) -> None:
176171
"""Test SERVICE_TURN_ON of an evohome DHW zone."""
177172

178173
# turn_on
@@ -181,7 +176,7 @@ async def test_turn_on(hass: HomeAssistant) -> None:
181176
WATER_HEATER_DOMAIN,
182177
SERVICE_TURN_ON,
183178
{
184-
ATTR_ENTITY_ID: DHW_ENTITY_ID,
179+
ATTR_ENTITY_ID: dhw_id,
185180
},
186181
blocking=True,
187182
)

0 commit comments

Comments
 (0)