mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Use runtime_data in gogogate2 (#144322)
This commit is contained in:
parent
5a01521ff8
commit
57217b46ed
@ -1,16 +1,16 @@
|
||||
"""The gogogate2 component."""
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_DEVICE, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .common import get_data_update_coordinator
|
||||
from .common import create_data_update_coordinator
|
||||
from .const import DEVICE_TYPE_GOGOGATE2
|
||||
from .coordinator import GogoGateConfigEntry
|
||||
|
||||
PLATFORMS = [Platform.COVER, Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: GogoGateConfigEntry) -> bool:
|
||||
"""Do setup of Gogogate2."""
|
||||
|
||||
# Update the config entry.
|
||||
@ -24,14 +24,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
if config_updates:
|
||||
hass.config_entries.async_update_entry(entry, data=config_updates)
|
||||
|
||||
data_update_coordinator = get_data_update_coordinator(hass, entry)
|
||||
data_update_coordinator = create_data_update_coordinator(hass, entry)
|
||||
await data_update_coordinator.async_config_entry_first_refresh()
|
||||
|
||||
entry.runtime_data = data_update_coordinator
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: GogoGateConfigEntry) -> bool:
|
||||
"""Unload Gogogate2 config entry."""
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
@ -16,7 +16,6 @@ from ismartgate import (
|
||||
)
|
||||
from ismartgate.common import AbstractDoor
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_DEVICE,
|
||||
CONF_IP_ADDRESS,
|
||||
@ -27,8 +26,8 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.httpx_client import get_async_client
|
||||
from homeassistant.helpers.update_coordinator import UpdateFailed
|
||||
|
||||
from .const import DATA_UPDATE_COORDINATOR, DEVICE_TYPE_ISMARTGATE, DOMAIN
|
||||
from .coordinator import DeviceDataUpdateCoordinator
|
||||
from .const import DEVICE_TYPE_ISMARTGATE
|
||||
from .coordinator import DeviceDataUpdateCoordinator, GogoGateConfigEntry
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -41,47 +40,40 @@ class StateData(NamedTuple):
|
||||
door: AbstractDoor | None
|
||||
|
||||
|
||||
def get_data_update_coordinator(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
def create_data_update_coordinator(
|
||||
hass: HomeAssistant, config_entry: GogoGateConfigEntry
|
||||
) -> DeviceDataUpdateCoordinator:
|
||||
"""Get an update coordinator."""
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN].setdefault(config_entry.entry_id, {})
|
||||
config_entry_data = hass.data[DOMAIN][config_entry.entry_id]
|
||||
api = get_api(hass, config_entry.data)
|
||||
|
||||
if DATA_UPDATE_COORDINATOR not in config_entry_data:
|
||||
api = get_api(hass, config_entry.data)
|
||||
async def async_update_data() -> GogoGate2InfoResponse | ISmartGateInfoResponse:
|
||||
try:
|
||||
return await api.async_info()
|
||||
except Exception as exception:
|
||||
raise UpdateFailed(
|
||||
f"Error communicating with API: {exception}"
|
||||
) from exception
|
||||
|
||||
async def async_update_data() -> GogoGate2InfoResponse | ISmartGateInfoResponse:
|
||||
try:
|
||||
return await api.async_info()
|
||||
except Exception as exception:
|
||||
raise UpdateFailed(
|
||||
f"Error communicating with API: {exception}"
|
||||
) from exception
|
||||
|
||||
config_entry_data[DATA_UPDATE_COORDINATOR] = DeviceDataUpdateCoordinator(
|
||||
hass,
|
||||
config_entry,
|
||||
_LOGGER,
|
||||
api,
|
||||
# Name of the data. For logging purposes.
|
||||
name="gogogate2",
|
||||
update_method=async_update_data,
|
||||
# Polling interval. Will only be polled if there are subscribers.
|
||||
update_interval=timedelta(seconds=5),
|
||||
)
|
||||
|
||||
return config_entry_data[DATA_UPDATE_COORDINATOR]
|
||||
return DeviceDataUpdateCoordinator(
|
||||
hass,
|
||||
config_entry,
|
||||
_LOGGER,
|
||||
api,
|
||||
# Name of the data. For logging purposes.
|
||||
name="gogogate2",
|
||||
update_method=async_update_data,
|
||||
# Polling interval. Will only be polled if there are subscribers.
|
||||
update_interval=timedelta(seconds=5),
|
||||
)
|
||||
|
||||
|
||||
def cover_unique_id(config_entry: ConfigEntry, door: AbstractDoor) -> str:
|
||||
def cover_unique_id(config_entry: GogoGateConfigEntry, door: AbstractDoor) -> str:
|
||||
"""Generate a cover entity unique id."""
|
||||
return f"{config_entry.unique_id}_{door.door_id}"
|
||||
|
||||
|
||||
def sensor_unique_id(
|
||||
config_entry: ConfigEntry, door: AbstractDoor, sensor_type: str
|
||||
config_entry: GogoGateConfigEntry, door: AbstractDoor, sensor_type: str
|
||||
) -> str:
|
||||
"""Generate a cover entity unique id."""
|
||||
return f"{config_entry.unique_id}_{door.door_id}_{sensor_type}"
|
||||
|
@ -1,7 +1,7 @@
|
||||
"""Constants for integration."""
|
||||
|
||||
DOMAIN = "gogogate2"
|
||||
DATA_UPDATE_COORDINATOR = "data_update_coordinator"
|
||||
|
||||
DEVICE_TYPE_GOGOGATE2 = "gogogate2"
|
||||
DEVICE_TYPE_ISMARTGATE = "ismartgate"
|
||||
MANUFACTURER = "Remsol"
|
||||
|
@ -13,18 +13,20 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.debounce import Debouncer
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
type GogoGateConfigEntry = ConfigEntry[DeviceDataUpdateCoordinator]
|
||||
|
||||
|
||||
class DeviceDataUpdateCoordinator(
|
||||
DataUpdateCoordinator[GogoGate2InfoResponse | ISmartGateInfoResponse]
|
||||
):
|
||||
"""Manages polling for state changes from the device."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: GogoGateConfigEntry
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: GogoGateConfigEntry,
|
||||
logger: logging.Logger,
|
||||
api: AbstractGateApi,
|
||||
*,
|
||||
|
@ -16,22 +16,21 @@ from homeassistant.components.cover import (
|
||||
CoverEntity,
|
||||
CoverEntityFeature,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .common import cover_unique_id, get_data_update_coordinator
|
||||
from .coordinator import DeviceDataUpdateCoordinator
|
||||
from .common import cover_unique_id
|
||||
from .coordinator import DeviceDataUpdateCoordinator, GogoGateConfigEntry
|
||||
from .entity import GoGoGate2Entity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: GogoGateConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the config entry."""
|
||||
data_update_coordinator = get_data_update_coordinator(hass, config_entry)
|
||||
data_update_coordinator = config_entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
[
|
||||
@ -48,7 +47,7 @@ class DeviceCover(GoGoGate2Entity, CoverEntity):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: GogoGateConfigEntry,
|
||||
data_update_coordinator: DeviceDataUpdateCoordinator,
|
||||
door: AbstractDoor,
|
||||
) -> None:
|
||||
|
@ -4,13 +4,12 @@ from __future__ import annotations
|
||||
|
||||
from ismartgate.common import AbstractDoor, get_door_by_id
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_IP_ADDRESS
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN, MANUFACTURER
|
||||
from .coordinator import DeviceDataUpdateCoordinator
|
||||
from .coordinator import DeviceDataUpdateCoordinator, GogoGateConfigEntry
|
||||
|
||||
|
||||
class GoGoGate2Entity(CoordinatorEntity[DeviceDataUpdateCoordinator]):
|
||||
@ -18,7 +17,7 @@ class GoGoGate2Entity(CoordinatorEntity[DeviceDataUpdateCoordinator]):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: GogoGateConfigEntry,
|
||||
data_update_coordinator: DeviceDataUpdateCoordinator,
|
||||
door: AbstractDoor,
|
||||
unique_id: str,
|
||||
|
@ -11,13 +11,12 @@ from homeassistant.components.sensor import (
|
||||
SensorEntity,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .common import get_data_update_coordinator, sensor_unique_id
|
||||
from .coordinator import DeviceDataUpdateCoordinator
|
||||
from .common import sensor_unique_id
|
||||
from .coordinator import DeviceDataUpdateCoordinator, GogoGateConfigEntry
|
||||
from .entity import GoGoGate2Entity
|
||||
|
||||
SENSOR_ID_WIRED = "WIRE"
|
||||
@ -25,11 +24,11 @@ SENSOR_ID_WIRED = "WIRE"
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: GogoGateConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the config entry."""
|
||||
data_update_coordinator = get_data_update_coordinator(hass, config_entry)
|
||||
data_update_coordinator = config_entry.runtime_data
|
||||
|
||||
sensors = chain(
|
||||
[
|
||||
@ -69,7 +68,7 @@ class DoorSensorBattery(DoorSensorEntity):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: GogoGateConfigEntry,
|
||||
data_update_coordinator: DeviceDataUpdateCoordinator,
|
||||
door: AbstractDoor,
|
||||
) -> None:
|
||||
@ -97,7 +96,7 @@ class DoorSensorTemperature(DoorSensorEntity):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: GogoGateConfigEntry,
|
||||
data_update_coordinator: DeviceDataUpdateCoordinator,
|
||||
door: AbstractDoor,
|
||||
) -> None:
|
||||
|
Loading…
x
Reference in New Issue
Block a user