1 """Support updates for SLZB-06 ESP32 and Zigbee firmwares."""
3 from __future__
import annotations
6 from collections.abc
import Callable
7 from dataclasses
import dataclass
8 from typing
import Any, Final
10 from pysmlight.const
import Events
as SmEvents
11 from pysmlight.models
import Firmware, Info
12 from pysmlight.sse
import MessageEvent
17 UpdateEntityDescription,
25 from .
import SmConfigEntry
26 from .const
import LOGGER
27 from .coordinator
import SmFirmwareUpdateCoordinator, SmFwData
28 from .entity
import SmEntity
31 @dataclass(frozen=True, kw_only=True)
33 """Describes SMLIGHT SLZB-06 update entity."""
35 installed_version: Callable[[Info], str |
None]
36 fw_list: Callable[[SmFwData], list[Firmware] |
None]
39 UPDATE_ENTITIES: Final = [
42 translation_key=
"core_update",
43 installed_version=
lambda x: x.sw_version,
44 fw_list=
lambda x: x.esp_firmware,
48 translation_key=
"zigbee_update",
49 installed_version=
lambda x: x.zb_version,
50 fw_list=
lambda x: x.zb_firmware,
56 hass: HomeAssistant, entry: SmConfigEntry, async_add_entities: AddEntitiesCallback
58 """Set up the SMLIGHT update entities."""
59 coordinator = entry.runtime_data.firmware
62 SmUpdateEntity(coordinator, description)
for description
in UPDATE_ENTITIES
67 """Representation for SLZB-06 update entities."""
69 coordinator: SmFirmwareUpdateCoordinator
70 entity_description: SmUpdateEntityDescription
71 _attr_entity_category = EntityCategory.CONFIG
72 _attr_device_class = UpdateDeviceClass.FIRMWARE
73 _attr_supported_features = (
74 UpdateEntityFeature.INSTALL
75 | UpdateEntityFeature.PROGRESS
76 | UpdateEntityFeature.RELEASE_NOTES
81 coordinator: SmFirmwareUpdateCoordinator,
82 description: SmUpdateEntityDescription,
84 """Initialize the entity."""
88 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.unique_id}-{description.key}"
91 self.
_firmware_firmware: Firmware |
None =
None
92 self._unload: list[Callable] = []
96 """Version installed.."""
97 data = self.coordinator.data
100 return version
if version !=
"-1" else None
104 """Latest version available for install."""
105 data = self.coordinator.data
106 if self.coordinator.legacy_api == 2:
112 fw = [f
for f
in fw
if f.type == data.info.zb_type]
121 """Register callbacks for SSE update events."""
123 self.coordinator.client.sse.register_callback(
128 self.coordinator.client.sse.register_callback(
132 if self.coordinator.legacy_api == 1:
134 self.coordinator.client.sse.register_callback(
139 self.coordinator.client.sse.register_callback(
145 """Return release notes for firmware."""
154 """Update install progress on event."""
156 progress =
int(progress.data)
161 """Handle cleanup for update done."""
164 for remove_cb
in self._unload:
174 """Handle event for update finished."""
181 self.coordinator.in_progress =
False
185 self, version: str |
None, backup: bool, **kwargs: Any
187 """Install firmware update."""
189 if not self.coordinator.in_progress
and self.
_firmware_firmware:
190 self.coordinator.in_progress =
True
195 await self.coordinator.client.fw_update(self.
_firmware_firmware)
202 async
with asyncio.timeout(180):
204 self.coordinator.in_progress
208 await asyncio.sleep(1)
211 "Timeout waiting for %s to reboot after update",
212 self.coordinator.data.info.hostname,
215 self.coordinator.in_progress =
False
str|None installed_version(self)
str|None release_notes(self)
None async_install(self, str|None version, bool backup, **Any kwargs)
None register_callbacks(self)
None _update_finished(self, MessageEvent event)
str|None latest_version(self)
None __init__(self, SmFirmwareUpdateCoordinator coordinator, SmUpdateEntityDescription description)
None _update_failed(self, MessageEvent event)
None _update_progress(self, MessageEvent progress)
str|None installed_version(self)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, SmConfigEntry entry, AddEntitiesCallback async_add_entities)