mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Store runtime data inside the config entry in Goalzero (#119440)
This commit is contained in:
parent
dc3ade6558
commit
5b91ea4550
@ -6,20 +6,18 @@ from typing import TYPE_CHECKING
|
|||||||
|
|
||||||
from goalzero import Yeti, exceptions
|
from goalzero import Yeti, exceptions
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_HOST, Platform
|
from homeassistant.const import CONF_HOST, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.device_registry import format_mac
|
from homeassistant.helpers.device_registry import format_mac
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import GoalZeroConfigEntry, GoalZeroDataUpdateCoordinator
|
||||||
from .coordinator import GoalZeroDataUpdateCoordinator
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: GoalZeroConfigEntry) -> bool:
|
||||||
"""Set up Goal Zero Yeti from a config entry."""
|
"""Set up Goal Zero Yeti from a config entry."""
|
||||||
|
|
||||||
mac = entry.unique_id
|
mac = entry.unique_id
|
||||||
@ -38,16 +36,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
except exceptions.ConnectError as ex:
|
except exceptions.ConnectError as ex:
|
||||||
raise ConfigEntryNotReady(f"Failed to connect to device: {ex}") from ex
|
raise ConfigEntryNotReady(f"Failed to connect to device: {ex}") from ex
|
||||||
|
|
||||||
coordinator = GoalZeroDataUpdateCoordinator(hass, api)
|
entry.runtime_data = GoalZeroDataUpdateCoordinator(hass, api)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await entry.runtime_data.async_config_entry_first_refresh()
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: GoalZeroConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
return unload_ok
|
|
||||||
|
@ -9,12 +9,11 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
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 .coordinator import GoalZeroConfigEntry
|
||||||
from .entity import GoalZeroEntity
|
from .entity import GoalZeroEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
@ -43,14 +42,13 @@ BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: GoalZeroConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Goal Zero Yeti sensor."""
|
"""Set up the Goal Zero Yeti sensor."""
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
GoalZeroBinarySensor(
|
GoalZeroBinarySensor(entry.runtime_data, description)
|
||||||
hass.data[DOMAIN][entry.entry_id],
|
|
||||||
description,
|
|
||||||
)
|
|
||||||
for description in BINARY_SENSOR_TYPES
|
for description in BINARY_SENSOR_TYPES
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -10,11 +10,13 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
|||||||
|
|
||||||
from .const import DOMAIN, LOGGER
|
from .const import DOMAIN, LOGGER
|
||||||
|
|
||||||
|
type GoalZeroConfigEntry = ConfigEntry[GoalZeroDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class GoalZeroDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
class GoalZeroDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
||||||
"""Data update coordinator for the Goal zero integration."""
|
"""Data update coordinator for the Goal zero integration."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: GoalZeroConfigEntry
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, api: Yeti) -> None:
|
def __init__(self, hass: HomeAssistant, api: Yeti) -> None:
|
||||||
"""Initialize the coordinator."""
|
"""Initialize the coordinator."""
|
||||||
|
@ -10,7 +10,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,
|
||||||
SIGNAL_STRENGTH_DECIBELS,
|
SIGNAL_STRENGTH_DECIBELS,
|
||||||
@ -26,7 +25,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import GoalZeroConfigEntry
|
||||||
from .entity import GoalZeroEntity
|
from .entity import GoalZeroEntity
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
@ -130,15 +129,13 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: GoalZeroConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Goal Zero Yeti sensor."""
|
"""Set up the Goal Zero Yeti sensor."""
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
GoalZeroSensor(
|
GoalZeroSensor(entry.runtime_data, description) for description in SENSOR_TYPES
|
||||||
hass.data[DOMAIN][entry.entry_id],
|
|
||||||
description,
|
|
||||||
)
|
|
||||||
for description in SENSOR_TYPES
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,11 +5,10 @@ from __future__ import annotations
|
|||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
||||||
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 .const import DOMAIN
|
from .coordinator import GoalZeroConfigEntry
|
||||||
from .entity import GoalZeroEntity
|
from .entity import GoalZeroEntity
|
||||||
|
|
||||||
SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
|
SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
|
||||||
@ -29,15 +28,13 @@ SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: GoalZeroConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Goal Zero Yeti switch."""
|
"""Set up the Goal Zero Yeti switch."""
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
GoalZeroSwitch(
|
GoalZeroSwitch(entry.runtime_data, description) for description in SWITCH_TYPES
|
||||||
hass.data[DOMAIN][entry.entry_id],
|
|
||||||
description,
|
|
||||||
)
|
|
||||||
for description in SWITCH_TYPES
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,8 +3,7 @@
|
|||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
from homeassistant.components import dhcp
|
from homeassistant.components import dhcp
|
||||||
from homeassistant.components.goalzero import DOMAIN
|
from homeassistant.components.goalzero.const import DEFAULT_NAME, DOMAIN
|
||||||
from homeassistant.components.goalzero.const import DEFAULT_NAME
|
|
||||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import format_mac
|
from homeassistant.helpers.device_registry import format_mac
|
||||||
|
Loading…
x
Reference in New Issue
Block a user