Fix onvif cameras with invalid encodings in device info (#92450)

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
pull/92495/head
J. Nick Koston 2023-05-04 06:48:13 -05:00 committed by GitHub
parent 95023ecf08
commit 41515249a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 6 deletions

View File

@ -12,7 +12,7 @@ from httpx import RequestError
import onvif import onvif
from onvif import ONVIFCamera from onvif import ONVIFCamera
from onvif.exceptions import ONVIFError from onvif.exceptions import ONVIFError
from zeep.exceptions import Fault, TransportError, XMLParseError from zeep.exceptions import Fault, TransportError, XMLParseError, XMLSyntaxError
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -286,7 +286,21 @@ class ONVIFDevice:
async def async_get_device_info(self) -> DeviceInfo: async def async_get_device_info(self) -> DeviceInfo:
"""Obtain information about this device.""" """Obtain information about this device."""
device_mgmt = self.device.create_devicemgmt_service() device_mgmt = self.device.create_devicemgmt_service()
device_info = await device_mgmt.GetDeviceInformation() manufacturer = None
model = None
firmware_version = None
serial_number = None
try:
device_info = await device_mgmt.GetDeviceInformation()
except (XMLParseError, XMLSyntaxError, TransportError) as ex:
# Some cameras have invalid UTF-8 in their device information (TransportError)
# and others have completely invalid XML (XMLParseError, XMLSyntaxError)
LOGGER.warning("%s: Failed to fetch device information: %s", self.name, ex)
else:
manufacturer = device_info.Manufacturer
model = device_info.Model
firmware_version = device_info.FirmwareVersion
serial_number = device_info.SerialNumber
# Grab the last MAC address for backwards compatibility # Grab the last MAC address for backwards compatibility
mac = None mac = None
@ -306,10 +320,10 @@ class ONVIFDevice:
) )
return DeviceInfo( return DeviceInfo(
device_info.Manufacturer, manufacturer,
device_info.Model, model,
device_info.FirmwareVersion, firmware_version,
device_info.SerialNumber, serial_number,
mac, mac,
) )