Change update after button press for lamarzocco (#129616)

This commit is contained in:
Josef Zweck 2024-11-09 16:42:10 +01:00 committed by GitHub
parent 97fa568876
commit 622682eb43
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 15 deletions

View File

@ -1,11 +1,11 @@
"""Button platform for La Marzocco espresso machines.""" """Button platform for La Marzocco espresso machines."""
import asyncio
from collections.abc import Callable, Coroutine from collections.abc import Callable, Coroutine
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any from typing import Any
from pylamarzocco.exceptions import RequestNotSuccessful from pylamarzocco.exceptions import RequestNotSuccessful
from pylamarzocco.lm_machine import LaMarzoccoMachine
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -13,9 +13,11 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN from .const import DOMAIN
from .coordinator import LaMarzoccoConfigEntry from .coordinator import LaMarzoccoConfigEntry, LaMarzoccoUpdateCoordinator
from .entity import LaMarzoccoEntity, LaMarzoccoEntityDescription from .entity import LaMarzoccoEntity, LaMarzoccoEntityDescription
BACKFLUSH_ENABLED_DURATION = 15
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)
class LaMarzoccoButtonEntityDescription( class LaMarzoccoButtonEntityDescription(
@ -24,14 +26,25 @@ class LaMarzoccoButtonEntityDescription(
): ):
"""Description of a La Marzocco button.""" """Description of a La Marzocco button."""
press_fn: Callable[[LaMarzoccoMachine], Coroutine[Any, Any, None]] press_fn: Callable[[LaMarzoccoUpdateCoordinator], Coroutine[Any, Any, None]]
async def async_backflush_and_update(coordinator: LaMarzoccoUpdateCoordinator) -> None:
"""Press backflush button."""
await coordinator.device.start_backflush()
# lib will set state optimistically
coordinator.async_set_updated_data(None)
# backflush is enabled for 15 seconds
# then turns off automatically
await asyncio.sleep(BACKFLUSH_ENABLED_DURATION + 1)
await coordinator.async_request_refresh()
ENTITIES: tuple[LaMarzoccoButtonEntityDescription, ...] = ( ENTITIES: tuple[LaMarzoccoButtonEntityDescription, ...] = (
LaMarzoccoButtonEntityDescription( LaMarzoccoButtonEntityDescription(
key="start_backflush", key="start_backflush",
translation_key="start_backflush", translation_key="start_backflush",
press_fn=lambda machine: machine.start_backflush(), press_fn=async_backflush_and_update,
), ),
) )
@ -59,7 +72,7 @@ class LaMarzoccoButtonEntity(LaMarzoccoEntity, ButtonEntity):
async def async_press(self) -> None: async def async_press(self) -> None:
"""Press button.""" """Press button."""
try: try:
await self.entity_description.press_fn(self.coordinator.device) await self.entity_description.press_fn(self.coordinator)
except RequestNotSuccessful as exc: except RequestNotSuccessful as exc:
raise HomeAssistantError( raise HomeAssistantError(
translation_domain=DOMAIN, translation_domain=DOMAIN,
@ -68,4 +81,3 @@ class LaMarzoccoButtonEntity(LaMarzoccoEntity, ButtonEntity):
"key": self.entity_description.key, "key": self.entity_description.key,
}, },
) from exc ) from exc
await self.coordinator.async_request_refresh()

View File

@ -1,6 +1,6 @@
"""Tests for the La Marzocco Buttons.""" """Tests for the La Marzocco Buttons."""
from unittest.mock import MagicMock from unittest.mock import AsyncMock, MagicMock, patch
from pylamarzocco.exceptions import RequestNotSuccessful from pylamarzocco.exceptions import RequestNotSuccessful
import pytest import pytest
@ -33,14 +33,18 @@ async def test_start_backflush(
assert entry assert entry
assert entry == snapshot assert entry == snapshot
await hass.services.async_call( with patch(
BUTTON_DOMAIN, "homeassistant.components.lamarzocco.button.asyncio.sleep",
SERVICE_PRESS, new_callable=AsyncMock,
{ ):
ATTR_ENTITY_ID: f"button.{serial_number}_start_backflush", await hass.services.async_call(
}, BUTTON_DOMAIN,
blocking=True, SERVICE_PRESS,
) {
ATTR_ENTITY_ID: f"button.{serial_number}_start_backflush",
},
blocking=True,
)
assert len(mock_lamarzocco.start_backflush.mock_calls) == 1 assert len(mock_lamarzocco.start_backflush.mock_calls) == 1
mock_lamarzocco.start_backflush.assert_called_once() mock_lamarzocco.start_backflush.assert_called_once()