2019-05-20 05:45:31 +00:00
|
|
|
"""Support for Axis switches."""
|
|
|
|
|
|
|
|
from axis.event_stream import CLASS_OUTPUT
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2019-05-20 05:45:31 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
|
|
|
from .axis_base import AxisEventBase
|
|
|
|
from .const import DOMAIN as AXIS_DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up a Axis switch."""
|
2020-01-04 07:58:18 +00:00
|
|
|
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_switch(event_id):
|
|
|
|
"""Add switch from Axis device."""
|
2020-05-14 08:49:27 +00:00
|
|
|
event = device.api.event[event_id]
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
if event.CLASS == CLASS_OUTPUT:
|
2020-10-16 16:54:48 +00:00
|
|
|
async_add_entities([AxisSwitch(event, device)])
|
2019-05-20 05:45:31 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
device.listeners.append(
|
2020-05-14 08:49:27 +00:00
|
|
|
async_dispatcher_connect(hass, device.signal_new_event, async_add_switch)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class AxisSwitch(AxisEventBase, SwitchEntity):
|
2019-05-20 05:45:31 +00:00
|
|
|
"""Representation of a Axis switch."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if event is active."""
|
|
|
|
return self.event.is_tripped
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Turn on switch."""
|
|
|
|
await self.hass.async_add_executor_job(
|
2020-05-31 18:00:15 +00:00
|
|
|
self.device.api.vapix.ports[self.event.id].close
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Turn off switch."""
|
|
|
|
await self.hass.async_add_executor_job(
|
2020-05-31 18:00:15 +00:00
|
|
|
self.device.api.vapix.ports[self.event.id].open
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the event."""
|
|
|
|
if self.event.id and self.device.api.vapix.ports[self.event.id].name:
|
2020-02-23 21:38:05 +00:00
|
|
|
return (
|
|
|
|
f"{self.device.name} {self.device.api.vapix.ports[self.event.id].name}"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
return super().name
|