Add Axis MQTT support (#36015)

* Working PoC

* Store

* Handle subscribing to MQTT and stopping stream when first telegram arrives

* Improve naming

* Now with test

* Better strings

* Fix Martins comments

* Improve mock device patching

* Bump dependency to v27
Add MQTT as after dependency
This commit is contained in:
Robert Svensson authored and GitHub committed 2020-05-25 23:13:34 +02:00
1 parent db92ffdf89
commit 376e0e0e93
5 files changed
+115 -22

No files matched your search

+50 -11
View File
@@ -5,8 +5,12 @@ import asyncio
import async_timeout
import axis
from axis.event_stream import OPERATION_INITIALIZED
from axis.streammanager import SIGNAL_PLAYING
from axis.mqtt import mqtt_json_to_event
from axis.streammanager import SIGNAL_PLAYING, STATE_STOPPED
from homeassistant.components import mqtt
from homeassistant.components.mqtt import DOMAIN as MQTT_DOMAIN
from homeassistant.components.mqtt.models import Message
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
@@ -15,10 +19,11 @@ from homeassistant.const import (
CONF_TRIGGER_TIME,
CONF_USERNAME,
)
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.setup import async_when_setup
from .const import (
ATTR_MANUFACTURER,
@@ -122,6 +127,7 @@ class AxisNetworkDevice:
async def async_new_address_callback(hass, entry):
"""Handle signals of device getting new address.
Called when config entry is updated.
This is a static method because a class method (bound method),
can not be used with weak references.
"""
@@ -142,6 +148,25 @@ class AxisNetworkDevice:
sw_version=self.fw_version,
)
async def use_mqtt(self, hass: HomeAssistant, component: str) -> None:
"""Set up to use MQTT."""
status = await hass.async_add_executor_job(
self.api.vapix.mqtt.get_client_status
)
if status.get("data", {}).get("status", {}).get("state") == "active":
self.listeners.append(
await mqtt.async_subscribe(hass, f"{self.serial}/#", self.mqtt_message)
)
@callback
def mqtt_message(self, message: Message) -> None:
"""Receive Axis MQTT message."""
self.disconnect_from_stream()
event = mqtt_json_to_event(message.payload)
self.api.event.process_event(event)
async def async_setup(self):
"""Set up the device."""
try:
@@ -173,11 +198,14 @@ class AxisNetworkDevice:
]
)
if self.option_events:
self.api.stream.connection_status_callback = (
self.api.stream.connection_status_callback.append(
self.async_connection_status_callback
)
self.api.enable_events(event_callback=self.async_event_callback)
self.api.start()
self.api.stream.start()
if self.api.vapix.mqtt:
async_when_setup(self.hass, MQTT_DOMAIN, self.use_mqtt)
self.hass.async_create_task(start_platforms())
@@ -185,14 +213,23 @@ class AxisNetworkDevice:
return True
@callback
def disconnect_from_stream(self):
"""Stop stream."""
if self.api.stream.state != STATE_STOPPED:
self.api.stream.connection_status_callback.remove(
self.async_connection_status_callback
)
self.api.stream.stop()
@callback
def shutdown(self, event):
"""Stop the event stream."""
self.api.stop()
self.disconnect_from_stream()
async def async_reset(self):
"""Reset this device to default state."""
self.api.stop()
self.disconnect_from_stream()
unload_ok = all(
await asyncio.gather(
@@ -226,11 +263,13 @@ async def get_device(hass, host, port, username, password):
try:
with async_timeout.timeout(15):
await asyncio.gather(
hass.async_add_executor_job(device.vapix.params.update_brand),
hass.async_add_executor_job(device.vapix.params.update_properties),
hass.async_add_executor_job(device.vapix.ports.update),
)
for vapix_call in (
device.vapix.initialize_api_discovery,
device.vapix.params.update_brand,
device.vapix.params.update_properties,
device.vapix.ports.update,
):
await hass.async_add_executor_job(vapix_call)
return device