mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Move evil_genius_labs coordinator to separate module (#117435)
This commit is contained in:
parent
9add251b0a
commit
83f5133065
@ -2,12 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
|
||||||
from datetime import timedelta
|
|
||||||
import logging
|
|
||||||
from typing import cast
|
|
||||||
|
|
||||||
from aiohttp import ContentTypeError
|
|
||||||
import pyevilgenius
|
import pyevilgenius
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -15,12 +9,10 @@ from homeassistant.const import Platform
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import aiohttp_client, device_registry as dr
|
from homeassistant.helpers import aiohttp_client, device_registry as dr
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
CoordinatorEntity,
|
|
||||||
DataUpdateCoordinator,
|
|
||||||
)
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .coordinator import EvilGeniusUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.LIGHT]
|
PLATFORMS = [Platform.LIGHT]
|
||||||
|
|
||||||
@ -51,56 +43,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return unload_ok
|
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]):
|
class EvilGeniusEntity(CoordinatorEntity[EvilGeniusUpdateCoordinator]):
|
||||||
"""Base entity for Evil Genius."""
|
"""Base entity for Evil Genius."""
|
||||||
|
|
||||||
|
66
homeassistant/components/evil_genius_labs/coordinator.py
Normal file
66
homeassistant/components/evil_genius_labs/coordinator.py
Normal 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())
|
@ -8,8 +8,8 @@ from homeassistant.components.diagnostics import async_redact_data
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from . import EvilGeniusUpdateCoordinator
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .coordinator import EvilGeniusUpdateCoordinator
|
||||||
|
|
||||||
TO_REDACT = {"wiFiSsidDefault", "wiFiSSID"}
|
TO_REDACT = {"wiFiSsidDefault", "wiFiSSID"}
|
||||||
|
|
||||||
|
@ -11,8 +11,9 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
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 EvilGeniusEntity, EvilGeniusUpdateCoordinator
|
from . import EvilGeniusEntity
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .coordinator import EvilGeniusUpdateCoordinator
|
||||||
from .util import update_when_done
|
from .util import update_when_done
|
||||||
|
|
||||||
HA_NO_EFFECT = "None"
|
HA_NO_EFFECT = "None"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user