From def11f9959a9fb6a6e74001ee90955bbb143805b Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Tue, 22 Apr 2025 12:52:27 +0200 Subject: [PATCH] Change lamarzocco general update frequency (#143417) --- .../components/lamarzocco/coordinator.py | 49 +++++++++---------- .../lamarzocco/test_binary_sensor.py | 1 + tests/components/lamarzocco/test_init.py | 2 + 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/lamarzocco/coordinator.py b/homeassistant/components/lamarzocco/coordinator.py index a8b3d9d0ee7..a83f7e6ab76 100644 --- a/homeassistant/components/lamarzocco/coordinator.py +++ b/homeassistant/components/lamarzocco/coordinator.py @@ -19,7 +19,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda from .const import DOMAIN -SCAN_INTERVAL = timedelta(seconds=30) +SCAN_INTERVAL = timedelta(seconds=15) SETTINGS_UPDATE_INTERVAL = timedelta(hours=1) SCHEDULE_UPDATE_INTERVAL = timedelta(minutes=5) _LOGGER = logging.getLogger(__name__) @@ -82,35 +82,32 @@ class LaMarzoccoUpdateCoordinator(DataUpdateCoordinator[None]): class LaMarzoccoConfigUpdateCoordinator(LaMarzoccoUpdateCoordinator): """Class to handle fetching data from the La Marzocco API centrally.""" - async def _async_connect_websocket(self) -> None: - """Set up the coordinator.""" - if not self.device.websocket.connected: - _LOGGER.debug("Init WebSocket in background task") - - self.config_entry.async_create_background_task( - hass=self.hass, - target=self.device.connect_dashboard_websocket( - update_callback=lambda _: self.async_set_updated_data(None) - ), - name="lm_websocket_task", - ) - - async def websocket_close(_: Any | None = None) -> None: - if self.device.websocket.connected: - await self.device.websocket.disconnect() - - self.config_entry.async_on_unload( - self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, websocket_close - ) - ) - self.config_entry.async_on_unload(websocket_close) - async def _internal_async_update_data(self) -> None: """Fetch data from API endpoint.""" + + if self.device.websocket.connected: + return await self.device.get_dashboard() _LOGGER.debug("Current status: %s", self.device.dashboard.to_dict()) - await self._async_connect_websocket() + + _LOGGER.debug("Init WebSocket in background task") + + self.config_entry.async_create_background_task( + hass=self.hass, + target=self.device.connect_dashboard_websocket( + update_callback=lambda _: self.async_set_updated_data(None) + ), + name="lm_websocket_task", + ) + + async def websocket_close(_: Any | None = None) -> None: + if self.device.websocket.connected: + await self.device.websocket.disconnect() + + self.config_entry.async_on_unload( + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, websocket_close) + ) + self.config_entry.async_on_unload(websocket_close) class LaMarzoccoSettingsUpdateCoordinator(LaMarzoccoUpdateCoordinator): diff --git a/tests/components/lamarzocco/test_binary_sensor.py b/tests/components/lamarzocco/test_binary_sensor.py index 2fbd58eab85..8e92c9bbba9 100644 --- a/tests/components/lamarzocco/test_binary_sensor.py +++ b/tests/components/lamarzocco/test_binary_sensor.py @@ -65,6 +65,7 @@ async def test_sensor_going_unavailable( assert state assert state.state != STATE_UNAVAILABLE + mock_lamarzocco.websocket.connected = False mock_lamarzocco.get_dashboard.side_effect = RequestNotSuccessful("") freezer.tick(timedelta(minutes=10)) async_fire_time_changed(hass) diff --git a/tests/components/lamarzocco/test_init.py b/tests/components/lamarzocco/test_init.py index 94429913ed7..31510ad1426 100644 --- a/tests/components/lamarzocco/test_init.py +++ b/tests/components/lamarzocco/test_init.py @@ -53,6 +53,7 @@ async def test_config_entry_not_ready( mock_lamarzocco: MagicMock, ) -> None: """Test the La Marzocco configuration entry not ready.""" + mock_lamarzocco.websocket.connected = False mock_lamarzocco.get_dashboard.side_effect = RequestNotSuccessful("") await async_init_integration(hass, mock_config_entry) @@ -90,6 +91,7 @@ async def test_invalid_auth( mock_lamarzocco: MagicMock, ) -> None: """Test auth error during setup.""" + mock_lamarzocco.websocket.connected = False mock_lamarzocco.get_dashboard.side_effect = AuthFail("") await async_init_integration(hass, mock_config_entry)