mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 01:07:10 +00:00
Migrate doorbird to use new logbook platform (#37097)
This commit is contained in:
parent
2dd0a182ab
commit
6d9fa34f65
@ -9,7 +9,6 @@ from doorbirdpy import DoorBird
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.http import HomeAssistantView
|
from homeassistant.components.http import HomeAssistantView
|
||||||
from homeassistant.components.logbook import log_entry
|
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_DEVICES,
|
CONF_DEVICES,
|
||||||
@ -165,6 +164,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
|
|
||||||
unload_ok = all(
|
unload_ok = all(
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
*[
|
*[
|
||||||
@ -228,6 +228,7 @@ class ConfiguredDoorBird:
|
|||||||
self._device = device
|
self._device = device
|
||||||
self._custom_url = custom_url
|
self._custom_url = custom_url
|
||||||
self.events = events
|
self.events = events
|
||||||
|
self.doorstation_events = [self._get_event_name(event) for event in self.events]
|
||||||
self._token = token
|
self._token = token
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -259,9 +260,7 @@ class ConfiguredDoorBird:
|
|||||||
if self.custom_url is not None:
|
if self.custom_url is not None:
|
||||||
hass_url = self.custom_url
|
hass_url = self.custom_url
|
||||||
|
|
||||||
for event in self.events:
|
for event in self.doorstation_events:
|
||||||
event = self._get_event_name(event)
|
|
||||||
|
|
||||||
self._register_event(hass_url, event)
|
self._register_event(hass_url, event)
|
||||||
|
|
||||||
_LOGGER.info("Successfully registered URL for %s on %s", event, self.name)
|
_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)
|
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")
|
return web.Response(status=HTTP_OK, text="OK")
|
||||||
|
@ -10,7 +10,12 @@ from homeassistant.components.camera import SUPPORT_STREAM, Camera
|
|||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.util.dt as dt_util
|
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
|
from .entity import DoorBirdEntity
|
||||||
|
|
||||||
_LAST_VISITOR_INTERVAL = datetime.timedelta(minutes=2)
|
_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):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up the DoorBird camera platform."""
|
"""Set up the DoorBird camera platform."""
|
||||||
config_entry_id = config_entry.entry_id
|
config_entry_id = config_entry.entry_id
|
||||||
doorstation = hass.data[DOMAIN][config_entry_id][DOOR_STATION]
|
config_data = hass.data[DOMAIN][config_entry_id]
|
||||||
doorstation_info = hass.data[DOMAIN][config_entry_id][DOOR_STATION_INFO]
|
doorstation = config_data[DOOR_STATION]
|
||||||
|
doorstation_info = config_data[DOOR_STATION_INFO]
|
||||||
device = doorstation.device
|
device = doorstation.device
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
@ -35,6 +41,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
device.live_image_url,
|
device.live_image_url,
|
||||||
"live",
|
"live",
|
||||||
f"{doorstation.name} Live",
|
f"{doorstation.name} Live",
|
||||||
|
doorstation.doorstation_events,
|
||||||
_LIVE_INTERVAL,
|
_LIVE_INTERVAL,
|
||||||
device.rtsp_live_video_url,
|
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"),
|
device.history_image_url(1, "doorbell"),
|
||||||
"last_ring",
|
"last_ring",
|
||||||
f"{doorstation.name} Last Ring",
|
f"{doorstation.name} Last Ring",
|
||||||
|
[],
|
||||||
_LAST_VISITOR_INTERVAL,
|
_LAST_VISITOR_INTERVAL,
|
||||||
),
|
),
|
||||||
DoorBirdCamera(
|
DoorBirdCamera(
|
||||||
@ -52,6 +60,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
device.history_image_url(1, "motionsensor"),
|
device.history_image_url(1, "motionsensor"),
|
||||||
"last_motion",
|
"last_motion",
|
||||||
f"{doorstation.name} Last Motion",
|
f"{doorstation.name} Last Motion",
|
||||||
|
[],
|
||||||
_LAST_MOTION_INTERVAL,
|
_LAST_MOTION_INTERVAL,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
@ -68,6 +77,7 @@ class DoorBirdCamera(DoorBirdEntity, Camera):
|
|||||||
url,
|
url,
|
||||||
camera_id,
|
camera_id,
|
||||||
name,
|
name,
|
||||||
|
doorstation_events,
|
||||||
interval=None,
|
interval=None,
|
||||||
stream_url=None,
|
stream_url=None,
|
||||||
):
|
):
|
||||||
@ -81,6 +91,7 @@ class DoorBirdCamera(DoorBirdEntity, Camera):
|
|||||||
self._interval = interval or datetime.timedelta
|
self._interval = interval or datetime.timedelta
|
||||||
self._last_update = datetime.datetime.min
|
self._last_update = datetime.datetime.min
|
||||||
self._unique_id = f"{self._mac_addr}_{camera_id}"
|
self._unique_id = f"{self._mac_addr}_{camera_id}"
|
||||||
|
self._doorstation_events = doorstation_events
|
||||||
|
|
||||||
async def stream_source(self):
|
async def stream_source(self):
|
||||||
"""Return the stream source."""
|
"""Return the stream source."""
|
||||||
@ -124,3 +135,21 @@ class DoorBirdCamera(DoorBirdEntity, Camera):
|
|||||||
"DoorBird %s: Error getting camera image: %s", self._name, error
|
"DoorBird %s: Error getting camera image: %s", self._name, error
|
||||||
)
|
)
|
||||||
return self._last_image
|
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]
|
||||||
|
@ -5,6 +5,8 @@ DOMAIN = "doorbird"
|
|||||||
PLATFORMS = ["switch", "camera"]
|
PLATFORMS = ["switch", "camera"]
|
||||||
DOOR_STATION = "door_station"
|
DOOR_STATION = "door_station"
|
||||||
DOOR_STATION_INFO = "door_station_info"
|
DOOR_STATION_INFO = "door_station_info"
|
||||||
|
DOOR_STATION_EVENT_ENTITY_IDS = "door_station_event_entity_ids"
|
||||||
|
|
||||||
CONF_EVENTS = "events"
|
CONF_EVENTS = "events"
|
||||||
MANUFACTURER = "Bird Home Automation Group"
|
MANUFACTURER = "Bird Home Automation Group"
|
||||||
DOORBIRD_OUI = "1CCAE3"
|
DOORBIRD_OUI = "1CCAE3"
|
||||||
|
33
homeassistant/components/doorbird/logbook.py
Normal file
33
homeassistant/components/doorbird/logbook.py
Normal 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
|
||||||
|
)
|
@ -3,7 +3,7 @@
|
|||||||
"name": "DoorBird",
|
"name": "DoorBird",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/doorbird",
|
"documentation": "https://www.home-assistant.io/integrations/doorbird",
|
||||||
"requirements": ["doorbirdpy==2.0.8"],
|
"requirements": ["doorbirdpy==2.0.8"],
|
||||||
"dependencies": ["http", "logbook"],
|
"dependencies": ["http"],
|
||||||
"zeroconf": ["_axis-video._tcp.local."],
|
"zeroconf": ["_axis-video._tcp.local."],
|
||||||
"codeowners": ["@oblogic7", "@bdraco"],
|
"codeowners": ["@oblogic7", "@bdraco"],
|
||||||
"config_flow": true
|
"config_flow": true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user