-
-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Add reolink IP NVR/Camera integration #84081
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
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
0358e9c
Add reolink
starkillerOG 185eb6c
fix yamllint
starkillerOG f2f1347
fix prettier
starkillerOG ebd67cd
use RTSP as default protocol
starkillerOG bc68ab6
Update homeassistant/components/reolink/__init__.py
starkillerOG a3de06f
Update homeassistant/components/reolink/__init__.py
starkillerOG 398a1a5
Update homeassistant/components/reolink/manifest.json
starkillerOG 37e2d5e
Update manifest.json
starkillerOG f3478a4
Remove custom services
starkillerOG 47d6877
use dataclass
starkillerOG d857016
Only raise ConfigEntryNotReady for expected errors
starkillerOG b821275
fix styling
starkillerOG 937f0f3
Update homeassistant/components/reolink/camera.py
starkillerOG 37ae5a7
Update homeassistant/components/reolink/host.py
starkillerOG 9b371b0
use data_entry_flow.FlowResultType
starkillerOG 150f8ba
reduce loglevel to debug
starkillerOG 637b40f
remove request_refresh since async_update is already implemented
starkillerOG 6a4ea6f
Use DeviceInfo class
starkillerOG a5d86c9
Update homeassistant/components/reolink/entity.py
starkillerOG 058d736
Update homeassistant/components/reolink/entity.py
starkillerOG dc40864
simplify
starkillerOG aff4c37
remove unneeded exception
starkillerOG 64428d3
pass around host instead of unpacking
starkillerOG 38e89bf
use _attr_supported_features
starkillerOG 198b143
Update camera.py
starkillerOG 8e88ec6
do not catch broad exception during disconnect
starkillerOG 3b44314
Update homeassistant/components/reolink/config_flow.py
starkillerOG 1ac08ff
improve error message in config flow
starkillerOG 1131d46
Merge branch 'reolink_2' of https://github.com/starkillerOG/home-assi…
starkillerOG 9f50428
use aiohttp_session from upstream lib instead of HASS
starkillerOG 53d4d6d
fix styling
starkillerOG 8e2646f
adjust tests
starkillerOG ac39bf7
Update homeassistant/components/reolink/__init__.py
starkillerOG 1df83cd
Update homeassistant/components/reolink/__init__.py
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,108 @@ | ||
| """Reolink integration for HomeAssistant.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from datetime import timedelta | ||
| import logging | ||
| from typing import cast | ||
|
|
||
| import async_timeout | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import EVENT_HOMEASSISTANT_STOP | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.exceptions import ConfigEntryNotReady | ||
| from homeassistant.helpers.update_coordinator import DataUpdateCoordinator | ||
|
|
||
| from .const import ( | ||
| DEVICE_CONFIG_UPDATE_COORDINATOR, | ||
| DEVICE_UPDATE_INTERVAL, | ||
| DOMAIN, | ||
| HOST, | ||
| PLATFORMS, | ||
| SERVICE_PTZ_CONTROL, | ||
| SERVICE_SET_BACKLIGHT, | ||
| SERVICE_SET_DAYNIGHT, | ||
| SERVICE_SET_SENSITIVITY, | ||
| ) | ||
| from .host import ReolinkHost | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Set up Reolink from a config entry.""" | ||
| hass.data.setdefault(DOMAIN, {}) | ||
|
|
||
| host = ReolinkHost(hass, cast(dict, entry.data), cast(dict, entry.options)) | ||
|
starkillerOG marked this conversation as resolved.
Outdated
|
||
|
|
||
| try: | ||
| if not await host.init(): | ||
| raise ConfigEntryNotReady( | ||
| f"Error while trying to setup {host.api.host}:{host.api.port}: failed to obtain data from device." | ||
|
starkillerOG marked this conversation as resolved.
|
||
| ) | ||
| except Exception as error: # pylint: disable=broad-except | ||
|
starkillerOG marked this conversation as resolved.
Outdated
|
||
| err = str(error) | ||
| if err: | ||
| raise ConfigEntryNotReady( | ||
| f'Error while trying to setup {host.api.host}:{host.api.port}: "{err}".' | ||
| ) from error | ||
| raise ConfigEntryNotReady( | ||
| f"Error while trying to setup {host.api.host}:{host.api.port}: failed to connect to device." | ||
| ) from error | ||
|
|
||
| entry.async_on_unload( | ||
| hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, host.stop) | ||
| ) | ||
|
|
||
| hass.data[DOMAIN][entry.entry_id] = {HOST: host} | ||
|
starkillerOG marked this conversation as resolved.
Outdated
|
||
|
|
||
| async def async_device_config_update(): | ||
| """Perform the update of the host config-state cache, and renew the ONVIF-subscription.""" | ||
|
starkillerOG marked this conversation as resolved.
|
||
| async with async_timeout.timeout(host.api.timeout): | ||
| await host.update_states() # Login session is implicitly updated here, so no need to explicitly do it in a timer | ||
|
starkillerOG marked this conversation as resolved.
|
||
|
|
||
| coordinator_device_config_update = DataUpdateCoordinator( | ||
| hass, | ||
| _LOGGER, | ||
| name=f"reolink.{host.api.nvr_name}", | ||
| update_method=async_device_config_update, | ||
| update_interval=timedelta(seconds=DEVICE_UPDATE_INTERVAL), | ||
| ) | ||
| # Fetch initial data so we have data when entities subscribe | ||
| await coordinator_device_config_update.async_config_entry_first_refresh() | ||
|
|
||
| hass.data[DOMAIN][entry.entry_id][ | ||
| DEVICE_CONFIG_UPDATE_COORDINATOR | ||
| ] = coordinator_device_config_update | ||
|
|
||
| await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) | ||
|
|
||
| entry.async_on_unload(entry.add_update_listener(entry_update_listener)) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| async def entry_update_listener(hass: HomeAssistant, entry: ConfigEntry): | ||
| """Update the configuration of the host entity.""" | ||
| await hass.config_entries.async_reload(entry.entry_id) | ||
|
|
||
|
|
||
| async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Unload a config entry.""" | ||
| host: ReolinkHost = hass.data[DOMAIN][entry.entry_id][HOST] | ||
|
|
||
| await host.stop() | ||
|
|
||
| unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) | ||
|
|
||
| if unload_ok: | ||
|
starkillerOG marked this conversation as resolved.
Outdated
|
||
| hass.data[DOMAIN].pop(entry.entry_id) | ||
|
|
||
| if len(hass.data[DOMAIN]) == 0: | ||
| hass.services.async_remove(DOMAIN, SERVICE_SET_SENSITIVITY) | ||
| hass.services.async_remove(DOMAIN, SERVICE_SET_DAYNIGHT) | ||
| hass.services.async_remove(DOMAIN, SERVICE_SET_BACKLIGHT) | ||
| hass.services.async_remove(DOMAIN, SERVICE_PTZ_CONTROL) | ||
|
starkillerOG marked this conversation as resolved.
Outdated
|
||
|
|
||
| return unload_ok | ||
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,221 @@ | ||
| """This component provides support for Reolink IP cameras.""" | ||
| from __future__ import annotations | ||
|
|
||
| from datetime import datetime | ||
| import logging | ||
| from typing import Any, cast | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.camera import Camera, CameraEntityFeature | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers import config_validation as cv, entity_platform | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
|
||
| from .const import ( | ||
| DOMAIN, | ||
| HOST, | ||
| SERVICE_PTZ_CONTROL, | ||
| SERVICE_SET_BACKLIGHT, | ||
| SERVICE_SET_DAYNIGHT, | ||
| SERVICE_SET_SENSITIVITY, | ||
| SUPPORT_PTZ, | ||
| ) | ||
| from .entity import ReolinkCoordinatorEntity | ||
| from .host import ReolinkHost | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ConfigEntry, | ||
| async_add_devices: AddEntitiesCallback, | ||
|
starkillerOG marked this conversation as resolved.
|
||
| ) -> None: | ||
| """Set up a Reolink IP Camera.""" | ||
| platform = entity_platform.async_get_current_platform() | ||
| platform.async_register_entity_service( | ||
| SERVICE_SET_SENSITIVITY, | ||
| { | ||
| vol.Required("sensitivity"): cv.positive_int, | ||
| vol.Optional("preset"): cv.positive_int, | ||
| }, | ||
| SERVICE_SET_SENSITIVITY, | ||
| ) | ||
|
|
||
| platform.async_register_entity_service( | ||
| SERVICE_SET_DAYNIGHT, | ||
| {vol.Required("mode"): cv.string}, | ||
| SERVICE_SET_DAYNIGHT, | ||
| ) | ||
|
|
||
| platform.async_register_entity_service( | ||
| SERVICE_SET_BACKLIGHT, | ||
| {vol.Required("mode"): cv.string}, | ||
| SERVICE_SET_BACKLIGHT, | ||
| ) | ||
|
|
||
| platform.async_register_entity_service( | ||
| SERVICE_PTZ_CONTROL, | ||
| { | ||
| vol.Required("command"): cv.string, | ||
| vol.Optional("preset"): cv.positive_int, | ||
| vol.Optional("speed"): cv.positive_int, | ||
| }, | ||
| SERVICE_PTZ_CONTROL, | ||
| [SUPPORT_PTZ], | ||
| ) | ||
|
|
||
|
starkillerOG marked this conversation as resolved.
Outdated
|
||
| host: ReolinkHost = hass.data[DOMAIN][config_entry.entry_id][HOST] | ||
|
|
||
| cameras = [] | ||
| for channel in host.api.channels: | ||
| streams = ["sub", "main", "snapshots"] | ||
| if host.api.protocol == "rtmp": | ||
| streams.append("ext") | ||
|
|
||
| for stream in streams: | ||
| cameras.append(ReolinkCamera(hass, config_entry, channel, stream)) | ||
|
|
||
| async_add_devices(cameras, update_before_add=True) | ||
|
|
||
|
|
||
| class ReolinkCamera(ReolinkCoordinatorEntity, Camera): | ||
| """An implementation of a Reolink IP camera.""" | ||
|
|
||
| def __init__(self, hass, config, channel, stream): | ||
|
starkillerOG marked this conversation as resolved.
|
||
| """Initialize Reolink camera stream.""" | ||
| ReolinkCoordinatorEntity.__init__(self, hass, config) | ||
|
starkillerOG marked this conversation as resolved.
|
||
| Camera.__init__(self) | ||
|
|
||
| self._channel = channel | ||
| self._stream = stream | ||
|
|
||
| self._attr_name = f"{self._host.api.camera_name(self._channel)} {self._stream}" | ||
|
starkillerOG marked this conversation as resolved.
|
||
| self._attr_unique_id = ( | ||
| f"reolink_camera_{self._host.unique_id}_{self._channel}_{self._stream}" | ||
|
starkillerOG marked this conversation as resolved.
Outdated
|
||
| ) | ||
| self._attr_entity_registry_enabled_default = stream == "sub" | ||
|
|
||
| self._ptz_commands = { | ||
| "AUTO": "Auto", | ||
| "DOWN": "Down", | ||
| "FOCUSDEC": "FocusDec", | ||
| "FOCUSINC": "FocusInc", | ||
| "LEFT": "Left", | ||
| "LEFTDOWN": "LeftDown", | ||
| "LEFTUP": "LeftUp", | ||
| "RIGHT": "Right", | ||
| "RIGHTDOWN": "RightDown", | ||
| "RIGHTUP": "RightUp", | ||
| "STOP": "Stop", | ||
| "TOPOS": "ToPos", | ||
| "UP": "Up", | ||
| "ZOOMDEC": "ZoomDec", | ||
| "ZOOMINC": "ZoomInc", | ||
| } | ||
| self._daynight_modes = { | ||
| "AUTO": "Auto", | ||
| "COLOR": "Color", | ||
| "BLACKANDWHITE": "Black&White", | ||
| } | ||
|
|
||
| self._backlight_modes = { | ||
| "BACKLIGHTCONTROL": "BackLightControl", | ||
| "DYNAMICRANGECONTROL": "DynamicRangeControl", | ||
| "OFF": "Off", | ||
| } | ||
|
|
||
| @property | ||
| def ptz_supported(self): | ||
| """Supports ptz control.""" | ||
| return self._host.api.ptz_supported(self._channel) | ||
|
|
||
| @property | ||
| def supported_features(self) -> CameraEntityFeature: | ||
|
starkillerOG marked this conversation as resolved.
Outdated
|
||
| """Flag supported features.""" | ||
| features = int(CameraEntityFeature.STREAM) | ||
| if self.ptz_supported: | ||
| features += SUPPORT_PTZ | ||
| return cast(CameraEntityFeature, features) | ||
|
|
||
| @property | ||
| def extra_state_attributes(self) -> dict[str, Any] | None: | ||
| """Return the extra state attributes.""" | ||
| attrs = {} | ||
|
|
||
| if self._host.api.ptz_supported(self._channel): | ||
| attrs["ptz_presets"] = self._host.api.ptz_presets(self._channel) | ||
|
|
||
| for key, value in self._backlight_modes.items(): | ||
| if value == self._host.api.backlight_state(self._channel): | ||
| attrs["backlight_state"] = key | ||
|
|
||
| for key, value in self._daynight_modes.items(): | ||
| if value == self._host.api.daynight_state(self._channel): | ||
| attrs["daynight_state"] = key | ||
|
|
||
| if self._host.api.sensitivity_presets: | ||
| attrs["sensitivity"] = self.get_sensitivity_presets() | ||
|
|
||
| return attrs | ||
|
|
||
| async def stream_source(self) -> str | None: | ||
| """Return the source of the stream.""" | ||
| return await self._host.api.get_stream_source(self._channel, self._stream) | ||
|
|
||
| async def async_camera_image( | ||
| self, width: int | None = None, height: int | None = None | ||
| ) -> bytes | None: | ||
| """Return a still image response from the camera.""" | ||
| return await self._host.api.get_snapshot(self._channel) | ||
|
|
||
| async def ptz_control(self, command, **kwargs): | ||
| """Pass PTZ command to the camera.""" | ||
| if not self.ptz_supported: | ||
| _LOGGER.error("PTZ is not supported on %s camera", self.name) | ||
| return | ||
|
|
||
| await self._host.api.set_ptz_command( | ||
| self._channel, command=self._ptz_commands[command], **kwargs | ||
| ) | ||
|
|
||
| def get_sensitivity_presets(self): | ||
| """Get formatted sensitivity presets.""" | ||
| presets = [] | ||
| preset = {} | ||
|
|
||
| for api_preset in self._host.api.sensitivity_presets(self._channel): | ||
| preset["id"] = api_preset["id"] | ||
| preset["sensitivity"] = api_preset["sensitivity"] | ||
|
|
||
| time_string = f'{api_preset["beginHour"]}:{api_preset["beginMin"]}' | ||
| begin = datetime.strptime(time_string, "%H:%M") | ||
| preset["begin"] = begin.strftime("%H:%M") | ||
|
|
||
| time_string = f'{api_preset["endHour"]}:{api_preset["endMin"]}' | ||
| end = datetime.strptime(time_string, "%H:%M") | ||
| preset["end"] = end.strftime("%H:%M") | ||
|
|
||
| presets.append(preset.copy()) | ||
|
|
||
| return presets | ||
|
|
||
| async def set_sensitivity(self, sensitivity, **kwargs): | ||
| """Set the sensitivity to the camera.""" | ||
| if "preset" in kwargs: | ||
| kwargs["preset"] += 1 | ||
| await self._host.api.set_sensitivity(self._channel, value=sensitivity, **kwargs) | ||
|
|
||
| async def set_daynight(self, mode): | ||
| """Set the day and night mode to the camera.""" | ||
| await self._host.api.set_daynight( | ||
| self._channel, value=self._daynight_modes[mode] | ||
| ) | ||
|
|
||
| async def set_backlight(self, mode): | ||
| """Set the backlight mode to the camera.""" | ||
| await self._host.api.set_backlight( | ||
| self._channel, value=self._backlight_modes[mode] | ||
| ) | ||
Oops, something went wrong.
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.