Skip to content

Commit 0990cef

Browse files
Diegorro98frenck
authored andcommitted
Add Home Connect resume command button when an appliance is paused (home-assistant#148512)
1 parent 962ad99 commit 0990cef

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

homeassistant/components/home_connect/coordinator.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@
4141
from homeassistant.helpers import device_registry as dr, issue_registry as ir
4242
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
4343

44-
from .const import API_DEFAULT_RETRY_AFTER, APPLIANCES_WITH_PROGRAMS, DOMAIN
44+
from .const import (
45+
API_DEFAULT_RETRY_AFTER,
46+
APPLIANCES_WITH_PROGRAMS,
47+
BSH_OPERATION_STATE_PAUSE,
48+
DOMAIN,
49+
)
4550
from .utils import get_dict_from_home_connect_error
4651

4752
_LOGGER = logging.getLogger(__name__)
@@ -66,6 +71,7 @@ class HomeConnectApplianceData:
6671

6772
def update(self, other: HomeConnectApplianceData) -> None:
6873
"""Update data with data from other instance."""
74+
self.commands.clear()
6975
self.commands.update(other.commands)
7076
self.events.update(other.events)
7177
self.info.connected = other.info.connected
@@ -201,6 +207,28 @@ async def _event_listener(self) -> None: # noqa: C901
201207
raw_key=status_key.value,
202208
value=event.value,
203209
)
210+
if (
211+
status_key == StatusKey.BSH_COMMON_OPERATION_STATE
212+
and event.value == BSH_OPERATION_STATE_PAUSE
213+
and CommandKey.BSH_COMMON_RESUME_PROGRAM
214+
not in (
215+
commands := self.data[
216+
event_message_ha_id
217+
].commands
218+
)
219+
):
220+
# All the appliances that can be paused
221+
# should have the resume command available.
222+
commands.add(CommandKey.BSH_COMMON_RESUME_PROGRAM)
223+
for (
224+
listener,
225+
context,
226+
) in self._special_listeners.values():
227+
if (
228+
EventKey.BSH_COMMON_APPLIANCE_DEPAIRED
229+
not in context
230+
):
231+
listener()
204232
self._call_event_listener(event_message)
205233

206234
case EventType.NOTIFY:

tests/components/home_connect/test_button.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"""Tests for home_connect button entities."""
22

33
from collections.abc import Awaitable, Callable
4-
from typing import Any
4+
from typing import Any, cast
55
from unittest.mock import AsyncMock, MagicMock
66

77
from aiohomeconnect.model import (
88
ArrayOfCommands,
99
CommandKey,
10+
Event,
11+
EventKey,
1012
EventMessage,
1113
HomeAppliance,
1214
)
@@ -317,3 +319,62 @@ async def test_stop_program_button_exception(
317319
{ATTR_ENTITY_ID: entity_id},
318320
blocking=True,
319321
)
322+
323+
324+
@pytest.mark.parametrize("appliance", ["Washer"], indirect=True)
325+
async def test_enable_resume_command_on_pause(
326+
hass: HomeAssistant,
327+
client: MagicMock,
328+
config_entry: MockConfigEntry,
329+
integration_setup: Callable[[MagicMock], Awaitable[bool]],
330+
appliance: HomeAppliance,
331+
) -> None:
332+
"""Test if all commands enabled option works as expected."""
333+
entity_id = "button.washer_resume_program"
334+
335+
original_get_available_commands = client.get_available_commands
336+
337+
async def get_available_commands_side_effect(ha_id: str) -> ArrayOfCommands:
338+
array_of_commands = cast(
339+
ArrayOfCommands, await original_get_available_commands(ha_id)
340+
)
341+
if ha_id == appliance.ha_id:
342+
for command in array_of_commands.commands:
343+
if command.key == CommandKey.BSH_COMMON_RESUME_PROGRAM:
344+
# Simulate that the resume command is not available initially
345+
array_of_commands.commands.remove(command)
346+
break
347+
return array_of_commands
348+
349+
client.get_available_commands = AsyncMock(
350+
side_effect=get_available_commands_side_effect
351+
)
352+
353+
assert await integration_setup(client)
354+
assert config_entry.state is ConfigEntryState.LOADED
355+
356+
assert not hass.states.get(entity_id)
357+
358+
await client.add_events(
359+
[
360+
EventMessage(
361+
appliance.ha_id,
362+
EventType.STATUS,
363+
data=ArrayOfEvents(
364+
[
365+
Event(
366+
key=EventKey.BSH_COMMON_STATUS_OPERATION_STATE,
367+
raw_key=EventKey.BSH_COMMON_STATUS_OPERATION_STATE.value,
368+
timestamp=0,
369+
level="",
370+
handling="",
371+
value="BSH.Common.EnumType.OperationState.Pause",
372+
)
373+
]
374+
),
375+
)
376+
]
377+
)
378+
await hass.async_block_till_done()
379+
380+
assert hass.states.get(entity_id)

0 commit comments

Comments
 (0)