Add Reolink chime support (#122752)
This commit is contained in:
parent
f764705629
commit
8c0d9a1320
13 changed files with 549 additions and 54 deletions
|
@ -6,7 +6,7 @@ from collections.abc import Callable
|
|||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from reolink_aio.api import Host
|
||||
from reolink_aio.api import Chime, Host
|
||||
from reolink_aio.exceptions import InvalidParameterError, ReolinkError
|
||||
|
||||
from homeassistant.components.number import (
|
||||
|
@ -22,7 +22,11 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
|
||||
from . import ReolinkData
|
||||
from .const import DOMAIN
|
||||
from .entity import ReolinkChannelCoordinatorEntity, ReolinkChannelEntityDescription
|
||||
from .entity import (
|
||||
ReolinkChannelCoordinatorEntity,
|
||||
ReolinkChannelEntityDescription,
|
||||
ReolinkChimeCoordinatorEntity,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
|
@ -39,6 +43,18 @@ class ReolinkNumberEntityDescription(
|
|||
value: Callable[[Host, int], float | None]
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class ReolinkChimeNumberEntityDescription(
|
||||
NumberEntityDescription,
|
||||
ReolinkChannelEntityDescription,
|
||||
):
|
||||
"""A class that describes number entities for a chime."""
|
||||
|
||||
method: Callable[[Chime, float], Any]
|
||||
mode: NumberMode = NumberMode.AUTO
|
||||
value: Callable[[Chime], float | None]
|
||||
|
||||
|
||||
NUMBER_ENTITIES = (
|
||||
ReolinkNumberEntityDescription(
|
||||
key="zoom",
|
||||
|
@ -459,6 +475,20 @@ NUMBER_ENTITIES = (
|
|||
),
|
||||
)
|
||||
|
||||
CHIME_NUMBER_ENTITIES = (
|
||||
ReolinkChimeNumberEntityDescription(
|
||||
key="volume",
|
||||
cmd_key="DingDongOpt",
|
||||
translation_key="volume",
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
native_step=1,
|
||||
native_min_value=0,
|
||||
native_max_value=4,
|
||||
value=lambda chime: chime.volume,
|
||||
method=lambda chime, value: chime.set_option(volume=int(value)),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
|
@ -468,12 +498,18 @@ async def async_setup_entry(
|
|||
"""Set up a Reolink number entities."""
|
||||
reolink_data: ReolinkData = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
async_add_entities(
|
||||
entities: list[ReolinkNumberEntity | ReolinkChimeNumberEntity] = [
|
||||
ReolinkNumberEntity(reolink_data, channel, entity_description)
|
||||
for entity_description in NUMBER_ENTITIES
|
||||
for channel in reolink_data.host.api.channels
|
||||
if entity_description.supported(reolink_data.host.api, channel)
|
||||
]
|
||||
entities.extend(
|
||||
ReolinkChimeNumberEntity(reolink_data, chime, entity_description)
|
||||
for entity_description in CHIME_NUMBER_ENTITIES
|
||||
for chime in reolink_data.host.api.chime_list
|
||||
)
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class ReolinkNumberEntity(ReolinkChannelCoordinatorEntity, NumberEntity):
|
||||
|
@ -515,3 +551,36 @@ class ReolinkNumberEntity(ReolinkChannelCoordinatorEntity, NumberEntity):
|
|||
except ReolinkError as err:
|
||||
raise HomeAssistantError(err) from err
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class ReolinkChimeNumberEntity(ReolinkChimeCoordinatorEntity, NumberEntity):
|
||||
"""Base number entity class for Reolink IP cameras."""
|
||||
|
||||
entity_description: ReolinkChimeNumberEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
reolink_data: ReolinkData,
|
||||
chime: Chime,
|
||||
entity_description: ReolinkChimeNumberEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize Reolink chime number entity."""
|
||||
self.entity_description = entity_description
|
||||
super().__init__(reolink_data, chime)
|
||||
|
||||
self._attr_mode = entity_description.mode
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
"""State of the number entity."""
|
||||
return self.entity_description.value(self._chime)
|
||||
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Update the current value."""
|
||||
try:
|
||||
await self.entity_description.method(self._chime, value)
|
||||
except InvalidParameterError as err:
|
||||
raise ServiceValidationError(err) from err
|
||||
except ReolinkError as err:
|
||||
raise HomeAssistantError(err) from err
|
||||
self.async_write_ha_state()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue