Move flipr coordinator to its own file (#100467)

This commit is contained in:
Jan-Philipp Benecke 2023-09-16 11:19:49 +02:00 committed by GitHub
parent 024db6dadf
commit 57337b5cee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 46 deletions

View File

@ -1,27 +1,16 @@
"""The Flipr integration.""" """The Flipr integration."""
from datetime import timedelta
import logging
from flipr_api import FliprAPIRestClient
from flipr_api.exceptions import FliprError
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
DataUpdateCoordinator, DataUpdateCoordinator,
UpdateFailed,
) )
from .const import ATTRIBUTION, CONF_FLIPR_ID, DOMAIN, MANUFACTURER from .const import ATTRIBUTION, CONF_FLIPR_ID, DOMAIN, MANUFACTURER
from .coordinator import FliprDataUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(minutes=60)
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR] PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
@ -49,38 +38,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok return unload_ok
class FliprDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to hold Flipr data retrieval."""
def __init__(self, hass, entry):
"""Initialize."""
username = entry.data[CONF_EMAIL]
password = entry.data[CONF_PASSWORD]
self.flipr_id = entry.data[CONF_FLIPR_ID]
# Establishes the connection.
self.client = FliprAPIRestClient(username, password)
self.entry = entry
super().__init__(
hass,
_LOGGER,
name=f"Flipr data measure for {self.flipr_id}",
update_interval=SCAN_INTERVAL,
)
async def _async_update_data(self):
"""Fetch data from API endpoint."""
try:
data = await self.hass.async_add_executor_job(
self.client.get_pool_measure_latest, self.flipr_id
)
except FliprError as error:
raise UpdateFailed(error) from error
return data
class FliprEntity(CoordinatorEntity): class FliprEntity(CoordinatorEntity):
"""Implements a common class elements representing the Flipr component.""" """Implements a common class elements representing the Flipr component."""

View File

@ -0,0 +1,45 @@
"""DataUpdateCoordinator for flipr integration."""
from datetime import timedelta
import logging
from flipr_api import FliprAPIRestClient
from flipr_api.exceptions import FliprError
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import CONF_FLIPR_ID
_LOGGER = logging.getLogger(__name__)
class FliprDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to hold Flipr data retrieval."""
def __init__(self, hass, entry):
"""Initialize."""
username = entry.data[CONF_EMAIL]
password = entry.data[CONF_PASSWORD]
self.flipr_id = entry.data[CONF_FLIPR_ID]
# Establishes the connection.
self.client = FliprAPIRestClient(username, password)
self.entry = entry
super().__init__(
hass,
_LOGGER,
name=f"Flipr data measure for {self.flipr_id}",
update_interval=timedelta(minutes=60),
)
async def _async_update_data(self):
"""Fetch data from API endpoint."""
try:
data = await self.hass.async_add_executor_job(
self.client.get_pool_measure_latest, self.flipr_id
)
except FliprError as error:
raise UpdateFailed(error) from error
return data

View File

@ -21,7 +21,7 @@ async def test_unload_entry(hass: HomeAssistant) -> None:
unique_id="123456", unique_id="123456",
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
with patch("homeassistant.components.flipr.FliprAPIRestClient"): with patch("homeassistant.components.flipr.coordinator.FliprAPIRestClient"):
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)