Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webhook support to onvif #91485

Merged
merged 1 commit into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions homeassistant/components/onvif/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up a ONVIF binary sensor."""
device = hass.data[DOMAIN][config_entry.unique_id]
device: ONVIFDevice = hass.data[DOMAIN][config_entry.unique_id]

entities = {
event.uid: ONVIFBinarySensor(event.uid, device)
Expand All @@ -39,16 +39,20 @@ async def async_setup_entry(
)

async_add_entities(entities.values())
uids_by_platform = device.events.get_uids_by_platform("binary_sensor")

@callback
def async_check_entities():
def async_check_entities() -> None:
"""Check if we have added an entity for the event."""
new_entities = []
for event in device.events.get_platform("binary_sensor"):
if event.uid not in entities:
entities[event.uid] = ONVIFBinarySensor(event.uid, device)
new_entities.append(entities[event.uid])
async_add_entities(new_entities)
nonlocal uids_by_platform
if not (missing := uids_by_platform.difference(entities)):
return
new_entities: dict[str, ONVIFBinarySensor] = {
uid: ONVIFBinarySensor(uid, device) for uid in missing
}
if new_entities:
entities.update(new_entities)
async_add_entities(new_entities.values())

device.events.async_add_listener(async_check_entities)

Expand Down
25 changes: 14 additions & 11 deletions homeassistant/components/onvif/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async def async_setup(self) -> None:

# Create event manager
assert self.config_entry.unique_id
self.events = EventManager(self.hass, self.device, self.config_entry.unique_id)
self.events = EventManager(self.hass, self.device, self.config_entry, self.name)

# Fetch basic device info and capabilities
self.info = await self.async_get_device_info()
Expand Down Expand Up @@ -159,10 +159,10 @@ async def async_manually_set_date_and_time(self) -> None:

async def async_check_date_and_time(self) -> None:
"""Warns if device and system date not synced."""
LOGGER.debug("Setting up the ONVIF device management service")
LOGGER.debug("%s: Setting up the ONVIF device management service", self.name)
device_mgmt = self.device.create_devicemgmt_service()

LOGGER.debug("Retrieving current device date/time")
LOGGER.debug("%s: Retrieving current device date/time", self.name)
try:
system_date = dt_util.utcnow()
device_time = await device_mgmt.GetSystemDateAndTime()
Expand All @@ -174,7 +174,7 @@ async def async_check_date_and_time(self) -> None:
)
return

LOGGER.debug("Device time: %s", device_time)
LOGGER.debug("%s: Device time: %s", self.name, device_time)

tzone = dt_util.DEFAULT_TIME_ZONE
cdate = device_time.LocalDateTime
Expand All @@ -185,7 +185,9 @@ async def async_check_date_and_time(self) -> None:
tzone = dt_util.get_time_zone(device_time.TimeZone.TZ) or tzone

if cdate is None:
LOGGER.warning("Could not retrieve date/time on this camera")
LOGGER.warning(
"%s: Could not retrieve date/time on this camera", self.name
)
else:
cam_date = dt.datetime(
cdate.Date.Year,
Expand All @@ -201,7 +203,8 @@ async def async_check_date_and_time(self) -> None:
cam_date_utc = cam_date.astimezone(dt_util.UTC)

LOGGER.debug(
"Device date/time: %s | System date/time: %s",
"%s: Device date/time: %s | System date/time: %s",
self.name,
cam_date_utc,
system_date,
)
Expand Down Expand Up @@ -266,10 +269,6 @@ async def async_get_capabilities(self):
media_capabilities = await media_service.GetServiceCapabilities()
snapshot = media_capabilities and media_capabilities.SnapshotUri

pullpoint = False
with suppress(ONVIFError, Fault, RequestError, XMLParseError):
pullpoint = await self.events.async_start()

ptz = False
with suppress(ONVIFError, Fault, RequestError):
self.device.get_definition("ptz")
Expand All @@ -280,7 +279,11 @@ async def async_get_capabilities(self):
self.device.create_imaging_service()
imaging = True

return Capabilities(snapshot, pullpoint, ptz, imaging)
events = False
with suppress(ONVIFError, Fault, RequestError, XMLParseError):
events = await self.events.async_start()

return Capabilities(snapshot, events, ptz, imaging)

async def async_get_profiles(self) -> list[Profile]:
"""Obtain media profiles for this device."""
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/onvif/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ async def async_get_config_entry_diagnostics(
"capabilities": asdict(device.capabilities),
"profiles": [asdict(profile) for profile in device.profiles],
}
data["events"] = {
"webhook_manager_state": device.events.webhook_manager.state,
"pullpoint_manager_state": device.events.pullpoint_manager.state,
}

return data
Loading