Store runtime data inside the config entry in fyta (#120761)

This commit is contained in:
dontinelli 2024-06-30 14:57:48 +02:00 committed by GitHub
parent 7aca7cf858
commit 255cc9ed74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 21 additions and 21 deletions

View file

@ -18,7 +18,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
from homeassistant.util.dt import async_get_time_zone
from .const import CONF_EXPIRATION, DOMAIN
from .const import CONF_EXPIRATION
from .coordinator import FytaCoordinator
_LOGGER = logging.getLogger(__name__)
@ -26,9 +26,10 @@ _LOGGER = logging.getLogger(__name__)
PLATFORMS = [
Platform.SENSOR,
]
type FytaConfigEntry = ConfigEntry[FytaCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: FytaConfigEntry) -> bool:
"""Set up the Fyta integration."""
tz: str = hass.config.time_zone
@ -45,7 +46,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
@ -55,11 +56,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload Fyta entity."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:

View file

@ -14,9 +14,10 @@ from fyta_cli.fyta_exceptions import (
)
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from . import FytaConfigEntry
from .const import CONF_EXPIRATION, DOMAIN
_LOGGER = logging.getLogger(__name__)
@ -36,7 +37,7 @@ class FytaConfigFlow(ConfigFlow, domain=DOMAIN):
def __init__(self) -> None:
"""Initialize FytaConfigFlow."""
self.credentials: dict[str, Any] = {}
self._entry: ConfigEntry | None = None
self._entry: FytaConfigEntry | None = None
async def async_auth(self, user_input: Mapping[str, Any]) -> dict[str, str]:
"""Reusable Auth Helper."""

View file

@ -1,8 +1,10 @@
"""Coordinator for FYTA integration."""
from __future__ import annotations
from datetime import datetime, timedelta
import logging
from typing import Any
from typing import TYPE_CHECKING, Any
from fyta_cli.fyta_connector import FytaConnector
from fyta_cli.fyta_exceptions import (
@ -12,7 +14,6 @@ from fyta_cli.fyta_exceptions import (
FytaPlantError,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
@ -20,13 +21,16 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import CONF_EXPIRATION
if TYPE_CHECKING:
from . import FytaConfigEntry
_LOGGER = logging.getLogger(__name__)
class FytaCoordinator(DataUpdateCoordinator[dict[int, dict[str, Any]]]):
"""Fyta custom coordinator."""
config_entry: ConfigEntry
config_entry: FytaConfigEntry
def __init__(self, hass: HomeAssistant, fyta: FytaConnector) -> None:
"""Initialize my coordinator."""

View file

@ -5,11 +5,10 @@ from __future__ import annotations
from typing import Any
from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from .const import DOMAIN
from . import FytaConfigEntry
TO_REDACT = [
CONF_PASSWORD,
@ -19,10 +18,10 @@ TO_REDACT = [
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: ConfigEntry
hass: HomeAssistant, config_entry: FytaConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
data = hass.data[DOMAIN][config_entry.entry_id].data
data = config_entry.runtime_data.data
return {
"config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT),

View file

@ -15,7 +15,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
EntityCategory,
@ -25,7 +24,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from . import FytaConfigEntry
from .coordinator import FytaCoordinator
from .entity import FytaPlantEntity
@ -130,10 +129,10 @@ SENSORS: Final[list[FytaSensorEntityDescription]] = [
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: FytaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the FYTA sensors."""
coordinator: FytaCoordinator = hass.data[DOMAIN][entry.entry_id]
coordinator: FytaCoordinator = entry.runtime_data
plant_entities = [
FytaPlantSensor(coordinator, entry, sensor, plant_id)