Move iqvia base entity to separate module (#126489)

This commit is contained in:
epenet 2024-09-23 09:57:54 +02:00 committed by GitHub
parent 3f4f2f4e2b
commit 6d069bec19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 65 additions and 55 deletions

View file

@ -13,15 +13,10 @@ from pyiqvia.errors import IQVIAError
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from .const import ( from .const import (
CONF_ZIP_CODE, CONF_ZIP_CODE,
@ -112,50 +107,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN].pop(entry.entry_id) hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok return unload_ok
class IQVIAEntity(CoordinatorEntity[DataUpdateCoordinator[dict[str, Any]]]):
"""Define a base IQVIA entity."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: DataUpdateCoordinator[dict[str, Any]],
entry: ConfigEntry,
description: EntityDescription,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_extra_state_attributes = {}
self._attr_unique_id = f"{entry.data[CONF_ZIP_CODE]}_{description.key}"
self._entry = entry
self.entity_description = description
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
if not self.coordinator.last_update_success:
return
self.update_from_latest_data()
self.async_write_ha_state()
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
await super().async_added_to_hass()
if self.entity_description.key == TYPE_ALLERGY_FORECAST:
self.async_on_remove(
self.hass.data[DOMAIN][self._entry.entry_id][
TYPE_ALLERGY_OUTLOOK
].async_add_listener(self._handle_coordinator_update)
)
self.update_from_latest_data()
@callback
def update_from_latest_data(self) -> None:
"""Update the entity from the latest data."""
raise NotImplementedError

View file

@ -0,0 +1,62 @@
"""Support for IQVIA."""
from __future__ import annotations
from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from .const import CONF_ZIP_CODE, DOMAIN, TYPE_ALLERGY_FORECAST, TYPE_ALLERGY_OUTLOOK
class IQVIAEntity(CoordinatorEntity[DataUpdateCoordinator[dict[str, Any]]]):
"""Define a base IQVIA entity."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: DataUpdateCoordinator[dict[str, Any]],
entry: ConfigEntry,
description: EntityDescription,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_extra_state_attributes = {}
self._attr_unique_id = f"{entry.data[CONF_ZIP_CODE]}_{description.key}"
self._entry = entry
self.entity_description = description
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
if not self.coordinator.last_update_success:
return
self.update_from_latest_data()
self.async_write_ha_state()
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
await super().async_added_to_hass()
if self.entity_description.key == TYPE_ALLERGY_FORECAST:
self.async_on_remove(
self.hass.data[DOMAIN][self._entry.entry_id][
TYPE_ALLERGY_OUTLOOK
].async_add_listener(self._handle_coordinator_update)
)
self.update_from_latest_data()
@callback
def update_from_latest_data(self) -> None:
"""Update the entity from the latest data."""
raise NotImplementedError

View file

@ -17,7 +17,6 @@ from homeassistant.const import ATTR_STATE
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import IQVIAEntity
from .const import ( from .const import (
DOMAIN, DOMAIN,
TYPE_ALLERGY_FORECAST, TYPE_ALLERGY_FORECAST,
@ -33,6 +32,7 @@ from .const import (
TYPE_DISEASE_INDEX, TYPE_DISEASE_INDEX,
TYPE_DISEASE_TODAY, TYPE_DISEASE_TODAY,
) )
from .entity import IQVIAEntity
ATTR_ALLERGEN_AMOUNT = "allergen_amount" ATTR_ALLERGEN_AMOUNT = "allergen_amount"
ATTR_ALLERGEN_GENUS = "allergen_genus" ATTR_ALLERGEN_GENUS = "allergen_genus"