Migrate doorbird to use entry.runtime_data (#121413)
parent
b6609fa77c
commit
792c6a9cd9
|
@ -10,7 +10,6 @@ from doorbirdpy import DoorBird
|
|||
import requests
|
||||
|
||||
from homeassistant.components import persistent_notification
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_HOST,
|
||||
CONF_NAME,
|
||||
|
@ -25,7 +24,7 @@ from homeassistant.helpers.typing import ConfigType
|
|||
|
||||
from .const import CONF_EVENTS, DOMAIN, PLATFORMS
|
||||
from .device import ConfiguredDoorBird
|
||||
from .models import DoorBirdData
|
||||
from .models import DoorBirdConfigEntry, DoorBirdData
|
||||
from .view import DoorBirdRequestView
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -37,13 +36,12 @@ CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
|||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the DoorBird component."""
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
# Provide an endpoint for the door stations to call to trigger events
|
||||
hass.http.register_view(DoorBirdRequestView)
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: DoorBirdConfigEntry) -> bool:
|
||||
"""Set up DoorBird from a config entry."""
|
||||
|
||||
_async_import_options_from_data_if_missing(hass, entry)
|
||||
|
@ -91,7 +89,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
raise ConfigEntryNotReady
|
||||
|
||||
entry.async_on_unload(entry.add_update_listener(_update_listener))
|
||||
hass.data[DOMAIN][config_entry_id] = door_bird_data
|
||||
entry.runtime_data = door_bird_data
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
@ -102,12 +100,9 @@ def _init_door_bird_device(device: DoorBird) -> tuple[tuple[bool, int], dict[str
|
|||
return device.ready(), device.info()
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: DoorBirdConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
data: dict[str, DoorBirdData] = hass.data[DOMAIN]
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
data.pop(entry.entry_id)
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
|
||||
async def _async_register_events(
|
||||
|
@ -133,11 +128,9 @@ async def _async_register_events(
|
|||
return True
|
||||
|
||||
|
||||
async def _update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
||||
async def _update_listener(hass: HomeAssistant, entry: DoorBirdConfigEntry) -> None:
|
||||
"""Handle options update."""
|
||||
config_entry_id = entry.entry_id
|
||||
data: DoorBirdData = hass.data[DOMAIN][config_entry_id]
|
||||
door_station = data.door_station
|
||||
door_station = entry.runtime_data.door_station
|
||||
door_station.update_events(entry.options[CONF_EVENTS])
|
||||
# Subscribe to doorbell or motion events
|
||||
await _async_register_events(hass, door_station)
|
||||
|
@ -145,7 +138,7 @@ async def _update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
|||
|
||||
@callback
|
||||
def _async_import_options_from_data_if_missing(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
hass: HomeAssistant, entry: DoorBirdConfigEntry
|
||||
) -> None:
|
||||
options = dict(entry.options)
|
||||
modified = False
|
||||
|
|
|
@ -6,13 +6,11 @@ from dataclasses import dataclass
|
|||
from doorbirdpy import DoorBird
|
||||
|
||||
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .entity import DoorBirdEntity
|
||||
from .models import DoorBirdData
|
||||
from .models import DoorBirdConfigEntry, DoorBirdData
|
||||
|
||||
IR_RELAY = "__ir_light__"
|
||||
|
||||
|
@ -38,12 +36,11 @@ IR_ENTITY_DESCRIPTION = DoorbirdButtonEntityDescription(
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: DoorBirdConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the DoorBird button platform."""
|
||||
config_entry_id = config_entry.entry_id
|
||||
door_bird_data: DoorBirdData = hass.data[DOMAIN][config_entry_id]
|
||||
door_bird_data = config_entry.runtime_data
|
||||
relays = door_bird_data.door_station_info["RELAYS"]
|
||||
|
||||
entities = [
|
||||
|
|
|
@ -9,15 +9,13 @@ import logging
|
|||
import aiohttp
|
||||
|
||||
from homeassistant.components.camera import Camera, CameraEntityFeature
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .const import DOMAIN
|
||||
from .entity import DoorBirdEntity
|
||||
from .models import DoorBirdData
|
||||
from .models import DoorBirdConfigEntry, DoorBirdData
|
||||
|
||||
_LAST_VISITOR_INTERVAL = datetime.timedelta(minutes=2)
|
||||
_LAST_MOTION_INTERVAL = datetime.timedelta(seconds=30)
|
||||
|
@ -28,12 +26,11 @@ _TIMEOUT = 15 # seconds
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: DoorBirdConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the DoorBird camera platform."""
|
||||
config_entry_id = config_entry.entry_id
|
||||
door_bird_data: DoorBirdData = hass.data[DOMAIN][config_entry_id]
|
||||
door_bird_data = config_entry.runtime_data
|
||||
device = door_bird_data.door_station.device
|
||||
|
||||
async_add_entities(
|
||||
|
|
|
@ -7,14 +7,13 @@ from homeassistant.components.event import (
|
|||
EventEntity,
|
||||
EventEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .device import DoorbirdEvent
|
||||
from .entity import DoorBirdEntity
|
||||
from .models import DoorBirdData
|
||||
from .models import DoorBirdConfigEntry, DoorBirdData
|
||||
|
||||
EVENT_DESCRIPTIONS = {
|
||||
"doorbell": EventEntityDescription(
|
||||
|
@ -34,12 +33,11 @@ EVENT_DESCRIPTIONS = {
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: DoorBirdConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the DoorBird event platform."""
|
||||
config_entry_id = config_entry.entry_id
|
||||
door_bird_data: DoorBirdData = hass.data[DOMAIN][config_entry_id]
|
||||
door_bird_data = config_entry.runtime_data
|
||||
async_add_entities(
|
||||
DoorBirdEventEntity(door_bird_data, doorbird_event, description)
|
||||
for doorbird_event in door_bird_data.door_station.event_descriptions
|
||||
|
@ -48,7 +46,7 @@ async def async_setup_entry(
|
|||
|
||||
|
||||
class DoorBirdEventEntity(DoorBirdEntity, EventEntity):
|
||||
"""A relay in a DoorBird device."""
|
||||
"""A doorbird event entity."""
|
||||
|
||||
entity_description: EventEntityDescription
|
||||
_attr_has_entity_name = True
|
||||
|
|
|
@ -13,7 +13,7 @@ from homeassistant.const import ATTR_ENTITY_ID
|
|||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .models import DoorBirdData
|
||||
from .util import async_get_entries
|
||||
|
||||
|
||||
@callback
|
||||
|
@ -35,8 +35,8 @@ def async_describe_events(
|
|||
LOGBOOK_ENTRY_ENTITY_ID: event.data.get(ATTR_ENTITY_ID),
|
||||
}
|
||||
|
||||
domain_data: dict[str, DoorBirdData] = hass.data[DOMAIN]
|
||||
for data in domain_data.values():
|
||||
for entry in async_get_entries(hass):
|
||||
data = entry.runtime_data
|
||||
for event in data.door_station.door_station_events:
|
||||
async_describe_event(
|
||||
DOMAIN, f"{DOMAIN}_{event}", async_describe_logbook_event
|
||||
|
|
|
@ -5,8 +5,12 @@ from __future__ import annotations
|
|||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
|
||||
from .device import ConfiguredDoorBird
|
||||
|
||||
type DoorBirdConfigEntry = ConfigEntry[DoorBirdData]
|
||||
|
||||
|
||||
@dataclass
|
||||
class DoorBirdData:
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
"""DoorBird integration utils."""
|
||||
|
||||
from typing import Any
|
||||
from typing import Any, cast
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .device import ConfiguredDoorBird
|
||||
from .models import DoorBirdData
|
||||
from .models import DoorBirdConfigEntry
|
||||
|
||||
|
||||
def get_mac_address_from_door_station_info(door_station_info: dict[str, Any]) -> str:
|
||||
|
@ -18,9 +18,18 @@ def get_door_station_by_token(
|
|||
hass: HomeAssistant, token: str
|
||||
) -> ConfiguredDoorBird | None:
|
||||
"""Get door station by token."""
|
||||
domain_data: dict[str, DoorBirdData] = hass.data[DOMAIN]
|
||||
for data in domain_data.values():
|
||||
door_station = data.door_station
|
||||
for entry in async_get_entries(hass):
|
||||
door_station = entry.runtime_data.door_station
|
||||
if door_station.token == token:
|
||||
return door_station
|
||||
return None
|
||||
|
||||
|
||||
@callback
|
||||
def async_get_entries(hass: HomeAssistant) -> list[DoorBirdConfigEntry]:
|
||||
"""Get all the doorbird entries."""
|
||||
entries = hass.config_entries.async_entries(
|
||||
DOMAIN, include_ignore=True, include_disabled=True
|
||||
)
|
||||
active_entries = [entry for entry in entries if hasattr(entry, "runtime_data")]
|
||||
return cast(list[DoorBirdConfigEntry], active_entries)
|
||||
|
|
Loading…
Reference in New Issue