mirror of
https://github.com/home-assistant/core.git
synced 2026-04-18 07:56:03 +01:00
Use runtime_data in opengarage integration (#167040)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -4,18 +4,17 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import opengarage
|
import opengarage
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_VERIFY_SSL, Platform
|
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_VERIFY_SSL, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import CONF_DEVICE_KEY, DOMAIN
|
from .const import CONF_DEVICE_KEY
|
||||||
from .coordinator import OpenGarageDataUpdateCoordinator
|
from .coordinator import OpenGarageConfigEntry, OpenGarageDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.COVER, Platform.SENSOR]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.COVER, Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: OpenGarageConfigEntry) -> bool:
|
||||||
"""Set up OpenGarage from a config entry."""
|
"""Set up OpenGarage from a config entry."""
|
||||||
open_garage_connection = opengarage.OpenGarage(
|
open_garage_connection = opengarage.OpenGarage(
|
||||||
f"{entry.data[CONF_HOST]}:{entry.data[CONF_PORT]}",
|
f"{entry.data[CONF_HOST]}:{entry.data[CONF_PORT]}",
|
||||||
@@ -27,17 +26,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
hass, entry, open_garage_connection
|
hass, entry, open_garage_connection
|
||||||
)
|
)
|
||||||
await open_garage_data_coordinator.async_config_entry_first_refresh()
|
await open_garage_data_coordinator.async_config_entry_first_refresh()
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = open_garage_data_coordinator
|
entry.runtime_data = open_garage_data_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: OpenGarageConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
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
|
|
||||||
|
|||||||
@@ -9,12 +9,10 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import OpenGarageConfigEntry, OpenGarageDataUpdateCoordinator
|
||||||
from .coordinator import OpenGarageDataUpdateCoordinator
|
|
||||||
from .entity import OpenGarageEntity
|
from .entity import OpenGarageEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@@ -30,13 +28,11 @@ SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: OpenGarageConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the OpenGarage binary sensors."""
|
"""Set up the OpenGarage binary sensors."""
|
||||||
open_garage_data_coordinator: OpenGarageDataUpdateCoordinator = hass.data[DOMAIN][
|
open_garage_data_coordinator = entry.runtime_data
|
||||||
entry.entry_id
|
|
||||||
]
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
OpenGarageBinarySensor(
|
OpenGarageBinarySensor(
|
||||||
open_garage_data_coordinator,
|
open_garage_data_coordinator,
|
||||||
|
|||||||
@@ -13,13 +13,11 @@ from homeassistant.components.button import (
|
|||||||
ButtonEntity,
|
ButtonEntity,
|
||||||
ButtonEntityDescription,
|
ButtonEntityDescription,
|
||||||
)
|
)
|
||||||
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 AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import OpenGarageConfigEntry, OpenGarageDataUpdateCoordinator
|
||||||
from .coordinator import OpenGarageDataUpdateCoordinator
|
|
||||||
from .entity import OpenGarageEntity
|
from .entity import OpenGarageEntity
|
||||||
|
|
||||||
|
|
||||||
@@ -42,13 +40,11 @@ BUTTONS: tuple[OpenGarageButtonEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: OpenGarageConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the OpenGarage button entities."""
|
"""Set up the OpenGarage button entities."""
|
||||||
coordinator: OpenGarageDataUpdateCoordinator = hass.data[DOMAIN][
|
coordinator = config_entry.runtime_data
|
||||||
config_entry.entry_id
|
|
||||||
]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
OpenGarageButtonEntity(
|
OpenGarageButtonEntity(
|
||||||
|
|||||||
@@ -18,15 +18,18 @@ from .const import DOMAIN
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
type OpenGarageConfigEntry = ConfigEntry[OpenGarageDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class OpenGarageDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
class OpenGarageDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||||
"""Class to manage fetching Opengarage data."""
|
"""Class to manage fetching Opengarage data."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: OpenGarageConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: OpenGarageConfigEntry,
|
||||||
open_garage_connection: opengarage.OpenGarage,
|
open_garage_connection: opengarage.OpenGarage,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize global Opengarage data updater."""
|
"""Initialize global Opengarage data updater."""
|
||||||
|
|||||||
@@ -11,12 +11,10 @@ from homeassistant.components.cover import (
|
|||||||
CoverEntityFeature,
|
CoverEntityFeature,
|
||||||
CoverState,
|
CoverState,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import OpenGarageConfigEntry, OpenGarageDataUpdateCoordinator
|
||||||
from .coordinator import OpenGarageDataUpdateCoordinator
|
|
||||||
from .entity import OpenGarageEntity
|
from .entity import OpenGarageEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@@ -26,12 +24,12 @@ STATES_MAP = {0: CoverState.CLOSED, 1: CoverState.OPEN}
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: OpenGarageConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the OpenGarage covers."""
|
"""Set up the OpenGarage covers."""
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[OpenGarageCover(hass.data[DOMAIN][entry.entry_id], cast(str, entry.unique_id))]
|
[OpenGarageCover(entry.runtime_data, cast(str, entry.unique_id))]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,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_MILLIWATT,
|
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||||
@@ -22,8 +21,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import OpenGarageConfigEntry
|
||||||
from .coordinator import OpenGarageDataUpdateCoordinator
|
|
||||||
from .entity import OpenGarageEntity
|
from .entity import OpenGarageEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@@ -60,13 +58,11 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: OpenGarageConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the OpenGarage sensors."""
|
"""Set up the OpenGarage sensors."""
|
||||||
open_garage_data_coordinator: OpenGarageDataUpdateCoordinator = hass.data[DOMAIN][
|
open_garage_data_coordinator = entry.runtime_data
|
||||||
entry.entry_id
|
|
||||||
]
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
OpenGarageSensor(
|
OpenGarageSensor(
|
||||||
open_garage_data_coordinator,
|
open_garage_data_coordinator,
|
||||||
|
|||||||
Reference in New Issue
Block a user