Move Totalconnect coordinator to separate module (#116368)

* Move Totalconnect coordinator to separate module

* Move Totalconnect coordinator to separate module

* Move Totalconnect coordinator to separate module
This commit is contained in:
Joost Lekkerkerker 2024-05-05 13:05:36 +02:00 committed by GitHub
parent 7e11fec761
commit 7d5aa03bf0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 67 additions and 54 deletions

View file

@ -1,29 +1,20 @@
"""The totalconnect component.""" """The totalconnect component."""
from datetime import timedelta
import logging
from total_connect_client.client import TotalConnectClient from total_connect_client.client import TotalConnectClient
from total_connect_client.exceptions import ( from total_connect_client.exceptions import AuthenticationError
AuthenticationError,
ServiceUnavailable,
TotalConnectError,
)
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.exceptions import ConfigEntryAuthFailed
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import AUTO_BYPASS, CONF_USERCODES, DOMAIN from .const import AUTO_BYPASS, CONF_USERCODES, DOMAIN
from .coordinator import TotalConnectDataUpdateCoordinator
PLATFORMS = [Platform.ALARM_CONTROL_PANEL, Platform.BINARY_SENSOR, Platform.BUTTON] PLATFORMS = [Platform.ALARM_CONTROL_PANEL, Platform.BINARY_SENSOR, Platform.BUTTON]
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False) CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
SCAN_INTERVAL = timedelta(seconds=30)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
@ -76,41 +67,3 @@ async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
client = hass.data[DOMAIN][entry.entry_id].client client = hass.data[DOMAIN][entry.entry_id].client
for location_id in client.locations: for location_id in client.locations:
client.locations[location_id].auto_bypass_low_battery = bypass client.locations[location_id].auto_bypass_low_battery = bypass
class TotalConnectDataUpdateCoordinator(DataUpdateCoordinator[None]): # pylint: disable=hass-enforce-coordinator-module
"""Class to fetch data from TotalConnect."""
config_entry: ConfigEntry
def __init__(self, hass: HomeAssistant, client: TotalConnectClient) -> None:
"""Initialize."""
self.hass = hass
self.client = client
super().__init__(
hass, logger=_LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL
)
async def _async_update_data(self) -> None:
"""Update data."""
await self.hass.async_add_executor_job(self.sync_update_data)
def sync_update_data(self) -> None:
"""Fetch synchronous data from TotalConnect."""
try:
for location_id in self.client.locations:
self.client.locations[location_id].get_panel_meta_data()
except AuthenticationError as exception:
# should only encounter if password changes during operation
raise ConfigEntryAuthFailed(
"TotalConnect authentication failed during operation."
) from exception
except ServiceUnavailable as exception:
raise UpdateFailed(
"Error connecting to TotalConnect or the service is unavailable. "
"Check https://status.resideo.com/ for outages."
) from exception
except TotalConnectError as exception:
raise UpdateFailed(exception) from exception
except ValueError as exception:
raise UpdateFailed("Unknown state from TotalConnect") from exception

View file

@ -26,8 +26,8 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_platform from homeassistant.helpers import entity_platform
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import TotalConnectDataUpdateCoordinator
from .const import DOMAIN from .const import DOMAIN
from .coordinator import TotalConnectDataUpdateCoordinator
from .entity import TotalConnectLocationEntity from .entity import TotalConnectLocationEntity
SERVICE_ALARM_ARM_AWAY_INSTANT = "arm_away_instant" SERVICE_ALARM_ARM_AWAY_INSTANT = "arm_away_instant"

View file

@ -17,8 +17,8 @@ from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import TotalConnectDataUpdateCoordinator
from .const import DOMAIN from .const import DOMAIN
from .coordinator import TotalConnectDataUpdateCoordinator
from .entity import TotalConnectLocationEntity, TotalConnectZoneEntity from .entity import TotalConnectLocationEntity, TotalConnectZoneEntity
LOW_BATTERY = "low_battery" LOW_BATTERY = "low_battery"

View file

@ -12,8 +12,8 @@ from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import TotalConnectDataUpdateCoordinator
from .const import DOMAIN from .const import DOMAIN
from .coordinator import TotalConnectDataUpdateCoordinator
from .entity import TotalConnectLocationEntity, TotalConnectZoneEntity from .entity import TotalConnectLocationEntity, TotalConnectZoneEntity

View file

@ -0,0 +1,58 @@
"""The totalconnect component."""
from datetime import timedelta
import logging
from total_connect_client.client import TotalConnectClient
from total_connect_client.exceptions import (
AuthenticationError,
ServiceUnavailable,
TotalConnectError,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN
SCAN_INTERVAL = timedelta(seconds=30)
_LOGGER = logging.getLogger(__name__)
class TotalConnectDataUpdateCoordinator(DataUpdateCoordinator[None]):
"""Class to fetch data from TotalConnect."""
config_entry: ConfigEntry
def __init__(self, hass: HomeAssistant, client: TotalConnectClient) -> None:
"""Initialize."""
self.client = client
super().__init__(
hass, logger=_LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL
)
async def _async_update_data(self) -> None:
"""Update data."""
await self.hass.async_add_executor_job(self.sync_update_data)
def sync_update_data(self) -> None:
"""Fetch synchronous data from TotalConnect."""
try:
for location_id in self.client.locations:
self.client.locations[location_id].get_panel_meta_data()
except AuthenticationError as exception:
# should only encounter if password changes during operation
raise ConfigEntryAuthFailed(
"TotalConnect authentication failed during operation."
) from exception
except ServiceUnavailable as exception:
raise UpdateFailed(
"Error connecting to TotalConnect or the service is unavailable. "
"Check https://status.resideo.com/ for outages."
) from exception
except TotalConnectError as exception:
raise UpdateFailed(exception) from exception
except ValueError as exception:
raise UpdateFailed("Unknown state from TotalConnect") from exception

View file

@ -6,7 +6,8 @@ from total_connect_client.zone import TotalConnectZone
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import DOMAIN, TotalConnectDataUpdateCoordinator from .const import DOMAIN
from .coordinator import TotalConnectDataUpdateCoordinator
class TotalConnectEntity(CoordinatorEntity[TotalConnectDataUpdateCoordinator]): class TotalConnectEntity(CoordinatorEntity[TotalConnectDataUpdateCoordinator]):

View file

@ -8,11 +8,12 @@ from syrupy import SnapshotAssertion
from total_connect_client.exceptions import ServiceUnavailable, TotalConnectError from total_connect_client.exceptions import ServiceUnavailable, TotalConnectError
from homeassistant.components.alarm_control_panel import DOMAIN as ALARM_DOMAIN from homeassistant.components.alarm_control_panel import DOMAIN as ALARM_DOMAIN
from homeassistant.components.totalconnect import DOMAIN, SCAN_INTERVAL
from homeassistant.components.totalconnect.alarm_control_panel import ( from homeassistant.components.totalconnect.alarm_control_panel import (
SERVICE_ALARM_ARM_AWAY_INSTANT, SERVICE_ALARM_ARM_AWAY_INSTANT,
SERVICE_ALARM_ARM_HOME_INSTANT, SERVICE_ALARM_ARM_HOME_INSTANT,
) )
from homeassistant.components.totalconnect.const import DOMAIN
from homeassistant.components.totalconnect.coordinator import SCAN_INTERVAL
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
SERVICE_ALARM_ARM_AWAY, SERVICE_ALARM_ARM_AWAY,