Skip to content
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

Add Ezviz update entity #85377

Merged
merged 10 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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):
RenierM26 marked this conversation as resolved.
Show resolved Hide resolved
"""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