-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Add Reolink light platform #88619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Reolink light platform #88619
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ad7ffbf
Create light.py
starkillerOG 6f9632c
Merge remote-tracking branch 'upstream/dev' into reolink_light
starkillerOG 9fa78dd
Implement light entities
starkillerOG 178f4e5
fix styling
starkillerOG 39f3a2a
Fix setting brightness
starkillerOG fd94831
update name
starkillerOG bc30c7c
fix float/int typing
starkillerOG 2e1d7b2
remove duplicate const
starkillerOG eed34f5
suffix with _fn
starkillerOG b23a6c6
fix black
starkillerOG 31eda8f
require channel
starkillerOG 218c379
Adjust docstring
starkillerOG 35339c4
add typing
starkillerOG 820954b
Review feedback
starkillerOG a212950
styling
starkillerOG 6e3da75
Merge branch 'dev' into reolink_light
starkillerOG 8d98620
Merge branch 'dev' into reolink_light
frenck 9ce9a64
Merge branch 'dev' into reolink_light
starkillerOG 2726d9a
Merge branch 'dev' into reolink_light
starkillerOG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| """Component providing support for Reolink light entities.""" | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| from reolink_aio.api import Host | ||
|
|
||
| from homeassistant.components.light import ( | ||
| ATTR_BRIGHTNESS, | ||
| ColorMode, | ||
| LightEntity, | ||
| LightEntityDescription, | ||
| ) | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import EntityCategory | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
|
||
| from . import ReolinkData | ||
| from .const import DOMAIN | ||
| from .entity import ReolinkCoordinatorEntity | ||
|
|
||
|
|
||
| @dataclass | ||
| class ReolinkLightEntityDescriptionMixin: | ||
| """Mixin values for Reolink light entities.""" | ||
|
|
||
| is_on_fn: Callable[[Host, int], bool] | ||
| turn_on_off_fn: Callable[[Host, int, bool], Any] | ||
|
|
||
|
|
||
| @dataclass | ||
| class ReolinkLightEntityDescription( | ||
| LightEntityDescription, ReolinkLightEntityDescriptionMixin | ||
| ): | ||
| """A class that describes light entities.""" | ||
|
|
||
| supported_fn: Callable[[Host, int], bool] = lambda api, ch: True | ||
| get_brightness_fn: Callable[[Host, int], int] | None = None | ||
| set_brightness_fn: Callable[[Host, int, float], Any] | None = None | ||
|
|
||
|
|
||
| LIGHT_ENTITIES = ( | ||
| 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), | ||
| ), | ||
| ReolinkLightEntityDescription( | ||
| key="ir_lights", | ||
| name="Infra red lights in night mode", | ||
| icon="mdi:led-off", | ||
| supported_fn=lambda api, ch: api.supported(ch, "ir_lights"), | ||
| is_on_fn=lambda api, ch: api.ir_enabled(ch), | ||
| turn_on_off_fn=lambda api, ch, value: api.set_ir_lights(ch, value), | ||
| ), | ||
| ReolinkLightEntityDescription( | ||
| key="status_led", | ||
| name="Status LED", | ||
| icon="mdi:lightning-bolt-circle", | ||
| entity_category=EntityCategory.CONFIG, | ||
| supported_fn=lambda api, ch: api.supported(ch, "status_led"), | ||
| is_on_fn=lambda api, ch: api.status_led_enabled(ch), | ||
| turn_on_off_fn=lambda api, ch, value: api.set_status_led(ch, value), | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ConfigEntry, | ||
| async_add_entities: AddEntitiesCallback, | ||
| ) -> None: | ||
| """Set up a Reolink light entities.""" | ||
| reolink_data: ReolinkData = hass.data[DOMAIN][config_entry.entry_id] | ||
|
|
||
| async_add_entities( | ||
| ReolinkLightEntity(reolink_data, channel, entity_description) | ||
| for entity_description in LIGHT_ENTITIES | ||
| for channel in reolink_data.host.api.channels | ||
| if entity_description.supported_fn(reolink_data.host.api, channel) | ||
| ) | ||
|
|
||
|
|
||
| class ReolinkLightEntity(ReolinkCoordinatorEntity, LightEntity): | ||
| """Base light entity class for Reolink IP cameras.""" | ||
|
|
||
| entity_description: ReolinkLightEntityDescription | ||
|
|
||
| def __init__( | ||
| self, | ||
| reolink_data: ReolinkData, | ||
| channel: int, | ||
|
starkillerOG marked this conversation as resolved.
|
||
| entity_description: ReolinkLightEntityDescription, | ||
| ) -> None: | ||
| """Initialize Reolink light entity.""" | ||
| super().__init__(reolink_data, channel) | ||
| self.entity_description = entity_description | ||
|
|
||
| self._attr_unique_id = ( | ||
| f"{self._host.unique_id}_{channel}_{entity_description.key}" | ||
| ) | ||
|
|
||
| if entity_description.set_brightness_fn is None: | ||
| self._attr_supported_color_modes = {ColorMode.ONOFF} | ||
| self._attr_color_mode = ColorMode.ONOFF | ||
| else: | ||
| self._attr_supported_color_modes = {ColorMode.BRIGHTNESS} | ||
| self._attr_color_mode = ColorMode.BRIGHTNESS | ||
|
|
||
| @property | ||
| def is_on(self) -> bool: | ||
| """Return true if light is on.""" | ||
| return self.entity_description.is_on_fn(self._host.api, self._channel) | ||
|
|
||
| @property | ||
| def brightness(self) -> int | None: | ||
| """Return the brightness of this light between 0.255.""" | ||
| if self.entity_description.get_brightness_fn is None: | ||
| return None | ||
|
|
||
| return round( | ||
| 255 | ||
| * ( | ||
| self.entity_description.get_brightness_fn(self._host.api, self._channel) | ||
| / 100.0 | ||
| ) | ||
| ) | ||
|
|
||
| async def async_turn_off(self, **kwargs: Any) -> None: | ||
| """Turn light off.""" | ||
| await self.entity_description.turn_on_off_fn( | ||
| self._host.api, self._channel, False | ||
| ) | ||
| self.async_write_ha_state() | ||
|
|
||
| async def async_turn_on(self, **kwargs: Any) -> None: | ||
| """Turn light on.""" | ||
| if ( | ||
| brightness := kwargs.get(ATTR_BRIGHTNESS) | ||
| ) is not None and self.entity_description.set_brightness_fn is not None: | ||
| brightness_pct = int(brightness / 255.0 * 100) | ||
| await self.entity_description.set_brightness_fn( | ||
| self._host.api, self._channel, brightness_pct | ||
| ) | ||
|
|
||
| await self.entity_description.turn_on_off_fn( | ||
| self._host.api, self._channel, True | ||
| ) | ||
| self.async_write_ha_state() | ||
|
starkillerOG marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.