Axis use entity description in switch platform (#113595)
* Draft * Make a generic register platform
This commit is contained in:
parent
00361f5293
commit
c57dcacade
1 changed files with 55 additions and 13 deletions
|
@ -1,11 +1,19 @@
|
||||||
"""Support for Axis switches."""
|
"""Support for Axis switches."""
|
||||||
|
|
||||||
|
from collections.abc import Callable, Iterable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from functools import partial
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from axis.models.event import Event, EventOperation, EventTopic
|
from axis.models.event import Event, EventOperation, EventTopic
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity
|
from homeassistant.components.switch import (
|
||||||
|
SwitchDeviceClass,
|
||||||
|
SwitchEntity,
|
||||||
|
SwitchEntityDescription,
|
||||||
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
@ -13,34 +21,68 @@ from .entity import AxisEventEntity
|
||||||
from .hub import AxisHub
|
from .hub import AxisHub
|
||||||
|
|
||||||
|
|
||||||
|
@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,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a Axis switch."""
|
"""Set up the Axis switch platform."""
|
||||||
hub = AxisHub.get_hub(hass, config_entry)
|
hub = AxisHub.get_hub(hass, config_entry)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_create_entity(event: Event) -> None:
|
def register_platform(descriptions: Iterable[AxisSwitchDescription]) -> None:
|
||||||
"""Create Axis switch entity."""
|
"""Register entity platform to create entities on event initialized signal."""
|
||||||
async_add_entities([AxisSwitch(event, hub)])
|
|
||||||
|
|
||||||
hub.api.event.subscribe(
|
@callback
|
||||||
async_create_entity,
|
def create_entity(description: AxisSwitchDescription, event: Event) -> None:
|
||||||
topic_filter=EventTopic.RELAY,
|
"""Create Axis entity."""
|
||||||
operation_filter=EventOperation.INITIALIZED,
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
|
register_platform(ENTITY_DESCRIPTIONS)
|
||||||
|
|
||||||
|
|
||||||
class AxisSwitch(AxisEventEntity, SwitchEntity):
|
class AxisSwitch(AxisEventEntity, SwitchEntity):
|
||||||
"""Representation of a Axis switch."""
|
"""Representation of a Axis switch."""
|
||||||
|
|
||||||
def __init__(self, event: Event, hub: AxisHub) -> None:
|
def __init__(
|
||||||
|
self, hub: AxisHub, description: AxisSwitchDescription, event: Event
|
||||||
|
) -> None:
|
||||||
"""Initialize the Axis switch."""
|
"""Initialize the Axis switch."""
|
||||||
super().__init__(event, hub)
|
super().__init__(event, hub)
|
||||||
if event.id and hub.api.vapix.ports[event.id].name:
|
self.entity_description = description
|
||||||
self._attr_name = hub.api.vapix.ports[event.id].name
|
self._attr_name = description.name_fn(hub, event) or self._attr_name
|
||||||
self._attr_is_on = event.is_tripped
|
self._attr_is_on = event.is_tripped
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue