1
mirror of https://github.com/home-assistant/core synced 2024-07-30 21:18:57 +02:00

Migrate doorbird to use new logbook platform (#37097)

This commit is contained in:
J. Nick Koston 2020-06-25 15:17:05 -05:00 committed by GitHub
parent 2dd0a182ab
commit 6d9fa34f65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 10 deletions

View File

@ -9,7 +9,6 @@ from doorbirdpy import DoorBird
import voluptuous as vol
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.logbook import log_entry
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
CONF_DEVICES,
@ -165,6 +164,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
@ -228,6 +228,7 @@ class ConfiguredDoorBird:
self._device = device
self._custom_url = custom_url
self.events = events
self.doorstation_events = [self._get_event_name(event) for event in self.events]
self._token = token
@property
@ -259,9 +260,7 @@ class ConfiguredDoorBird:
if self.custom_url is not None:
hass_url = self.custom_url
for event in self.events:
event = self._get_event_name(event)
for event in self.doorstation_events:
self._register_event(hass_url, event)
_LOGGER.info("Successfully registered URL for %s on %s", event, self.name)
@ -365,6 +364,4 @@ class DoorBirdRequestView(HomeAssistantView):
hass.bus.async_fire(f"{DOMAIN}_{event}", event_data)
log_entry(hass, f"Doorbird {event}", "event was fired.", DOMAIN)
return web.Response(status=HTTP_OK, text="OK")

View File

@ -10,7 +10,12 @@ from homeassistant.components.camera import SUPPORT_STREAM, Camera
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.util.dt as dt_util
from .const import DOMAIN, DOOR_STATION, DOOR_STATION_INFO
from .const import (
DOMAIN,
DOOR_STATION,
DOOR_STATION_EVENT_ENTITY_IDS,
DOOR_STATION_INFO,
)
from .entity import DoorBirdEntity
_LAST_VISITOR_INTERVAL = datetime.timedelta(minutes=2)
@ -23,8 +28,9 @@ _TIMEOUT = 15 # seconds
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the DoorBird camera platform."""
config_entry_id = config_entry.entry_id
doorstation = hass.data[DOMAIN][config_entry_id][DOOR_STATION]
doorstation_info = hass.data[DOMAIN][config_entry_id][DOOR_STATION_INFO]
config_data = hass.data[DOMAIN][config_entry_id]
doorstation = config_data[DOOR_STATION]
doorstation_info = config_data[DOOR_STATION_INFO]
device = doorstation.device
async_add_entities(
@ -35,6 +41,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
device.live_image_url,
"live",
f"{doorstation.name} Live",
doorstation.doorstation_events,
_LIVE_INTERVAL,
device.rtsp_live_video_url,
),
@ -44,6 +51,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
device.history_image_url(1, "doorbell"),
"last_ring",
f"{doorstation.name} Last Ring",
[],
_LAST_VISITOR_INTERVAL,
),
DoorBirdCamera(
@ -52,6 +60,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
device.history_image_url(1, "motionsensor"),
"last_motion",
f"{doorstation.name} Last Motion",
[],
_LAST_MOTION_INTERVAL,
),
]
@ -68,6 +77,7 @@ class DoorBirdCamera(DoorBirdEntity, Camera):
url,
camera_id,
name,
doorstation_events,
interval=None,
stream_url=None,
):
@ -81,6 +91,7 @@ class DoorBirdCamera(DoorBirdEntity, Camera):
self._interval = interval or datetime.timedelta
self._last_update = datetime.datetime.min
self._unique_id = f"{self._mac_addr}_{camera_id}"
self._doorstation_events = doorstation_events
async def stream_source(self):
"""Return the stream source."""
@ -124,3 +135,21 @@ class DoorBirdCamera(DoorBirdEntity, Camera):
"DoorBird %s: Error getting camera image: %s", self._name, error
)
return self._last_image
async def async_added_to_hass(self):
"""Add callback after being added to hass.
Registers entity_id map for the logbook
"""
event_to_entity_id = self.hass.data[DOMAIN].setdefault(
DOOR_STATION_EVENT_ENTITY_IDS, {}
)
for event in self._doorstation_events:
event_to_entity_id[event] = self.entity_id
async def will_remove_from_hass(self):
"""Unregister entity_id map for the logbook."""
event_to_entity_id = self.hass.data[DOMAIN][DOOR_STATION_EVENT_ENTITY_IDS]
for event in self._doorstation_events:
if event in event_to_entity_id:
del event_to_entity_id[event]

View File

@ -5,6 +5,8 @@ DOMAIN = "doorbird"
PLATFORMS = ["switch", "camera"]
DOOR_STATION = "door_station"
DOOR_STATION_INFO = "door_station_info"
DOOR_STATION_EVENT_ENTITY_IDS = "door_station_event_entity_ids"
CONF_EVENTS = "events"
MANUFACTURER = "Bird Home Automation Group"
DOORBIRD_OUI = "1CCAE3"

View File

@ -0,0 +1,33 @@
"""Describe logbook events."""
from homeassistant.core import callback
from .const import DOMAIN, DOOR_STATION, DOOR_STATION_EVENT_ENTITY_IDS
@callback
def async_describe_events(hass, async_describe_event):
"""Describe logbook events."""
@callback
def async_describe_logbook_event(event):
"""Describe a logbook event."""
_, doorbird_event = event.event_type.split("_", 1)
return {
"name": "Doorbird",
"message": f"Event {event.event_type} was fired.",
"entity_id": hass.data[DOMAIN][DOOR_STATION_EVENT_ENTITY_IDS].get(
doorbird_event
),
}
domain_data = hass.data[DOMAIN]
for config_entry_id in domain_data:
door_station = domain_data[config_entry_id][DOOR_STATION]
for event in door_station.doorstation_events:
async_describe_event(
DOMAIN, f"{DOMAIN}_{event}", async_describe_logbook_event
)

View File

@ -3,7 +3,7 @@
"name": "DoorBird",
"documentation": "https://www.home-assistant.io/integrations/doorbird",
"requirements": ["doorbirdpy==2.0.8"],
"dependencies": ["http", "logbook"],
"dependencies": ["http"],
"zeroconf": ["_axis-video._tcp.local."],
"codeowners": ["@oblogic7", "@bdraco"],
"config_flow": true