mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Store runtime data inside the config entry in fyta (#120761)
This commit is contained in:
parent
7aca7cf858
commit
255cc9ed74
@ -18,7 +18,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.util.dt import async_get_time_zone
|
from homeassistant.util.dt import async_get_time_zone
|
||||||
|
|
||||||
from .const import CONF_EXPIRATION, DOMAIN
|
from .const import CONF_EXPIRATION
|
||||||
from .coordinator import FytaCoordinator
|
from .coordinator import FytaCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -26,9 +26,10 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
PLATFORMS = [
|
PLATFORMS = [
|
||||||
Platform.SENSOR,
|
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."""
|
"""Set up the Fyta integration."""
|
||||||
tz: str = hass.config.time_zone
|
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()
|
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)
|
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:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload Fyta entity."""
|
"""Unload Fyta entity."""
|
||||||
|
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|
||||||
|
|
||||||
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||||
|
@ -14,9 +14,10 @@ from fyta_cli.fyta_exceptions import (
|
|||||||
)
|
)
|
||||||
import voluptuous as vol
|
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 homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
|
|
||||||
|
from . import FytaConfigEntry
|
||||||
from .const import CONF_EXPIRATION, DOMAIN
|
from .const import CONF_EXPIRATION, DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -36,7 +37,7 @@ class FytaConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
"""Initialize FytaConfigFlow."""
|
"""Initialize FytaConfigFlow."""
|
||||||
self.credentials: dict[str, Any] = {}
|
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]:
|
async def async_auth(self, user_input: Mapping[str, Any]) -> dict[str, str]:
|
||||||
"""Reusable Auth Helper."""
|
"""Reusable Auth Helper."""
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
"""Coordinator for FYTA integration."""
|
"""Coordinator for FYTA integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
from fyta_cli.fyta_connector import FytaConnector
|
from fyta_cli.fyta_connector import FytaConnector
|
||||||
from fyta_cli.fyta_exceptions import (
|
from fyta_cli.fyta_exceptions import (
|
||||||
@ -12,7 +14,6 @@ from fyta_cli.fyta_exceptions import (
|
|||||||
FytaPlantError,
|
FytaPlantError,
|
||||||
)
|
)
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
@ -20,13 +21,16 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
|||||||
|
|
||||||
from .const import CONF_EXPIRATION
|
from .const import CONF_EXPIRATION
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from . import FytaConfigEntry
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class FytaCoordinator(DataUpdateCoordinator[dict[int, dict[str, Any]]]):
|
class FytaCoordinator(DataUpdateCoordinator[dict[int, dict[str, Any]]]):
|
||||||
"""Fyta custom coordinator."""
|
"""Fyta custom coordinator."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: FytaConfigEntry
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, fyta: FytaConnector) -> None:
|
def __init__(self, hass: HomeAssistant, fyta: FytaConnector) -> None:
|
||||||
"""Initialize my coordinator."""
|
"""Initialize my coordinator."""
|
||||||
|
@ -5,11 +5,10 @@ from __future__ import annotations
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import async_redact_data
|
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.const import CONF_ACCESS_TOKEN, CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import FytaConfigEntry
|
||||||
|
|
||||||
TO_REDACT = [
|
TO_REDACT = [
|
||||||
CONF_PASSWORD,
|
CONF_PASSWORD,
|
||||||
@ -19,10 +18,10 @@ TO_REDACT = [
|
|||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, config_entry: ConfigEntry
|
hass: HomeAssistant, config_entry: FytaConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
data = hass.data[DOMAIN][config_entry.entry_id].data
|
data = config_entry.runtime_data.data
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT),
|
"config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT),
|
||||||
|
@ -15,7 +15,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
EntityCategory,
|
EntityCategory,
|
||||||
@ -25,7 +24,7 @@ from homeassistant.const import (
|
|||||||
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 .const import DOMAIN
|
from . import FytaConfigEntry
|
||||||
from .coordinator import FytaCoordinator
|
from .coordinator import FytaCoordinator
|
||||||
from .entity import FytaPlantEntity
|
from .entity import FytaPlantEntity
|
||||||
|
|
||||||
@ -130,10 +129,10 @@ SENSORS: Final[list[FytaSensorEntityDescription]] = [
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: FytaConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the FYTA sensors."""
|
"""Set up the FYTA sensors."""
|
||||||
coordinator: FytaCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: FytaCoordinator = entry.runtime_data
|
||||||
|
|
||||||
plant_entities = [
|
plant_entities = [
|
||||||
FytaPlantSensor(coordinator, entry, sensor, plant_id)
|
FytaPlantSensor(coordinator, entry, sensor, plant_id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user