Skip to content

Commit

Permalink
Add Ezviz update entity (#85377)
Browse files Browse the repository at this point in the history
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
  • Loading branch information
RenierM26 and frenck committed May 23, 2023
1 parent c23718d commit 497fa19
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 7 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/ezviz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Platform.CAMERA,
Platform.SENSOR,
Platform.SWITCH,
Platform.UPDATE,
],
}

Expand Down
4 changes: 0 additions & 4 deletions homeassistant/components/ezviz/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
key="alarm_schedules_enabled"
),
"encrypted": BinarySensorEntityDescription(key="encrypted"),
"upgrade_available": BinarySensorEntityDescription(
key="upgrade_available",
device_class=BinarySensorDeviceClass.UPDATE,
),
}


Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/ezviz/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
109 changes: 109 additions & 0 deletions homeassistant/components/ezviz/update.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 497fa19

Please sign in to comment.