Skip to content

Commit

Permalink
Move venstar coordinator to separate module (home-assistant#117500)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored May 15, 2024
1 parent d5a1587 commit 4e600b7
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 74 deletions.
3 changes: 1 addition & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 2 additions & 67 deletions homeassistant/components/venstar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]

Expand Down Expand Up @@ -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."""

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/venstar/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -46,6 +46,7 @@
DOMAIN,
HOLD_MODE_TEMPERATURE,
)
from .coordinator import VenstarDataUpdateCoordinator

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
Expand Down
75 changes: 75 additions & 0 deletions homeassistant/components/venstar/coordinator.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion homeassistant/components/venstar/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions tests/components/venstar/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/components/venstar/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
):
Expand Down

0 comments on commit 4e600b7

Please sign in to comment.