core/homeassistant/components/zha/update.py

168 lines
5.6 KiB
Python
Raw Normal View History

"""Representation of ZHA updates."""
from __future__ import annotations
import functools
Use new zigpy OTA providers for ZHA (#111159) * Use `None` instead of `"unknown"` when the current version is unknown * Only use the current file version from the OTA notification * Use `sw_version`, if available, and update `current_file_version` * Assume the current version is the latest version * Fix lint errors * Use `image` instead of `firmware` * Include a changelog if updates expose it * Clear latest firmware only after updating the installed version * Bump minimum zigpy version to 0.63.0 * Create a data update coordinator to consolidate updates * Fix overridden `async_update` * Fix most unit tests * Simplify `test_devices` to fix current tests * Use a dict comprehension for creating mocked entities * Fix unit tests (thanks @dmulcahey!) * Update the currently installed version on cluster attribute update * Drop `PARALLEL_UPDATES` now that we use an update coordinator * Drop `_reset_progress`, it is already handled by the update component * Do not update the progress if we are not supposed to be updating * Ignore latest version (e.g. if device attrs changed) if zigpy rejects it * Clean up handling of command id in `Ota.cluster_command` * Start progress at 1%: 0 and False are considered equal and are filtered! Use `ceil` instead of remapping 1-100 * The installed version will be auto-updated when the upgrade succeeds * Avoid 1 as well, it collides with `True` * Bump zigpy to (unreleased) 0.63.2 * Fix unit tests * Fix existing unit tests Send both event types Globally enable sending both event types * Remove unnecessary branches * Test ignoring invalid progress callbacks * Test updating a device with a no longer compatible firmware
2024-02-28 19:38:04 +00:00
import logging
import math
from typing import Any
from zha.exceptions import ZHAException
from zigpy.application import ControllerApplication
from homeassistant.components.update import (
UpdateDeviceClass,
UpdateEntity,
UpdateEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Use new zigpy OTA providers for ZHA (#111159) * Use `None` instead of `"unknown"` when the current version is unknown * Only use the current file version from the OTA notification * Use `sw_version`, if available, and update `current_file_version` * Assume the current version is the latest version * Fix lint errors * Use `image` instead of `firmware` * Include a changelog if updates expose it * Clear latest firmware only after updating the installed version * Bump minimum zigpy version to 0.63.0 * Create a data update coordinator to consolidate updates * Fix overridden `async_update` * Fix most unit tests * Simplify `test_devices` to fix current tests * Use a dict comprehension for creating mocked entities * Fix unit tests (thanks @dmulcahey!) * Update the currently installed version on cluster attribute update * Drop `PARALLEL_UPDATES` now that we use an update coordinator * Drop `_reset_progress`, it is already handled by the update component * Do not update the progress if we are not supposed to be updating * Ignore latest version (e.g. if device attrs changed) if zigpy rejects it * Clean up handling of command id in `Ota.cluster_command` * Start progress at 1%: 0 and False are considered equal and are filtered! Use `ceil` instead of remapping 1-100 * The installed version will be auto-updated when the upgrade succeeds * Avoid 1 as well, it collides with `True` * Bump zigpy to (unreleased) 0.63.2 * Fix unit tests * Fix existing unit tests Send both event types Globally enable sending both event types * Remove unnecessary branches * Test ignoring invalid progress callbacks * Test updating a device with a no longer compatible firmware
2024-02-28 19:38:04 +00:00
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from .entity import ZHAEntity
from .helpers import (
SIGNAL_ADD_ENTITIES,
EntityData,
async_add_entities as zha_async_add_entities,
get_zha_data,
get_zha_gateway,
)
Use new zigpy OTA providers for ZHA (#111159) * Use `None` instead of `"unknown"` when the current version is unknown * Only use the current file version from the OTA notification * Use `sw_version`, if available, and update `current_file_version` * Assume the current version is the latest version * Fix lint errors * Use `image` instead of `firmware` * Include a changelog if updates expose it * Clear latest firmware only after updating the installed version * Bump minimum zigpy version to 0.63.0 * Create a data update coordinator to consolidate updates * Fix overridden `async_update` * Fix most unit tests * Simplify `test_devices` to fix current tests * Use a dict comprehension for creating mocked entities * Fix unit tests (thanks @dmulcahey!) * Update the currently installed version on cluster attribute update * Drop `PARALLEL_UPDATES` now that we use an update coordinator * Drop `_reset_progress`, it is already handled by the update component * Do not update the progress if we are not supposed to be updating * Ignore latest version (e.g. if device attrs changed) if zigpy rejects it * Clean up handling of command id in `Ota.cluster_command` * Start progress at 1%: 0 and False are considered equal and are filtered! Use `ceil` instead of remapping 1-100 * The installed version will be auto-updated when the upgrade succeeds * Avoid 1 as well, it collides with `True` * Bump zigpy to (unreleased) 0.63.2 * Fix unit tests * Fix existing unit tests Send both event types Globally enable sending both event types * Remove unnecessary branches * Test ignoring invalid progress callbacks * Test updating a device with a no longer compatible firmware
2024-02-28 19:38:04 +00:00
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Zigbee Home Automation update from config entry."""
zha_data = get_zha_data(hass)
if zha_data.update_coordinator is None:
zha_data.update_coordinator = ZHAFirmwareUpdateCoordinator(
hass, get_zha_gateway(hass).application_controller
)
entities_to_create = zha_data.platforms[Platform.UPDATE]
unsub = async_dispatcher_connect(
hass,
SIGNAL_ADD_ENTITIES,
functools.partial(
zha_async_add_entities,
Use new zigpy OTA providers for ZHA (#111159) * Use `None` instead of `"unknown"` when the current version is unknown * Only use the current file version from the OTA notification * Use `sw_version`, if available, and update `current_file_version` * Assume the current version is the latest version * Fix lint errors * Use `image` instead of `firmware` * Include a changelog if updates expose it * Clear latest firmware only after updating the installed version * Bump minimum zigpy version to 0.63.0 * Create a data update coordinator to consolidate updates * Fix overridden `async_update` * Fix most unit tests * Simplify `test_devices` to fix current tests * Use a dict comprehension for creating mocked entities * Fix unit tests (thanks @dmulcahey!) * Update the currently installed version on cluster attribute update * Drop `PARALLEL_UPDATES` now that we use an update coordinator * Drop `_reset_progress`, it is already handled by the update component * Do not update the progress if we are not supposed to be updating * Ignore latest version (e.g. if device attrs changed) if zigpy rejects it * Clean up handling of command id in `Ota.cluster_command` * Start progress at 1%: 0 and False are considered equal and are filtered! Use `ceil` instead of remapping 1-100 * The installed version will be auto-updated when the upgrade succeeds * Avoid 1 as well, it collides with `True` * Bump zigpy to (unreleased) 0.63.2 * Fix unit tests * Fix existing unit tests Send both event types Globally enable sending both event types * Remove unnecessary branches * Test ignoring invalid progress callbacks * Test updating a device with a no longer compatible firmware
2024-02-28 19:38:04 +00:00
async_add_entities,
ZHAFirmwareUpdateEntity,
Use new zigpy OTA providers for ZHA (#111159) * Use `None` instead of `"unknown"` when the current version is unknown * Only use the current file version from the OTA notification * Use `sw_version`, if available, and update `current_file_version` * Assume the current version is the latest version * Fix lint errors * Use `image` instead of `firmware` * Include a changelog if updates expose it * Clear latest firmware only after updating the installed version * Bump minimum zigpy version to 0.63.0 * Create a data update coordinator to consolidate updates * Fix overridden `async_update` * Fix most unit tests * Simplify `test_devices` to fix current tests * Use a dict comprehension for creating mocked entities * Fix unit tests (thanks @dmulcahey!) * Update the currently installed version on cluster attribute update * Drop `PARALLEL_UPDATES` now that we use an update coordinator * Drop `_reset_progress`, it is already handled by the update component * Do not update the progress if we are not supposed to be updating * Ignore latest version (e.g. if device attrs changed) if zigpy rejects it * Clean up handling of command id in `Ota.cluster_command` * Start progress at 1%: 0 and False are considered equal and are filtered! Use `ceil` instead of remapping 1-100 * The installed version will be auto-updated when the upgrade succeeds * Avoid 1 as well, it collides with `True` * Bump zigpy to (unreleased) 0.63.2 * Fix unit tests * Fix existing unit tests Send both event types Globally enable sending both event types * Remove unnecessary branches * Test ignoring invalid progress callbacks * Test updating a device with a no longer compatible firmware
2024-02-28 19:38:04 +00:00
entities_to_create,
),
)
config_entry.async_on_unload(unsub)
class ZHAFirmwareUpdateCoordinator(DataUpdateCoordinator[None]): # pylint: disable=hass-enforce-coordinator-module
Use new zigpy OTA providers for ZHA (#111159) * Use `None` instead of `"unknown"` when the current version is unknown * Only use the current file version from the OTA notification * Use `sw_version`, if available, and update `current_file_version` * Assume the current version is the latest version * Fix lint errors * Use `image` instead of `firmware` * Include a changelog if updates expose it * Clear latest firmware only after updating the installed version * Bump minimum zigpy version to 0.63.0 * Create a data update coordinator to consolidate updates * Fix overridden `async_update` * Fix most unit tests * Simplify `test_devices` to fix current tests * Use a dict comprehension for creating mocked entities * Fix unit tests (thanks @dmulcahey!) * Update the currently installed version on cluster attribute update * Drop `PARALLEL_UPDATES` now that we use an update coordinator * Drop `_reset_progress`, it is already handled by the update component * Do not update the progress if we are not supposed to be updating * Ignore latest version (e.g. if device attrs changed) if zigpy rejects it * Clean up handling of command id in `Ota.cluster_command` * Start progress at 1%: 0 and False are considered equal and are filtered! Use `ceil` instead of remapping 1-100 * The installed version will be auto-updated when the upgrade succeeds * Avoid 1 as well, it collides with `True` * Bump zigpy to (unreleased) 0.63.2 * Fix unit tests * Fix existing unit tests Send both event types Globally enable sending both event types * Remove unnecessary branches * Test ignoring invalid progress callbacks * Test updating a device with a no longer compatible firmware
2024-02-28 19:38:04 +00:00
"""Firmware update coordinator that broadcasts updates network-wide."""
def __init__(
self, hass: HomeAssistant, controller_application: ControllerApplication
) -> None:
"""Initialize the coordinator."""
super().__init__(
hass,
_LOGGER,
name="ZHA firmware update coordinator",
update_method=self.async_update_data,
)
self.controller_application = controller_application
async def async_update_data(self) -> None:
"""Fetch the latest firmware update data."""
# Broadcast to all devices
await self.controller_application.ota.broadcast_notify(jitter=100)
class ZHAFirmwareUpdateEntity(
ZHAEntity, CoordinatorEntity[ZHAFirmwareUpdateCoordinator], UpdateEntity
):
"""Representation of a ZHA firmware update entity."""
_attr_device_class = UpdateDeviceClass.FIRMWARE
_attr_supported_features = (
UpdateEntityFeature.INSTALL
| UpdateEntityFeature.PROGRESS
| UpdateEntityFeature.SPECIFIC_VERSION
)
def __init__(self, entity_data: EntityData, **kwargs: Any) -> None:
"""Initialize the ZHA siren."""
zha_data = get_zha_data(entity_data.device_proxy.gateway_proxy.hass)
assert zha_data.update_coordinator is not None
Use new zigpy OTA providers for ZHA (#111159) * Use `None` instead of `"unknown"` when the current version is unknown * Only use the current file version from the OTA notification * Use `sw_version`, if available, and update `current_file_version` * Assume the current version is the latest version * Fix lint errors * Use `image` instead of `firmware` * Include a changelog if updates expose it * Clear latest firmware only after updating the installed version * Bump minimum zigpy version to 0.63.0 * Create a data update coordinator to consolidate updates * Fix overridden `async_update` * Fix most unit tests * Simplify `test_devices` to fix current tests * Use a dict comprehension for creating mocked entities * Fix unit tests (thanks @dmulcahey!) * Update the currently installed version on cluster attribute update * Drop `PARALLEL_UPDATES` now that we use an update coordinator * Drop `_reset_progress`, it is already handled by the update component * Do not update the progress if we are not supposed to be updating * Ignore latest version (e.g. if device attrs changed) if zigpy rejects it * Clean up handling of command id in `Ota.cluster_command` * Start progress at 1%: 0 and False are considered equal and are filtered! Use `ceil` instead of remapping 1-100 * The installed version will be auto-updated when the upgrade succeeds * Avoid 1 as well, it collides with `True` * Bump zigpy to (unreleased) 0.63.2 * Fix unit tests * Fix existing unit tests Send both event types Globally enable sending both event types * Remove unnecessary branches * Test ignoring invalid progress callbacks * Test updating a device with a no longer compatible firmware
2024-02-28 19:38:04 +00:00
super().__init__(entity_data, coordinator=zha_data.update_coordinator, **kwargs)
CoordinatorEntity.__init__(self, zha_data.update_coordinator)
Use new zigpy OTA providers for ZHA (#111159) * Use `None` instead of `"unknown"` when the current version is unknown * Only use the current file version from the OTA notification * Use `sw_version`, if available, and update `current_file_version` * Assume the current version is the latest version * Fix lint errors * Use `image` instead of `firmware` * Include a changelog if updates expose it * Clear latest firmware only after updating the installed version * Bump minimum zigpy version to 0.63.0 * Create a data update coordinator to consolidate updates * Fix overridden `async_update` * Fix most unit tests * Simplify `test_devices` to fix current tests * Use a dict comprehension for creating mocked entities * Fix unit tests (thanks @dmulcahey!) * Update the currently installed version on cluster attribute update * Drop `PARALLEL_UPDATES` now that we use an update coordinator * Drop `_reset_progress`, it is already handled by the update component * Do not update the progress if we are not supposed to be updating * Ignore latest version (e.g. if device attrs changed) if zigpy rejects it * Clean up handling of command id in `Ota.cluster_command` * Start progress at 1%: 0 and False are considered equal and are filtered! Use `ceil` instead of remapping 1-100 * The installed version will be auto-updated when the upgrade succeeds * Avoid 1 as well, it collides with `True` * Bump zigpy to (unreleased) 0.63.2 * Fix unit tests * Fix existing unit tests Send both event types Globally enable sending both event types * Remove unnecessary branches * Test ignoring invalid progress callbacks * Test updating a device with a no longer compatible firmware
2024-02-28 19:38:04 +00:00
@property
def installed_version(self) -> str | None:
"""Version installed and in use."""
return self.entity_data.entity.installed_version
@property
def in_progress(self) -> bool | int | None:
"""Update installation progress.
Needs UpdateEntityFeature.PROGRESS flag to be set for it to be used.
Can either return a boolean (True if in progress, False if not)
or an integer to indicate the progress in from 0 to 100%.
"""
if not self.entity_data.entity.in_progress:
return self.entity_data.entity.in_progress
Use new zigpy OTA providers for ZHA (#111159) * Use `None` instead of `"unknown"` when the current version is unknown * Only use the current file version from the OTA notification * Use `sw_version`, if available, and update `current_file_version` * Assume the current version is the latest version * Fix lint errors * Use `image` instead of `firmware` * Include a changelog if updates expose it * Clear latest firmware only after updating the installed version * Bump minimum zigpy version to 0.63.0 * Create a data update coordinator to consolidate updates * Fix overridden `async_update` * Fix most unit tests * Simplify `test_devices` to fix current tests * Use a dict comprehension for creating mocked entities * Fix unit tests (thanks @dmulcahey!) * Update the currently installed version on cluster attribute update * Drop `PARALLEL_UPDATES` now that we use an update coordinator * Drop `_reset_progress`, it is already handled by the update component * Do not update the progress if we are not supposed to be updating * Ignore latest version (e.g. if device attrs changed) if zigpy rejects it * Clean up handling of command id in `Ota.cluster_command` * Start progress at 1%: 0 and False are considered equal and are filtered! Use `ceil` instead of remapping 1-100 * The installed version will be auto-updated when the upgrade succeeds * Avoid 1 as well, it collides with `True` * Bump zigpy to (unreleased) 0.63.2 * Fix unit tests * Fix existing unit tests Send both event types Globally enable sending both event types * Remove unnecessary branches * Test ignoring invalid progress callbacks * Test updating a device with a no longer compatible firmware
2024-02-28 19:38:04 +00:00
# Stay in an indeterminate state until we actually send something
if self.entity_data.entity.progress == 0:
return True
# Rescale 0-100% to 2-100% to avoid 0 and 1 colliding with None, False, and True
return int(math.ceil(2 + 98 * self.entity_data.entity.progress / 100))
@property
def latest_version(self) -> str | None:
"""Latest version available for install."""
return self.entity_data.entity.latest_version
@property
def release_summary(self) -> str | None:
"""Summary of the release notes or changelog.
This is not suitable for long changelogs, but merely suitable
for a short excerpt update description of max 255 characters.
"""
return self.entity_data.entity.release_summary
@property
def release_url(self) -> str | None:
"""URL to the full release notes of the latest version available."""
return self.entity_data.entity.release_url
# We explicitly convert ZHA exceptions to HA exceptions here so there is no need to
# use the `@convert_zha_error_to_ha_error` decorator.
async def async_install(
self, version: str | None, backup: bool, **kwargs: Any
) -> None:
"""Install an update."""
try:
await self.entity_data.entity.async_install(version=version, backup=backup)
except ZHAException as exc:
raise HomeAssistantError(exc) from exc
finally:
self.async_write_ha_state()
Use new zigpy OTA providers for ZHA (#111159) * Use `None` instead of `"unknown"` when the current version is unknown * Only use the current file version from the OTA notification * Use `sw_version`, if available, and update `current_file_version` * Assume the current version is the latest version * Fix lint errors * Use `image` instead of `firmware` * Include a changelog if updates expose it * Clear latest firmware only after updating the installed version * Bump minimum zigpy version to 0.63.0 * Create a data update coordinator to consolidate updates * Fix overridden `async_update` * Fix most unit tests * Simplify `test_devices` to fix current tests * Use a dict comprehension for creating mocked entities * Fix unit tests (thanks @dmulcahey!) * Update the currently installed version on cluster attribute update * Drop `PARALLEL_UPDATES` now that we use an update coordinator * Drop `_reset_progress`, it is already handled by the update component * Do not update the progress if we are not supposed to be updating * Ignore latest version (e.g. if device attrs changed) if zigpy rejects it * Clean up handling of command id in `Ota.cluster_command` * Start progress at 1%: 0 and False are considered equal and are filtered! Use `ceil` instead of remapping 1-100 * The installed version will be auto-updated when the upgrade succeeds * Avoid 1 as well, it collides with `True` * Bump zigpy to (unreleased) 0.63.2 * Fix unit tests * Fix existing unit tests Send both event types Globally enable sending both event types * Remove unnecessary branches * Test ignoring invalid progress callbacks * Test updating a device with a no longer compatible firmware
2024-02-28 19:38:04 +00:00
async def async_update(self) -> None:
"""Update the entity."""
await CoordinatorEntity.async_update(self)
await super().async_update()