Automatically add newly added devices for UniFi Protect (#73879)

This commit is contained in:
Christopher Bailey 2022-06-27 17:03:25 -04:00 committed by GitHub
parent 33f5b225fb
commit b9c636ba4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 696 additions and 76 deletions

View file

@ -11,6 +11,7 @@ from pyunifiprotect.data import (
Event,
Light,
MountType,
ProtectAdoptableDeviceModel,
ProtectModelWithId,
Sensor,
)
@ -23,10 +24,11 @@ from homeassistant.components.binary_sensor import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .const import DISPATCH_ADOPT, DOMAIN
from .data import ProtectData
from .entity import (
EventThumbnailMixin,
@ -35,6 +37,7 @@ from .entity import (
async_all_device_entities,
)
from .models import PermRequired, ProtectRequiredKeysMixin
from .utils import async_dispatch_id as _ufpd
_LOGGER = logging.getLogger(__name__)
_KEY_DOOR = "door"
@ -364,6 +367,24 @@ async def async_setup_entry(
) -> None:
"""Set up binary sensors for UniFi Protect integration."""
data: ProtectData = hass.data[DOMAIN][entry.entry_id]
async def _add_new_device(device: ProtectAdoptableDeviceModel) -> None:
entities: list[ProtectDeviceEntity] = async_all_device_entities(
data,
ProtectDeviceBinarySensor,
camera_descs=CAMERA_SENSORS,
light_descs=LIGHT_SENSORS,
sense_descs=SENSE_SENSORS,
lock_descs=DOORLOCK_SENSORS,
viewer_descs=VIEWER_SENSORS,
ufp_device=device,
)
if device.is_adopted and isinstance(device, Camera):
entities += _async_motion_entities(data, ufp_device=device)
async_add_entities(entities)
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device)
entities: list[ProtectDeviceEntity] = async_all_device_entities(
data,
ProtectDeviceBinarySensor,
@ -382,10 +403,14 @@ async def async_setup_entry(
@callback
def _async_motion_entities(
data: ProtectData,
ufp_device: ProtectAdoptableDeviceModel | None = None,
) -> list[ProtectDeviceEntity]:
entities: list[ProtectDeviceEntity] = []
for device in data.api.bootstrap.cameras.values():
if not device.is_adopted_by_us:
devices = (
data.api.bootstrap.cameras.values() if ufp_device is None else [ufp_device]
)
for device in devices:
if not device.is_adopted:
continue
for description in MOTION_SENSORS: