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