Skip to content

Commit

Permalink
Fix ONVIF Transport (home-assistant#35932)
Browse files Browse the repository at this point in the history
* allow lib to create AsyncTransport

* fix transport close issue
  • Loading branch information
hunterjm authored and frenck committed May 24, 2020
1 parent f452e26 commit 8a9ba7e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
3 changes: 2 additions & 1 deletion homeassistant/components/onvif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
16 changes: 9 additions & 7 deletions homeassistant/components/onvif/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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
Expand All @@ -20,7 +19,6 @@
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 (
Expand Down Expand Up @@ -141,6 +139,12 @@ async def async_setup(self) -> bool:

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")
Expand Down Expand Up @@ -278,7 +282,7 @@ async def async_get_profiles(self) -> List[Profile]:
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]

Expand Down Expand Up @@ -326,7 +330,7 @@ async def async_perform_ptz(
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)
Expand Down Expand Up @@ -423,13 +427,11 @@ async def async_perform_ptz(

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,
)
4 changes: 2 additions & 2 deletions homeassistant/components/onvif/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def async_start(self) -> bool:

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
Expand All @@ -110,7 +110,7 @@ async def async_renew(self) -> None:
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)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/onvif/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8a9ba7e

Please sign in to comment.