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
|
2022-01-03 15:31:24 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2019-05-20 05:45:31 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2022-01-03 15:31:24 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
from .axis_base import AxisEventBase
|
|
|
|
from .const import DOMAIN as AXIS_DOMAIN
|
|
|
|
|
|
|
|
|
2022-01-03 15:31:24 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-05-20 05:45:31 +00:00
|
|
|
"""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
|
|
|
|
2021-04-20 18:53:05 +00:00
|
|
|
config_entry.async_on_unload(
|
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."""
|
|
|
|
|
2021-06-10 17:15:01 +00:00
|
|
|
def __init__(self, event, device):
|
|
|
|
"""Initialize the Axis switch."""
|
|
|
|
super().__init__(event, device)
|
|
|
|
|
|
|
|
if event.id and device.api.vapix.ports[event.id].name:
|
|
|
|
self._attr_name = f"{device.name} {device.api.vapix.ports[event.id].name}"
|
|
|
|
|
2019-05-20 05:45:31 +00:00
|
|
|
@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."""
|
2020-10-20 07:31:04 +00:00
|
|
|
await self.device.api.vapix.ports[self.event.id].close()
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Turn off switch."""
|
2020-10-20 07:31:04 +00:00
|
|
|
await self.device.api.vapix.ports[self.event.id].open()
|