2019-05-20 05:45:31 +00:00
|
|
|
"""Support for Axis switches."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
2024-03-16 19:47:54 +00:00
|
|
|
from collections.abc import Callable, Iterable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from functools import partial
|
2022-07-30 09:04:31 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2023-01-09 11:43:40 +00:00
|
|
|
from axis.models.event import Event, EventOperation, EventTopic
|
2019-05-20 05:45:31 +00:00
|
|
|
|
2024-03-16 19:47:54 +00:00
|
|
|
from homeassistant.components.switch import (
|
|
|
|
SwitchDeviceClass,
|
|
|
|
SwitchEntity,
|
|
|
|
SwitchEntityDescription,
|
|
|
|
)
|
2022-01-03 15:31:24 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2024-03-16 19:47:54 +00:00
|
|
|
from homeassistant.const import EntityCategory
|
2022-01-03 15:31:24 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-05-20 05:45:31 +00:00
|
|
|
|
2023-01-18 16:27:13 +00:00
|
|
|
from .entity import AxisEventEntity
|
2024-03-02 16:32:51 +00:00
|
|
|
from .hub import AxisHub
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
|
2024-03-16 19:47:54 +00:00
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
|
|
class AxisSwitchDescription(SwitchEntityDescription):
|
|
|
|
"""Axis switch entity description."""
|
|
|
|
|
|
|
|
event_topic: EventTopic
|
|
|
|
"""Event topic that provides state updates."""
|
|
|
|
name_fn: Callable[[AxisHub, Event], str]
|
|
|
|
"""Function providing the corresponding name to the event ID."""
|
|
|
|
supported_fn: Callable[[AxisHub, Event], bool]
|
|
|
|
"""Function validating if event is supported."""
|
|
|
|
|
|
|
|
|
|
|
|
ENTITY_DESCRIPTIONS = (
|
|
|
|
AxisSwitchDescription(
|
|
|
|
key="Relay state control",
|
|
|
|
device_class=SwitchDeviceClass.OUTLET,
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
event_topic=EventTopic.RELAY,
|
|
|
|
supported_fn=lambda hub, event: isinstance(int(event.id), int),
|
|
|
|
name_fn=lambda hub, event: hub.api.vapix.ports[event.id].name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-03 15:31:24 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2024-03-16 19:47:54 +00:00
|
|
|
"""Set up the Axis switch platform."""
|
2024-03-02 16:32:51 +00:00
|
|
|
hub = AxisHub.get_hub(hass, config_entry)
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
@callback
|
2024-03-16 19:47:54 +00:00
|
|
|
def register_platform(descriptions: Iterable[AxisSwitchDescription]) -> None:
|
|
|
|
"""Register entity platform to create entities on event initialized signal."""
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def create_entity(description: AxisSwitchDescription, event: Event) -> None:
|
|
|
|
"""Create Axis entity."""
|
|
|
|
if description.supported_fn(hub, event):
|
|
|
|
async_add_entities([AxisSwitch(hub, description, event)])
|
|
|
|
|
|
|
|
for description in descriptions:
|
|
|
|
hub.api.event.subscribe(
|
|
|
|
partial(create_entity, description),
|
|
|
|
topic_filter=description.event_topic,
|
|
|
|
operation_filter=EventOperation.INITIALIZED,
|
|
|
|
)
|
2023-01-09 11:43:40 +00:00
|
|
|
|
2024-03-16 19:47:54 +00:00
|
|
|
register_platform(ENTITY_DESCRIPTIONS)
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
|
2023-01-18 16:27:13 +00:00
|
|
|
class AxisSwitch(AxisEventEntity, SwitchEntity):
|
2019-05-20 05:45:31 +00:00
|
|
|
"""Representation of a Axis switch."""
|
|
|
|
|
2024-03-16 19:47:54 +00:00
|
|
|
def __init__(
|
|
|
|
self, hub: AxisHub, description: AxisSwitchDescription, event: Event
|
|
|
|
) -> None:
|
2021-06-10 17:15:01 +00:00
|
|
|
"""Initialize the Axis switch."""
|
2024-03-02 16:32:51 +00:00
|
|
|
super().__init__(event, hub)
|
2024-03-16 19:47:54 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_name = description.name_fn(hub, event) or self._attr_name
|
2023-01-18 16:27:13 +00:00
|
|
|
self._attr_is_on = event.is_tripped
|
2021-06-10 17:15:01 +00:00
|
|
|
|
2023-01-18 16:27:13 +00:00
|
|
|
@callback
|
|
|
|
def async_event_callback(self, event: Event) -> None:
|
|
|
|
"""Update light state."""
|
|
|
|
self._attr_is_on = event.is_tripped
|
|
|
|
self.async_write_ha_state()
|
2019-05-20 05:45:31 +00:00
|
|
|
|
2022-07-30 09:04:31 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2019-05-20 05:45:31 +00:00
|
|
|
"""Turn on switch."""
|
2024-03-02 16:32:51 +00:00
|
|
|
await self.hub.api.vapix.ports.close(self._event_id)
|
2019-05-20 05:45:31 +00:00
|
|
|
|
2022-07-30 09:04:31 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2019-05-20 05:45:31 +00:00
|
|
|
"""Turn off switch."""
|
2024-03-02 16:32:51 +00:00
|
|
|
await self.hub.api.vapix.ports.open(self._event_id)
|