Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 123 additions & 14 deletions homeassistant/components/reolink/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
NumberMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory, UnitOfTime
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

Expand All @@ -25,10 +26,8 @@
class ReolinkNumberEntityDescriptionMixin:
"""Mixin values for Reolink number entities."""

value: Callable[[Host, int | None], bool]
get_min_value: Callable[[Host, int | None], float]
get_max_value: Callable[[Host, int | None], float]
method: Callable[[Host, int | None, float], Any]
value: Callable[[Host, int], float]
method: Callable[[Host, int, float], Any]


@dataclass
Expand All @@ -38,7 +37,9 @@ class ReolinkNumberEntityDescription(
"""A class that describes number entities."""

mode: NumberMode = NumberMode.AUTO
supported: Callable[[Host, int | None], bool] = lambda api, ch: True
supported: Callable[[Host, int], bool] = lambda api, ch: True
get_min_value: Callable[[Host, int], float] | None = None
get_max_value: Callable[[Host, int], float] | None = None


NUMBER_ENTITIES = (
Expand All @@ -50,7 +51,7 @@ class ReolinkNumberEntityDescription(
native_step=1,
get_min_value=lambda api, ch: api.zoom_range(ch)["zoom"]["pos"]["min"],
get_max_value=lambda api, ch: api.zoom_range(ch)["zoom"]["pos"]["max"],
supported=lambda api, ch: api.zoom_supported(ch),
supported=lambda api, ch: api.supported(ch, "zoom"),
value=lambda api, ch: api.get_zoom(ch),
method=lambda api, ch, value: api.set_zoom(ch, int(value)),
),
Expand All @@ -62,10 +63,115 @@ class ReolinkNumberEntityDescription(
native_step=1,
get_min_value=lambda api, ch: api.zoom_range(ch)["focus"]["pos"]["min"],
get_max_value=lambda api, ch: api.zoom_range(ch)["focus"]["pos"]["max"],
supported=lambda api, ch: api.zoom_supported(ch),
supported=lambda api, ch: api.supported(ch, "zoom"),
value=lambda api, ch: api.get_focus(ch),
method=lambda api, ch, value: api.set_zoom(ch, int(value)),
),
ReolinkNumberEntityDescription(
key="floodlight_brightness",
name="Floodlight turn on brightness",
icon="mdi:spotlight-beam",
entity_category=EntityCategory.CONFIG,
native_step=1,
native_min_value=1,
native_max_value=100,
supported=lambda api, ch: api.supported(ch, "floodLight"),
value=lambda api, ch: api.whiteled_brightness(ch),
method=lambda api, ch, value: api.set_whiteled(ch, brightness=int(value)),
),
Comment on lines +70 to +81
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between this number entity and the light entity for the floodlight?

ReolinkLightEntityDescription(
key="floodlight",
name="Floodlight",
icon="mdi:spotlight-beam",
supported_fn=lambda api, ch: api.supported(ch, "floodLight"),
is_on_fn=lambda api, ch: api.whiteled_state(ch),
turn_on_off_fn=lambda api, ch, value: api.set_whiteled(ch, state=value),
get_brightness_fn=lambda api, ch: api.whiteled_brightness(ch),
set_brightness_fn=lambda api, ch, value: api.set_whiteled(ch, brightness=value),
),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See discussion above: #87932 (review)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read up on the comment history in the PR.

Please add a code comment explaining the situation so we don't need to wonder what's going on when reading the code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
CC @starkillerOG

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a code comment and some extra documentation because this is indeed not clear enough, just a moment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation PR is here: home-assistant/home-assistant.io#26469

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code comment PR is here: #89234

ReolinkNumberEntityDescription(
key="volume",
name="Volume",
icon="mdi:volume-high",
entity_category=EntityCategory.CONFIG,
native_step=1,
native_min_value=0,
native_max_value=100,
supported=lambda api, ch: api.supported(ch, "volume"),
value=lambda api, ch: api.volume(ch),
method=lambda api, ch, value: api.set_volume(ch, volume=int(value)),
),
ReolinkNumberEntityDescription(
key="guard_return_time",
name="Guard return time",
icon="mdi:crosshairs-gps",
entity_category=EntityCategory.CONFIG,
native_step=1,
native_unit_of_measurement=UnitOfTime.SECONDS,
native_min_value=10,
native_max_value=300,
supported=lambda api, ch: api.supported(ch, "ptz_guard"),
value=lambda api, ch: api.ptz_guard_time(ch),
method=lambda api, ch, value: api.set_ptz_guard(ch, time=int(value)),
),
ReolinkNumberEntityDescription(
key="motion_sensitivity",
name="Motion sensitivity",
icon="mdi:motion-sensor",
entity_category=EntityCategory.CONFIG,
native_step=1,
native_min_value=1,
native_max_value=50,
supported=lambda api, ch: api.supported(ch, "md_sensitivity"),
value=lambda api, ch: api.md_sensitivity(ch),
method=lambda api, ch, value: api.set_md_sensitivity(ch, int(value)),
),
ReolinkNumberEntityDescription(
key="ai_face_sensititvity",
name="AI face sensitivity",
icon="mdi:face-recognition",
entity_category=EntityCategory.CONFIG,
native_step=1,
native_min_value=0,
native_max_value=100,
supported=lambda api, ch: (
api.supported(ch, "ai_sensitivity") and api.ai_supported(ch, "face")
),
value=lambda api, ch: api.ai_sensitivity(ch, "face"),
method=lambda api, ch, value: api.set_ai_sensitivity(ch, int(value), "face"),
),
ReolinkNumberEntityDescription(
key="ai_person_sensititvity",
name="AI person sensitivity",
icon="mdi:account",
entity_category=EntityCategory.CONFIG,
native_step=1,
native_min_value=0,
native_max_value=100,
supported=lambda api, ch: (
api.supported(ch, "ai_sensitivity") and api.ai_supported(ch, "people")
),
value=lambda api, ch: api.ai_sensitivity(ch, "people"),
method=lambda api, ch, value: api.set_ai_sensitivity(ch, int(value), "people"),
),
ReolinkNumberEntityDescription(
key="ai_vehicle_sensititvity",
name="AI vehicle sensitivity",
icon="mdi:car",
entity_category=EntityCategory.CONFIG,
native_step=1,
native_min_value=0,
native_max_value=100,
supported=lambda api, ch: (
api.supported(ch, "ai_sensitivity") and api.ai_supported(ch, "vehicle")
),
value=lambda api, ch: api.ai_sensitivity(ch, "vehicle"),
method=lambda api, ch, value: api.set_ai_sensitivity(ch, int(value), "vehicle"),
),
ReolinkNumberEntityDescription(
key="ai_pet_sensititvity",
name="AI pet sensitivity",
icon="mdi:dog-side",
entity_category=EntityCategory.CONFIG,
native_step=1,
native_min_value=0,
native_max_value=100,
supported=lambda api, ch: (
api.supported(ch, "ai_sensitivity") and api.ai_supported(ch, "dog_cat")
),
value=lambda api, ch: api.ai_sensitivity(ch, "dog_cat"),
method=lambda api, ch, value: api.set_ai_sensitivity(ch, int(value), "dog_cat"),
),
)


Expand Down Expand Up @@ -100,15 +206,17 @@ def __init__(
super().__init__(reolink_data, channel)
self.entity_description = entity_description

self._attr_native_min_value = self.entity_description.get_min_value(
self._host.api, self._channel
)
self._attr_native_max_value = self.entity_description.get_max_value(
self._host.api, self._channel
)
if entity_description.get_min_value is not None:
self._attr_native_min_value = entity_description.get_min_value(
self._host.api, channel
)
if entity_description.get_max_value is not None:
self._attr_native_max_value = entity_description.get_max_value(
self._host.api, channel
)
self._attr_mode = entity_description.mode
self._attr_unique_id = (
f"{self._host.unique_id}_{self._channel}_{entity_description.key}"
f"{self._host.unique_id}_{channel}_{entity_description.key}"
)

@property
Expand All @@ -119,3 +227,4 @@ def native_value(self) -> float:
async def async_set_native_value(self, value: float) -> None:
"""Update the current value."""
await self.entity_description.method(self._host.api, self._channel, value)
self.async_write_ha_state()