Skip to content
Merged
Changes from 11 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
109 changes: 102 additions & 7 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 UnitOfTime
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

Expand All @@ -25,10 +26,10 @@
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]
get_min_value: Callable[[Host, int], float]
get_max_value: Callable[[Host, int], float]
method: Callable[[Host, int, float], Any]


@dataclass
Expand All @@ -38,7 +39,7 @@ 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


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,103 @@ 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 brightness",
Comment thread
frenck marked this conversation as resolved.
Outdated
icon="mdi:spotlight-beam",
native_step=1,
get_min_value=lambda api, ch: 1,
get_max_value=lambda api, ch: 100,
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
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",
native_step=1,
get_min_value=lambda api, ch: 0,
get_max_value=lambda api, ch: 100,
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
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",
native_step=1,
native_unit_of_measurement=UnitOfTime.SECONDS,
get_min_value=lambda api, ch: 10,
get_max_value=lambda api, ch: 300,
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
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",
native_step=1,
get_min_value=lambda api, ch: 1,
get_max_value=lambda api, ch: 50,
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
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",
native_step=1,
get_min_value=lambda api, ch: 0,
get_max_value=lambda api, ch: 100,
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
supported=lambda api, ch: api.supported(ch, "ai_sensitivity")
and api.ai_supported(ch, "face"),
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
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",
native_step=1,
get_min_value=lambda api, ch: 0,
get_max_value=lambda api, ch: 100,
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
supported=lambda api, ch: api.supported(ch, "ai_sensitivity")
and api.ai_supported(ch, "people"),
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
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",
native_step=1,
get_min_value=lambda api, ch: 0,
get_max_value=lambda api, ch: 100,
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
supported=lambda api, ch: api.supported(ch, "ai_sensitivity")
and api.ai_supported(ch, "vehicle"),
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
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",
native_step=1,
get_min_value=lambda api, ch: 0,
get_max_value=lambda api, ch: 100,
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
supported=lambda api, ch: api.supported(ch, "ai_sensitivity")
and api.ai_supported(ch, "dog_cat"),
Comment thread
starkillerOG marked this conversation as resolved.
Outdated
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 @@ -119,3 +213,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()