mirror of
https://github.com/home-assistant/core.git
synced 2025-11-13 13:00:11 +00:00
Add and delete Home Connect devices on CONNECTED/PAIRED and DEPAIRED events (#136952)
* Add and delete devices on CONNECT/PAIRED and DEPAIRED events * Simplify device depairing * small fixes Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Add always the devices * kind of revert changes to simplify the entity fetch and removing on connected/paired and depaired * cache `ha_id` * Fix typo * Remove unnecessary device info at HomeConnectEntity * Move common code of each platform to `common.py` * Added docstring to clarify usage * Apply suggestions Co-authored-by: Martin Hjelmare <marhje52@gmail.com> --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
committed by
GitHub
parent
147b5f549f
commit
30314ca32b
@@ -18,8 +18,11 @@ from aiohomeconnect.model import (
|
||||
EventKey,
|
||||
EventMessage,
|
||||
EventType,
|
||||
GetSetting,
|
||||
HomeAppliance,
|
||||
Option,
|
||||
Program,
|
||||
SettingKey,
|
||||
)
|
||||
from aiohomeconnect.model.error import HomeConnectApiError, HomeConnectError
|
||||
from aiohomeconnect.model.program import EnumerateProgram
|
||||
@@ -145,6 +148,14 @@ async def mock_integration_setup(
|
||||
return run
|
||||
|
||||
|
||||
def _get_specific_appliance_side_effect(ha_id: str) -> HomeAppliance:
|
||||
"""Get specific appliance side effect."""
|
||||
for appliance in copy.deepcopy(MOCK_APPLIANCES).homeappliances:
|
||||
if appliance.ha_id == ha_id:
|
||||
return appliance
|
||||
raise HomeConnectApiError("error.key", "error description")
|
||||
|
||||
|
||||
def _get_set_program_side_effect(
|
||||
event_queue: asyncio.Queue[list[EventMessage]], event_key: EventKey
|
||||
):
|
||||
@@ -253,6 +264,24 @@ async def _get_settings_side_effect(ha_id: str) -> ArrayOfSettings:
|
||||
)
|
||||
|
||||
|
||||
async def _get_setting_side_effect(ha_id: str, setting_key: SettingKey):
|
||||
"""Get setting."""
|
||||
for appliance in MOCK_APPLIANCES.homeappliances:
|
||||
if appliance.ha_id == ha_id:
|
||||
settings = MOCK_SETTINGS.get(
|
||||
next(
|
||||
appliance
|
||||
for appliance in MOCK_APPLIANCES.homeappliances
|
||||
if appliance.ha_id == ha_id
|
||||
).type,
|
||||
{},
|
||||
).get("data", {"settings": []})
|
||||
for setting_dict in cast(list[dict], settings["settings"]):
|
||||
if setting_dict["key"] == setting_key:
|
||||
return GetSetting.from_dict(setting_dict)
|
||||
raise HomeConnectApiError("error.key", "error description")
|
||||
|
||||
|
||||
@pytest.fixture(name="client")
|
||||
def mock_client(request: pytest.FixtureRequest) -> MagicMock:
|
||||
"""Fixture to mock Client from HomeConnect."""
|
||||
@@ -274,7 +303,10 @@ def mock_client(request: pytest.FixtureRequest) -> MagicMock:
|
||||
for event in await event_queue.get():
|
||||
yield event
|
||||
|
||||
mock.get_home_appliances = AsyncMock(return_value=MOCK_APPLIANCES)
|
||||
mock.get_home_appliances = AsyncMock(return_value=copy.deepcopy(MOCK_APPLIANCES))
|
||||
mock.get_specific_appliance = AsyncMock(
|
||||
side_effect=_get_specific_appliance_side_effect
|
||||
)
|
||||
mock.stream_all_events = stream_all_events
|
||||
mock.start_program = AsyncMock(
|
||||
side_effect=_get_set_program_side_effect(
|
||||
@@ -296,6 +328,7 @@ def mock_client(request: pytest.FixtureRequest) -> MagicMock:
|
||||
side_effect=_get_set_key_value_side_effect(event_queue, "setting_key"),
|
||||
)
|
||||
mock.get_settings = AsyncMock(side_effect=_get_settings_side_effect)
|
||||
mock.get_setting = AsyncMock(side_effect=_get_setting_side_effect)
|
||||
mock.get_status = AsyncMock(return_value=copy.deepcopy(MOCK_STATUS))
|
||||
mock.get_all_programs = AsyncMock(side_effect=_get_all_programs_side_effect)
|
||||
mock.put_command = AsyncMock()
|
||||
@@ -323,7 +356,7 @@ def mock_client_with_exception(request: pytest.FixtureRequest) -> MagicMock:
|
||||
for event in await event_queue.get():
|
||||
yield event
|
||||
|
||||
mock.get_home_appliances = AsyncMock(return_value=MOCK_APPLIANCES)
|
||||
mock.get_home_appliances = AsyncMock(return_value=copy.deepcopy(MOCK_APPLIANCES))
|
||||
mock.stream_all_events = stream_all_events
|
||||
|
||||
mock.start_program = AsyncMock(side_effect=exception)
|
||||
|
||||
Reference in New Issue
Block a user