1 """Update entities for Reolink devices."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
8 from reolink_aio.exceptions
import ReolinkError
9 from reolink_aio.software_version
import NewSoftwareVersion, SoftwareVersion
14 UpdateEntityDescription,
23 DataUpdateCoordinator,
26 from .
import DEVICE_UPDATE_INTERVAL
28 ReolinkChannelCoordinatorEntity,
29 ReolinkChannelEntityDescription,
30 ReolinkHostCoordinatorEntity,
31 ReolinkHostEntityDescription,
33 from .util
import ReolinkConfigEntry, ReolinkData
36 RESUME_AFTER_INSTALL = 15
37 POLL_AFTER_INSTALL = 120
41 @dataclass(frozen=True, kw_only=True)
43 UpdateEntityDescription,
44 ReolinkChannelEntityDescription,
46 """A class that describes update entities."""
49 @dataclass(frozen=True, kw_only=True)
51 UpdateEntityDescription,
52 ReolinkHostEntityDescription,
54 """A class that describes host update entities."""
60 supported=
lambda api, ch: api.supported(ch,
"firmware"),
61 device_class=UpdateDeviceClass.FIRMWARE,
65 HOST_UPDATE_ENTITIES = (
68 supported=
lambda api: api.supported(
None,
"firmware"),
69 device_class=UpdateDeviceClass.FIRMWARE,
76 config_entry: ReolinkConfigEntry,
77 async_add_entities: AddEntitiesCallback,
79 """Set up update entities for Reolink component."""
80 reolink_data: ReolinkData = config_entry.runtime_data
82 entities: list[ReolinkUpdateEntity | ReolinkHostUpdateEntity] = [
84 for entity_description
in UPDATE_ENTITIES
85 for channel
in reolink_data.host.api.channels
86 if entity_description.supported(reolink_data.host.api, channel)
90 for entity_description
in HOST_UPDATE_ENTITIES
91 if entity_description.supported(reolink_data.host.api)
97 CoordinatorEntity[DataUpdateCoordinator[
None]], UpdateEntity
99 """Base update entity class for Reolink."""
101 _attr_release_url =
"https://reolink.com/download-center/"
105 reolink_data: ReolinkData,
107 coordinator: DataUpdateCoordinator[
None],
109 """Initialize Reolink update entity."""
110 CoordinatorEntity.__init__(self, coordinator)
121 """Version currently in use."""
122 return self.
_host_host.api.camera_sw_version(self.
_channel_channel)
126 """Latest version available for install."""
127 new_firmware = self.
_host_host.api.firmware_update_available(self.
_channel_channel)
131 if isinstance(new_firmware, str):
134 return new_firmware.version_string
138 """Update installation progress."""
139 return self.
_host_host.api.sw_upload_progress(self.
_channel_channel) < 100
143 """Update installation progress."""
144 return self.
_host_host.api.sw_upload_progress(self.
_channel_channel)
148 """Flag supported features."""
149 supported_features = UpdateEntityFeature.INSTALL
150 new_firmware = self.
_host_host.api.firmware_update_available(self.
_channel_channel)
151 if isinstance(new_firmware, NewSoftwareVersion):
152 supported_features |= UpdateEntityFeature.RELEASE_NOTES
153 supported_features |= UpdateEntityFeature.PROGRESS
154 return supported_features
158 """Return True if entity is available."""
161 return super().available
164 """Return True if latest_version is newer than installed_version."""
166 installed = SoftwareVersion(installed_version)
167 latest = SoftwareVersion(latest_version)
172 return latest > installed
175 """Return the release notes."""
176 new_firmware = self.
_host_host.api.firmware_update_available(self.
_channel_channel)
177 assert isinstance(new_firmware, NewSoftwareVersion)
180 "If the install button fails, download this"
181 f
" [firmware zip file]({new_firmware.download_url})."
182 " Then, follow the installation guide (PDF in the zip file).\n\n"
183 f
"## Release notes\n\n{new_firmware.release_notes}"
187 self, version: str |
None, backup: bool, **kwargs: Any
189 """Install the latest firmware version."""
196 await self.
_host_host.api.update_firmware(self.
_channel_channel)
197 except ReolinkError
as err:
199 f
"Error trying to update Reolink firmware: {err}"
212 """Pause updating the states using the data update coordinator (during reboots)."""
213 self.
_reolink_data_reolink_data.device_coordinator.update_interval =
None
214 self.
_reolink_data_reolink_data.device_coordinator.async_set_updated_data(
None)
217 """Resume updating the states using the data update coordinator (after reboots)."""
218 self.
_reolink_data_reolink_data.device_coordinator.update_interval = DEVICE_UPDATE_INTERVAL
220 await self.
_reolink_data_reolink_data.device_coordinator.async_refresh()
225 """Request update."""
233 """Request update."""
240 """Entity created."""
242 self.
_host_host.firmware_ch_list.append(self.
_channel_channel)
245 """Entity removed."""
247 if self.
_channel_channel
in self.
_host_host.firmware_ch_list:
248 self.
_host_host.firmware_ch_list.remove(self.
_channel_channel)
258 ReolinkUpdateBaseEntity,
259 ReolinkChannelCoordinatorEntity,
261 """Base update entity class for Reolink IP cameras."""
263 entity_description: ReolinkUpdateEntityDescription
268 reolink_data: ReolinkData,
270 entity_description: ReolinkUpdateEntityDescription,
272 """Initialize Reolink update entity."""
274 ReolinkUpdateBaseEntity.__init__(
275 self, reolink_data, channel, reolink_data.firmware_coordinator
277 ReolinkChannelCoordinatorEntity.__init__(
278 self, reolink_data, channel, reolink_data.firmware_coordinator
283 ReolinkUpdateBaseEntity,
284 ReolinkHostCoordinatorEntity,
286 """Update entity class for Reolink Host."""
288 entity_description: ReolinkHostUpdateEntityDescription
292 reolink_data: ReolinkData,
293 entity_description: ReolinkHostUpdateEntityDescription,
295 """Initialize Reolink update entity."""
297 ReolinkUpdateBaseEntity.__init__(
298 self, reolink_data,
None, reolink_data.firmware_coordinator
300 ReolinkHostCoordinatorEntity.__init__(
301 self, reolink_data, reolink_data.firmware_coordinator
None __init__(self, ReolinkData reolink_data, ReolinkHostUpdateEntityDescription entity_description)
None _resume_update_coordinator(self, *Any args)
None async_added_to_hass(self)
int update_percentage(self)
str|None installed_version(self)
str|None latest_version(self)
None _pause_update_coordinator(self)
None __init__(self, ReolinkData reolink_data, int|None channel, DataUpdateCoordinator[None] coordinator)
bool version_is_newer(self, str latest_version, str installed_version)
None _async_update_progress(self, *Any args)
None async_install(self, str|None version, bool backup, **Any kwargs)
None async_will_remove_from_hass(self)
UpdateEntityFeature supported_features(self)
None _async_update_future(self, *Any args)
str|None async_release_notes(self)
None __init__(self, ReolinkData reolink_data, int channel, ReolinkUpdateEntityDescription entity_description)
str|None installed_version(self)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, ReolinkConfigEntry config_entry, AddEntitiesCallback async_add_entities)
CALLBACK_TYPE async_call_later(HomeAssistant hass, float|timedelta delay, HassJob[[datetime], Coroutine[Any, Any, None]|None]|Callable[[datetime], Coroutine[Any, Any, None]|None] action)