Move evil_genius_labs coordinator to separate module (#117435)

This commit is contained in:
epenet 2024-05-14 16:32:41 +02:00 committed by GitHub
parent 9add251b0a
commit 83f5133065
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 62 deletions

View File

@ -2,12 +2,6 @@
from __future__ import annotations
import asyncio
from datetime import timedelta
import logging
from typing import cast
from aiohttp import ContentTypeError
import pyevilgenius
from homeassistant.config_entries import ConfigEntry
@ -15,12 +9,10 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client, device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import EvilGeniusUpdateCoordinator
PLATFORMS = [Platform.LIGHT]
@ -51,56 +43,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return unload_ok
class EvilGeniusUpdateCoordinator(DataUpdateCoordinator[dict]): # pylint: disable=hass-enforce-coordinator-module
"""Update coordinator for Evil Genius data."""
info: dict
product: dict | None
def __init__(
self, hass: HomeAssistant, name: str, client: pyevilgenius.EvilGeniusDevice
) -> None:
"""Initialize the data update coordinator."""
self.client = client
super().__init__(
hass,
logging.getLogger(__name__),
name=name,
update_interval=timedelta(seconds=UPDATE_INTERVAL),
)
@property
def device_name(self) -> str:
"""Return the device name."""
return cast(str, self.data["name"]["value"])
@property
def product_name(self) -> str | None:
"""Return the product name."""
if self.product is None:
return None
return cast(str, self.product["productName"])
async def _async_update_data(self) -> dict:
"""Update Evil Genius data."""
if not hasattr(self, "info"):
async with asyncio.timeout(5):
self.info = await self.client.get_info()
if not hasattr(self, "product"):
async with asyncio.timeout(5):
try:
self.product = await self.client.get_product()
except ContentTypeError:
# Older versions of the API don't support this
self.product = None
async with asyncio.timeout(5):
return cast(dict, await self.client.get_all())
class EvilGeniusEntity(CoordinatorEntity[EvilGeniusUpdateCoordinator]):
"""Base entity for Evil Genius."""

View File

@ -0,0 +1,66 @@
"""Coordinator for the Evil Genius Labs integration."""
from __future__ import annotations
import asyncio
from datetime import timedelta
import logging
from typing import cast
from aiohttp import ContentTypeError
import pyevilgenius
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
UPDATE_INTERVAL = 10
class EvilGeniusUpdateCoordinator(DataUpdateCoordinator[dict]):
"""Update coordinator for Evil Genius data."""
info: dict
product: dict | None
def __init__(
self, hass: HomeAssistant, name: str, client: pyevilgenius.EvilGeniusDevice
) -> None:
"""Initialize the data update coordinator."""
self.client = client
super().__init__(
hass,
logging.getLogger(__name__),
name=name,
update_interval=timedelta(seconds=UPDATE_INTERVAL),
)
@property
def device_name(self) -> str:
"""Return the device name."""
return cast(str, self.data["name"]["value"])
@property
def product_name(self) -> str | None:
"""Return the product name."""
if self.product is None:
return None
return cast(str, self.product["productName"])
async def _async_update_data(self) -> dict:
"""Update Evil Genius data."""
if not hasattr(self, "info"):
async with asyncio.timeout(5):
self.info = await self.client.get_info()
if not hasattr(self, "product"):
async with asyncio.timeout(5):
try:
self.product = await self.client.get_product()
except ContentTypeError:
# Older versions of the API don't support this
self.product = None
async with asyncio.timeout(5):
return cast(dict, await self.client.get_all())

View File

@ -8,8 +8,8 @@ from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from . import EvilGeniusUpdateCoordinator
from .const import DOMAIN
from .coordinator import EvilGeniusUpdateCoordinator
TO_REDACT = {"wiFiSsidDefault", "wiFiSSID"}

View File

@ -11,8 +11,9 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import EvilGeniusEntity, EvilGeniusUpdateCoordinator
from . import EvilGeniusEntity
from .const import DOMAIN
from .coordinator import EvilGeniusUpdateCoordinator
from .util import update_when_done
HA_NO_EFFECT = "None"