2024-01-17 13:49:08 +00:00
|
|
|
"""Support for La Marzocco update entities."""
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Any
|
|
|
|
|
2024-06-10 17:59:39 +00:00
|
|
|
from lmcloud.const import FirmwareType
|
2024-01-17 13:49:08 +00:00
|
|
|
|
|
|
|
from homeassistant.components.update import (
|
|
|
|
UpdateDeviceClass,
|
|
|
|
UpdateEntity,
|
|
|
|
UpdateEntityDescription,
|
|
|
|
UpdateEntityFeature,
|
|
|
|
)
|
|
|
|
from homeassistant.const import EntityCategory
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2024-06-11 20:27:47 +00:00
|
|
|
from . import LaMarzoccoConfigEntry
|
2024-01-17 13:49:08 +00:00
|
|
|
from .entity import LaMarzoccoEntity, LaMarzoccoEntityDescription
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
|
|
class LaMarzoccoUpdateEntityDescription(
|
|
|
|
LaMarzoccoEntityDescription,
|
|
|
|
UpdateEntityDescription,
|
|
|
|
):
|
|
|
|
"""Description of a La Marzocco update entities."""
|
|
|
|
|
2024-06-10 17:59:39 +00:00
|
|
|
component: FirmwareType
|
2024-01-17 13:49:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
ENTITIES: tuple[LaMarzoccoUpdateEntityDescription, ...] = (
|
|
|
|
LaMarzoccoUpdateEntityDescription(
|
|
|
|
key="machine_firmware",
|
|
|
|
translation_key="machine_firmware",
|
|
|
|
device_class=UpdateDeviceClass.FIRMWARE,
|
2024-06-10 17:59:39 +00:00
|
|
|
component=FirmwareType.MACHINE,
|
2024-01-17 13:49:08 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
|
|
|
LaMarzoccoUpdateEntityDescription(
|
|
|
|
key="gateway_firmware",
|
|
|
|
translation_key="gateway_firmware",
|
|
|
|
device_class=UpdateDeviceClass.FIRMWARE,
|
2024-06-10 17:59:39 +00:00
|
|
|
component=FirmwareType.GATEWAY,
|
2024-01-17 13:49:08 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
2024-06-11 20:27:47 +00:00
|
|
|
entry: LaMarzoccoConfigEntry,
|
2024-01-17 13:49:08 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Create update entities."""
|
|
|
|
|
2024-06-11 20:27:47 +00:00
|
|
|
coordinator = entry.runtime_data
|
2024-01-17 13:49:08 +00:00
|
|
|
async_add_entities(
|
|
|
|
LaMarzoccoUpdateEntity(coordinator, description)
|
|
|
|
for description in ENTITIES
|
|
|
|
if description.supported_fn(coordinator)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class LaMarzoccoUpdateEntity(LaMarzoccoEntity, UpdateEntity):
|
|
|
|
"""Entity representing the update state."""
|
|
|
|
|
|
|
|
entity_description: LaMarzoccoUpdateEntityDescription
|
|
|
|
_attr_supported_features = UpdateEntityFeature.INSTALL
|
|
|
|
|
|
|
|
@property
|
|
|
|
def installed_version(self) -> str | None:
|
|
|
|
"""Return the current firmware version."""
|
2024-06-10 17:59:39 +00:00
|
|
|
return self.coordinator.device.firmware[
|
|
|
|
self.entity_description.component
|
|
|
|
].current_version
|
2024-01-17 13:49:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def latest_version(self) -> str:
|
|
|
|
"""Return the latest firmware version."""
|
2024-06-10 17:59:39 +00:00
|
|
|
return self.coordinator.device.firmware[
|
|
|
|
self.entity_description.component
|
|
|
|
].latest_version
|
2024-01-17 13:49:08 +00:00
|
|
|
|
|
|
|
async def async_install(
|
|
|
|
self, version: str | None, backup: bool, **kwargs: Any
|
|
|
|
) -> None:
|
|
|
|
"""Install an update."""
|
|
|
|
self._attr_in_progress = True
|
|
|
|
self.async_write_ha_state()
|
2024-06-10 17:59:39 +00:00
|
|
|
success = await self.coordinator.device.update_firmware(
|
2024-01-17 13:49:08 +00:00
|
|
|
self.entity_description.component
|
|
|
|
)
|
|
|
|
if not success:
|
|
|
|
raise HomeAssistantError("Update failed")
|
|
|
|
self._attr_in_progress = False
|
2024-01-17 16:26:15 +00:00
|
|
|
await self.coordinator.async_request_refresh()
|