From 4e600b7b1987dd2fd4aa67f27d4599a8bb9f3499 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 15 May 2024 15:18:26 +0200 Subject: [PATCH] Move venstar coordinator to separate module (#117500) --- .coveragerc | 3 +- homeassistant/components/venstar/__init__.py | 69 +---------------- homeassistant/components/venstar/climate.py | 3 +- .../components/venstar/coordinator.py | 75 +++++++++++++++++++ homeassistant/components/venstar/sensor.py | 3 +- tests/components/venstar/test_climate.py | 4 +- tests/components/venstar/test_init.py | 2 +- 7 files changed, 85 insertions(+), 74 deletions(-) create mode 100644 homeassistant/components/venstar/coordinator.py diff --git a/.coveragerc b/.coveragerc index d0bd99a17d0d9e..4dd7e40c1d6d87 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1567,9 +1567,8 @@ omit = homeassistant/components/velux/__init__.py homeassistant/components/velux/cover.py homeassistant/components/velux/light.py - homeassistant/components/venstar/__init__.py - homeassistant/components/venstar/binary_sensor.py homeassistant/components/venstar/climate.py + homeassistant/components/venstar/coordinator.py homeassistant/components/venstar/sensor.py homeassistant/components/verisure/__init__.py homeassistant/components/verisure/alarm_control_panel.py diff --git a/homeassistant/components/venstar/__init__.py b/homeassistant/components/venstar/__init__.py index 13368a603503f1..cbcfd3dff90d05 100644 --- a/homeassistant/components/venstar/__init__.py +++ b/homeassistant/components/venstar/__init__.py @@ -2,10 +2,6 @@ from __future__ import annotations -import asyncio -from datetime import timedelta - -from requests import RequestException from venstarcolortouch import VenstarColorTouch from homeassistant.config_entries import ConfigEntry @@ -18,11 +14,11 @@ Platform, ) from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers import update_coordinator from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity -from .const import _LOGGER, DOMAIN, VENSTAR_SLEEP, VENSTAR_TIMEOUT +from .const import DOMAIN, VENSTAR_TIMEOUT +from .coordinator import VenstarDataUpdateCoordinator PLATFORMS = [Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.SENSOR] @@ -65,67 +61,6 @@ async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool: return unload_ok -class VenstarDataUpdateCoordinator(update_coordinator.DataUpdateCoordinator[None]): # pylint: disable=hass-enforce-coordinator-module - """Class to manage fetching Venstar data.""" - - def __init__( - self, - hass: HomeAssistant, - *, - venstar_connection: VenstarColorTouch, - ) -> None: - """Initialize global Venstar data updater.""" - super().__init__( - hass, - _LOGGER, - name=DOMAIN, - update_interval=timedelta(seconds=60), - ) - self.client = venstar_connection - self.runtimes: list[dict[str, int]] = [] - - async def _async_update_data(self) -> None: - """Update the state.""" - try: - await self.hass.async_add_executor_job(self.client.update_info) - except (OSError, RequestException) as ex: - raise update_coordinator.UpdateFailed( - f"Exception during Venstar info update: {ex}" - ) from ex - - # older venstars sometimes cannot handle rapid sequential connections - await asyncio.sleep(VENSTAR_SLEEP) - - try: - await self.hass.async_add_executor_job(self.client.update_sensors) - except (OSError, RequestException) as ex: - raise update_coordinator.UpdateFailed( - f"Exception during Venstar sensor update: {ex}" - ) from ex - - # older venstars sometimes cannot handle rapid sequential connections - await asyncio.sleep(VENSTAR_SLEEP) - - try: - await self.hass.async_add_executor_job(self.client.update_alerts) - except (OSError, RequestException) as ex: - raise update_coordinator.UpdateFailed( - f"Exception during Venstar alert update: {ex}" - ) from ex - - # older venstars sometimes cannot handle rapid sequential connections - await asyncio.sleep(VENSTAR_SLEEP) - - try: - self.runtimes = await self.hass.async_add_executor_job( - self.client.get_runtimes - ) - except (OSError, RequestException) as ex: - raise update_coordinator.UpdateFailed( - f"Exception during Venstar runtime update: {ex}" - ) from ex - - class VenstarEntity(CoordinatorEntity[VenstarDataUpdateCoordinator]): """Representation of a Venstar entity.""" diff --git a/homeassistant/components/venstar/climate.py b/homeassistant/components/venstar/climate.py index e0aacadffa7e34..f47cf59be9c982 100644 --- a/homeassistant/components/venstar/climate.py +++ b/homeassistant/components/venstar/climate.py @@ -36,7 +36,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from . import VenstarDataUpdateCoordinator, VenstarEntity +from . import VenstarEntity from .const import ( _LOGGER, ATTR_FAN_STATE, @@ -46,6 +46,7 @@ DOMAIN, HOLD_MODE_TEMPERATURE, ) +from .coordinator import VenstarDataUpdateCoordinator PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { diff --git a/homeassistant/components/venstar/coordinator.py b/homeassistant/components/venstar/coordinator.py new file mode 100644 index 00000000000000..b825775de7fe55 --- /dev/null +++ b/homeassistant/components/venstar/coordinator.py @@ -0,0 +1,75 @@ +"""Coordinator for the venstar component.""" + +from __future__ import annotations + +import asyncio +from datetime import timedelta + +from requests import RequestException +from venstarcolortouch import VenstarColorTouch + +from homeassistant.core import HomeAssistant +from homeassistant.helpers import update_coordinator + +from .const import _LOGGER, DOMAIN, VENSTAR_SLEEP + + +class VenstarDataUpdateCoordinator(update_coordinator.DataUpdateCoordinator[None]): + """Class to manage fetching Venstar data.""" + + def __init__( + self, + hass: HomeAssistant, + *, + venstar_connection: VenstarColorTouch, + ) -> None: + """Initialize global Venstar data updater.""" + super().__init__( + hass, + _LOGGER, + name=DOMAIN, + update_interval=timedelta(seconds=60), + ) + self.client = venstar_connection + self.runtimes: list[dict[str, int]] = [] + + async def _async_update_data(self) -> None: + """Update the state.""" + try: + await self.hass.async_add_executor_job(self.client.update_info) + except (OSError, RequestException) as ex: + raise update_coordinator.UpdateFailed( + f"Exception during Venstar info update: {ex}" + ) from ex + + # older venstars sometimes cannot handle rapid sequential connections + await asyncio.sleep(VENSTAR_SLEEP) + + try: + await self.hass.async_add_executor_job(self.client.update_sensors) + except (OSError, RequestException) as ex: + raise update_coordinator.UpdateFailed( + f"Exception during Venstar sensor update: {ex}" + ) from ex + + # older venstars sometimes cannot handle rapid sequential connections + await asyncio.sleep(VENSTAR_SLEEP) + + try: + await self.hass.async_add_executor_job(self.client.update_alerts) + except (OSError, RequestException) as ex: + raise update_coordinator.UpdateFailed( + f"Exception during Venstar alert update: {ex}" + ) from ex + + # older venstars sometimes cannot handle rapid sequential connections + await asyncio.sleep(VENSTAR_SLEEP) + + try: + self.runtimes = await self.hass.async_add_executor_job( + self.client.get_runtimes + ) + except (OSError, RequestException) as ex: + raise update_coordinator.UpdateFailed( + f"Exception during Venstar runtime update: {ex}" + ) from ex diff --git a/homeassistant/components/venstar/sensor.py b/homeassistant/components/venstar/sensor.py index b4913a874d0ade..ee4ad43ade6df3 100644 --- a/homeassistant/components/venstar/sensor.py +++ b/homeassistant/components/venstar/sensor.py @@ -23,8 +23,9 @@ from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import VenstarDataUpdateCoordinator, VenstarEntity +from . import VenstarEntity from .const import DOMAIN +from .coordinator import VenstarDataUpdateCoordinator RUNTIME_HEAT1 = "heat1" RUNTIME_HEAT2 = "heat2" diff --git a/tests/components/venstar/test_climate.py b/tests/components/venstar/test_climate.py index c090fadb445540..7107729d148e56 100644 --- a/tests/components/venstar/test_climate.py +++ b/tests/components/venstar/test_climate.py @@ -20,7 +20,7 @@ async def test_colortouch(hass: HomeAssistant) -> None: """Test interfacing with a venstar colortouch with attached humidifier.""" - with patch("homeassistant.components.venstar.VENSTAR_SLEEP", new=0): + with patch("homeassistant.components.venstar.coordinator.VENSTAR_SLEEP", new=0): await async_init_integration(hass) state = hass.states.get("climate.colortouch") @@ -56,7 +56,7 @@ async def test_colortouch(hass: HomeAssistant) -> None: async def test_t2000(hass: HomeAssistant) -> None: """Test interfacing with a venstar T2000 presently turned off.""" - with patch("homeassistant.components.venstar.VENSTAR_SLEEP", new=0): + with patch("homeassistant.components.venstar.coordinator.VENSTAR_SLEEP", new=0): await async_init_integration(hass) state = hass.states.get("climate.t2000") diff --git a/tests/components/venstar/test_init.py b/tests/components/venstar/test_init.py index bc8d400df6c473..3a03c4c4b88d52 100644 --- a/tests/components/venstar/test_init.py +++ b/tests/components/venstar/test_init.py @@ -47,7 +47,7 @@ async def test_setup_entry(hass: HomeAssistant) -> None: new=VenstarColorTouchMock.get_runtimes, ), patch( - "homeassistant.components.venstar.VENSTAR_SLEEP", + "homeassistant.components.venstar.coordinator.VENSTAR_SLEEP", new=0, ), ):