From 497fa1980bc613c323b51bb90e118959b2e437b8 Mon Sep 17 00:00:00 2001 From: RenierM26 <66512715+RenierM26@users.noreply.github.com> Date: Tue, 23 May 2023 12:32:14 +0200 Subject: [PATCH] Add Ezviz update entity (#85377) Co-authored-by: Franck Nijhof --- .coveragerc | 1 + homeassistant/components/ezviz/__init__.py | 1 + .../components/ezviz/binary_sensor.py | 4 - homeassistant/components/ezviz/manifest.json | 2 +- homeassistant/components/ezviz/update.py | 109 ++++++++++++++++++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 7 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 homeassistant/components/ezviz/update.py diff --git a/.coveragerc b/.coveragerc index 6f3b1b092cf..792e0c3785c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -330,6 +330,7 @@ omit = homeassistant/components/ezviz/entity.py homeassistant/components/ezviz/sensor.py homeassistant/components/ezviz/switch.py + homeassistant/components/ezviz/update.py homeassistant/components/faa_delays/__init__.py homeassistant/components/faa_delays/binary_sensor.py homeassistant/components/familyhub/camera.py diff --git a/homeassistant/components/ezviz/__init__.py b/homeassistant/components/ezviz/__init__.py index 489ff97eb4a..05857abbac7 100644 --- a/homeassistant/components/ezviz/__init__.py +++ b/homeassistant/components/ezviz/__init__.py @@ -37,6 +37,7 @@ PLATFORMS_BY_TYPE: dict[str, list] = { Platform.CAMERA, Platform.SENSOR, Platform.SWITCH, + Platform.UPDATE, ], } diff --git a/homeassistant/components/ezviz/binary_sensor.py b/homeassistant/components/ezviz/binary_sensor.py index bab6fa5ca97..77e95fa221d 100644 --- a/homeassistant/components/ezviz/binary_sensor.py +++ b/homeassistant/components/ezviz/binary_sensor.py @@ -25,10 +25,6 @@ BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = { key="alarm_schedules_enabled" ), "encrypted": BinarySensorEntityDescription(key="encrypted"), - "upgrade_available": BinarySensorEntityDescription( - key="upgrade_available", - device_class=BinarySensorDeviceClass.UPDATE, - ), } diff --git a/homeassistant/components/ezviz/manifest.json b/homeassistant/components/ezviz/manifest.json index 5dfeae4bae9..219f4c87d13 100644 --- a/homeassistant/components/ezviz/manifest.json +++ b/homeassistant/components/ezviz/manifest.json @@ -7,5 +7,5 @@ "documentation": "https://www.home-assistant.io/integrations/ezviz", "iot_class": "cloud_polling", "loggers": ["paho_mqtt", "pyezviz"], - "requirements": ["pyezviz==0.2.0.9"] + "requirements": ["pyezviz==0.2.0.12"] } diff --git a/homeassistant/components/ezviz/update.py b/homeassistant/components/ezviz/update.py new file mode 100644 index 00000000000..3acc1032514 --- /dev/null +++ b/homeassistant/components/ezviz/update.py @@ -0,0 +1,109 @@ +"""Support for EZVIZ sensors.""" +from __future__ import annotations + +from typing import Any + +from pyezviz import HTTPError, PyEzvizError + +from homeassistant.components.update import ( + UpdateDeviceClass, + UpdateEntity, + UpdateEntityDescription, + UpdateEntityFeature, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DATA_COORDINATOR, DOMAIN +from .coordinator import EzvizDataUpdateCoordinator +from .entity import EzvizEntity + +PARALLEL_UPDATES = 1 + +UPDATE_ENTITY_TYPES = UpdateEntityDescription( + key="version", + name="Firmware update", + device_class=UpdateDeviceClass.FIRMWARE, +) + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up EZVIZ sensors based on a config entry.""" + coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][ + DATA_COORDINATOR + ] + + async_add_entities( + EzvizUpdateEntity(coordinator, camera, sensor, UPDATE_ENTITY_TYPES) + for camera in coordinator.data + for sensor, value in coordinator.data[camera].items() + if sensor in UPDATE_ENTITY_TYPES.key + if value + ) + + +class EzvizUpdateEntity(EzvizEntity, UpdateEntity): + """Representation of a EZVIZ Update entity.""" + + _attr_has_entity_name = True + _attr_supported_features = ( + UpdateEntityFeature.INSTALL + | UpdateEntityFeature.PROGRESS + | UpdateEntityFeature.RELEASE_NOTES + ) + + def __init__( + self, + coordinator: EzvizDataUpdateCoordinator, + serial: str, + sensor: str, + description: UpdateEntityDescription, + ) -> None: + """Initialize the sensor.""" + super().__init__(coordinator, serial) + self._attr_unique_id = f"{serial}_{sensor}" + self.entity_description = description + + @property + def installed_version(self) -> str | None: + """Version installed and in use.""" + return self.data["version"] + + @property + def in_progress(self) -> bool | int | None: + """Update installation progress.""" + if self.data["upgrade_in_progress"]: + return self.data["upgrade_percent"] + return False + + @property + def latest_version(self) -> str | None: + """Latest version available for install.""" + if self.data["upgrade_available"]: + return self.data["latest_firmware_info"]["version"] + + return self.installed_version + + def release_notes(self) -> str | None: + """Return full release notes.""" + if self.data["latest_firmware_info"]: + return self.data["latest_firmware_info"].get("desc") + return None + + async def async_install( + self, version: str | None, backup: bool, **kwargs: Any + ) -> None: + """Install an update.""" + try: + await self.hass.async_add_executor_job( + self.coordinator.ezviz_client.upgrade_device, self._serial + ) + + except (HTTPError, PyEzvizError) as err: + raise HomeAssistantError( + f"Failed to update firmware on {self.name}" + ) from err diff --git a/requirements_all.txt b/requirements_all.txt index 414aff976ac..28ac5cc662d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1626,7 +1626,7 @@ pyeverlights==0.1.0 pyevilgenius==2.0.0 # homeassistant.components.ezviz -pyezviz==0.2.0.9 +pyezviz==0.2.0.12 # homeassistant.components.fibaro pyfibaro==0.7.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 7370d2b45fa..b21e7dca4f9 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1187,7 +1187,7 @@ pyeverlights==0.1.0 pyevilgenius==2.0.0 # homeassistant.components.ezviz -pyezviz==0.2.0.9 +pyezviz==0.2.0.12 # homeassistant.components.fibaro pyfibaro==0.7.1