Skip to content

Commit

Permalink
Moved data coordinator to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
iprak committed Dec 28, 2024
1 parent e806d5a commit 81995c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
19 changes: 2 additions & 17 deletions homeassistant/components/vesync/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""VeSync integration."""

from datetime import timedelta
import logging

from pyvesync import VeSync
Expand All @@ -9,7 +8,6 @@
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .common import async_process_devices
from .const import (
Expand All @@ -23,6 +21,7 @@
VS_SENSORS,
VS_SWITCHES,
)
from .coordinator import VeSyncDataCoordinator

PLATFORMS = [Platform.FAN, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH]

Expand Down Expand Up @@ -51,21 +50,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
hass.data[DOMAIN] = {}
hass.data[DOMAIN][VS_MANAGER] = manager

# Create a DataUpdateCoordinator for the manager
async def async_update_data():
"""Fetch data from API endpoint."""
try:
await hass.async_add_executor_job(manager.update)
except Exception as err:
raise UpdateFailed(f"Update failed: {err}") from err

coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name=DOMAIN,
update_method=async_update_data,
update_interval=timedelta(seconds=30),
)
coordinator = VeSyncDataCoordinator(hass, manager)

# Store coordinator at domain level since only single integration instance is permitted.
hass.data[DOMAIN][VS_COORDINATOR] = coordinator
Expand Down
37 changes: 37 additions & 0 deletions homeassistant/components/vesync/coordinator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Representation of VeSync data coordinator."""

from __future__ import annotations

from datetime import timedelta
import logging
from typing import Any

from pyvesync import VeSync

from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

_LOGGER = logging.getLogger(__name__)


class VeSyncDataCoordinator(DataUpdateCoordinator):
"""Class representing data coordinator for VeSync devices."""

def __init__(self, hass: HomeAssistant, manager: VeSync) -> None:
"""Initialize."""
self._manager = manager

super().__init__(
hass,
_LOGGER,
name="VeSyncDataCoordinator",
update_interval=timedelta(seconds=30),
)

async def _async_update_data(self) -> dict[str, Any]:
"""Fetch data from API endpoint."""

try:
return await self.hass.async_add_executor_job(self._manager.update)
except Exception as error:
raise UpdateFailed(error) from error

Check warning on line 37 in homeassistant/components/vesync/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/vesync/coordinator.py#L36-L37

Added lines #L36 - L37 were not covered by tests

0 comments on commit 81995c5

Please sign in to comment.