Small cleanups to doorbird (#121454)

This commit is contained in:
J. Nick Koston 2024-07-07 13:25:13 -07:00 committed by GitHub
parent 8a28cbe9e2
commit 4cf733b9a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 50 deletions

View File

@ -17,7 +17,7 @@ from homeassistant.const import (
CONF_TOKEN, CONF_TOKEN,
CONF_USERNAME, CONF_USERNAME,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
@ -43,12 +43,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: DoorBirdConfigEntry) -> 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)
door_station_config = entry.data door_station_config = entry.data
config_entry_id = entry.entry_id config_entry_id = entry.entry_id
device_ip = door_station_config[CONF_HOST] device_ip = door_station_config[CONF_HOST]
username = door_station_config[CONF_USERNAME] username = door_station_config[CONF_USERNAME]
password = door_station_config[CONF_PASSWORD] password = door_station_config[CONF_PASSWORD]
@ -134,18 +130,3 @@ async def _update_listener(hass: HomeAssistant, entry: DoorBirdConfigEntry) -> N
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)
@callback
def _async_import_options_from_data_if_missing(
hass: HomeAssistant, entry: DoorBirdConfigEntry
) -> None:
options = dict(entry.options)
modified = False
for importable_option in (CONF_EVENTS,):
if importable_option not in entry.options and importable_option in entry.data:
options[importable_option] = entry.data[importable_option]
modified = True
if modified:
hass.config_entries.async_update_entry(entry, options=options)

View File

@ -39,7 +39,6 @@ async def async_setup_entry(
door_bird_data, door_bird_data,
device.live_image_url, device.live_image_url,
"live", "live",
"live",
_LIVE_INTERVAL, _LIVE_INTERVAL,
device.rtsp_live_video_url, device.rtsp_live_video_url,
), ),
@ -47,14 +46,12 @@ async def async_setup_entry(
door_bird_data, door_bird_data,
device.history_image_url(1, "doorbell"), device.history_image_url(1, "doorbell"),
"last_ring", "last_ring",
"last_ring",
_LAST_VISITOR_INTERVAL, _LAST_VISITOR_INTERVAL,
), ),
DoorBirdCamera( DoorBirdCamera(
door_bird_data, door_bird_data,
device.history_image_url(1, "motionsensor"), device.history_image_url(1, "motionsensor"),
"last_motion", "last_motion",
"last_motion",
_LAST_MOTION_INTERVAL, _LAST_MOTION_INTERVAL,
), ),
] ]
@ -69,7 +66,6 @@ class DoorBirdCamera(DoorBirdEntity, Camera):
door_bird_data: DoorBirdData, door_bird_data: DoorBirdData,
url: str, url: str,
camera_id: str, camera_id: str,
translation_key: str,
interval: datetime.timedelta, interval: datetime.timedelta,
stream_url: str | None = None, stream_url: str | None = None,
) -> None: ) -> None:
@ -77,7 +73,7 @@ class DoorBirdCamera(DoorBirdEntity, Camera):
super().__init__(door_bird_data) super().__init__(door_bird_data)
self._url = url self._url = url
self._stream_url = stream_url self._stream_url = stream_url
self._attr_translation_key = translation_key self._attr_translation_key = camera_id
self._last_image: bytes | None = None self._last_image: bytes | None = None
if self._stream_url: if self._stream_url:
self._attr_supported_features = CameraEntityFeature.STREAM self._attr_supported_features = CameraEntityFeature.STREAM

View File

@ -5,7 +5,7 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from functools import cached_property from functools import cached_property
import logging import logging
from typing import Any, cast from typing import Any
from doorbirdpy import DoorBird, DoorBirdScheduleEntry from doorbirdpy import DoorBird, DoorBirdScheduleEntry
@ -55,35 +55,35 @@ class ConfiguredDoorBird:
self._get_event_name(event) for event in self.events self._get_event_name(event) for event in self.events
] ]
@property @cached_property
def name(self) -> str | None: def name(self) -> str | None:
"""Get custom device name.""" """Get custom device name."""
return self._name return self._name
@property @cached_property
def device(self) -> DoorBird: def device(self) -> DoorBird:
"""Get the configured device.""" """Get the configured device."""
return self._device return self._device
@property @cached_property
def custom_url(self) -> str | None: def custom_url(self) -> str | None:
"""Get custom url for device.""" """Get custom url for device."""
return self._custom_url return self._custom_url
@property @cached_property
def token(self) -> str: def token(self) -> str:
"""Get token for device.""" """Get token for device."""
return self._token return self._token
def register_events(self, hass: HomeAssistant) -> None: def register_events(self, hass: HomeAssistant) -> None:
"""Register events on device.""" """Register events on device."""
# Override url if another is specified in the configuration
if custom_url := self.custom_url:
hass_url = custom_url
else:
# Get the URL of this server # Get the URL of this server
hass_url = get_url(hass, prefer_external=False) hass_url = get_url(hass, prefer_external=False)
# Override url if another is specified in the configuration
if self.custom_url is not None:
hass_url = self.custom_url
if not self.door_station_events: if not self.door_station_events:
# User may not have permission to get the favorites # User may not have permission to get the favorites
return return
@ -97,14 +97,13 @@ class ConfiguredDoorBird:
schedule: list[DoorBirdScheduleEntry] = self.device.schedule() schedule: list[DoorBirdScheduleEntry] = self.device.schedule()
http_fav: dict[str, dict[str, Any]] = favorites.get("http") or {} http_fav: dict[str, dict[str, Any]] = favorites.get("http") or {}
favorite_input_type: dict[str, str] = {
output.param: entry.input
for entry in schedule
for output in entry.output
if output.event == "http"
}
events: list[DoorbirdEvent] = [] events: list[DoorbirdEvent] = []
favorite_input_type: dict[str, str] = {}
for entry in schedule:
input_type = entry.input
for output in entry.output:
if output.event == "http":
favorite_input_type[output.param] = input_type
for identifier, data in http_fav.items(): for identifier, data in http_fav.items():
title: str | None = data.get("title") title: str | None = data.get("title")
if not title or not title.startswith("Home Assistant"): if not title or not title.startswith("Home Assistant"):
@ -157,14 +156,10 @@ class ConfiguredDoorBird:
The favorite must exist or there will be problems. The favorite must exist or there will be problems.
""" """
favs = favs if favs else self.device.favorites() favs = favs if favs else self.device.favorites()
http_fav: dict[str, dict[str, Any]] = favs.get("http") or {}
if "http" not in favs: for fav_id, data in http_fav.items():
return None if data["value"] == url:
return fav_id
for fav_id in favs["http"]:
if favs["http"][fav_id]["value"] == url:
return cast(str, fav_id)
return None return None
def get_event_data(self, event: str) -> dict[str, str | None]: def get_event_data(self, event: str) -> dict[str, str | None]: