Fix ONVIF Transport (#35932)

* allow lib to create AsyncTransport

* fix transport close issue
pull/36070/head
Jason Hunter 2020-05-22 19:46:11 -04:00 committed by Franck Nijhof
parent f452e26269
commit 8a9ba7e72d
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
6 changed files with 16 additions and 13 deletions

View File

@ -82,13 +82,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
if device.capabilities.events and await device.events.async_start():
platforms += ["binary_sensor", "sensor"]
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, device.events.async_stop)
for component in platforms:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, component)
)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, device.async_stop)
return True

View File

@ -8,7 +8,6 @@ from aiohttp.client_exceptions import ClientConnectionError, ServerDisconnectedE
import onvif
from onvif import ONVIFCamera
from onvif.exceptions import ONVIFError
from zeep.asyncio import AsyncTransport
from zeep.exceptions import Fault
from homeassistant.config_entries import ConfigEntry
@ -20,7 +19,6 @@ from homeassistant.const import (
CONF_USERNAME,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.util.dt as dt_util
from .const import (
@ -141,6 +139,12 @@ class ONVIFDevice:
return True
async def async_stop(self, event=None):
"""Shut it all down."""
if self.events:
await self.events.async_stop()
await self.device.close()
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")
@ -278,7 +282,7 @@ class ONVIFDevice:
is not None,
)
ptz_service = self.device.get_service("ptz")
ptz_service = self.device.create_ptz_service()
presets = await ptz_service.GetPresets(profile.token)
profile.ptz.presets = [preset.token for preset in presets]
@ -326,7 +330,7 @@ class ONVIFDevice:
LOGGER.warning("PTZ actions are not supported on device '%s'", self.name)
return
ptz_service = self.device.get_service("ptz")
ptz_service = self.device.create_ptz_service()
pan_val = distance * PAN_FACTOR.get(pan, 0)
tilt_val = distance * TILT_FACTOR.get(tilt, 0)
@ -423,13 +427,11 @@ class ONVIFDevice:
def get_device(hass, host, port, username, password) -> ONVIFCamera:
"""Get ONVIFCamera instance."""
session = async_get_clientsession(hass)
transport = AsyncTransport(None, session=session)
return ONVIFCamera(
host,
port,
username,
password,
f"{os.path.dirname(onvif.__file__)}/wsdl/",
transport=transport,
no_cache=True,
)

View File

@ -91,7 +91,7 @@ class EventManager:
return self.started
async def async_stop(self, event=None) -> None:
async def async_stop(self) -> None:
"""Unsubscribe from events."""
if not self._subscription:
return
@ -110,7 +110,7 @@ class EventManager:
async def async_pull_messages(self, _now: dt = None) -> None:
"""Pull messages from device."""
try:
pullpoint = self.device.get_service("pullpoint")
pullpoint = self.device.create_pullpoint_service()
req = pullpoint.create_type("PullMessages")
req.MessageLimit = 100
req.Timeout = dt.timedelta(seconds=60)

View File

@ -2,7 +2,7 @@
"domain": "onvif",
"name": "ONVIF",
"documentation": "https://www.home-assistant.io/integrations/onvif",
"requirements": ["onvif-zeep-async==0.3.0", "WSDiscovery==2.0.0"],
"requirements": ["onvif-zeep-async==0.4.0", "WSDiscovery==2.0.0"],
"dependencies": ["ffmpeg"],
"codeowners": ["@hunterjm"],
"config_flow": true

View File

@ -994,7 +994,7 @@ oemthermostat==1.1
onkyo-eiscp==1.2.7
# homeassistant.components.onvif
onvif-zeep-async==0.3.0
onvif-zeep-async==0.4.0
# homeassistant.components.opengarage
open-garage==0.1.4

View File

@ -414,7 +414,7 @@ numpy==1.18.4
oauth2client==4.0.0
# homeassistant.components.onvif
onvif-zeep-async==0.3.0
onvif-zeep-async==0.4.0
# homeassistant.components.openerz
openerz-api==0.1.0