Home Assistant Unofficial Reference 2024.12.1
update.py
Go to the documentation of this file.
1 """Support for La Marzocco update entities."""
2 
3 from dataclasses import dataclass
4 from typing import Any
5 
6 from pylamarzocco.const import FirmwareType
7 from pylamarzocco.exceptions import RequestNotSuccessful
8 
10  UpdateDeviceClass,
11  UpdateEntity,
12  UpdateEntityDescription,
13  UpdateEntityFeature,
14 )
15 from homeassistant.const import EntityCategory
16 from homeassistant.core import HomeAssistant
17 from homeassistant.exceptions import HomeAssistantError
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .const import DOMAIN
21 from .coordinator import LaMarzoccoConfigEntry
22 from .entity import LaMarzoccoEntity, LaMarzoccoEntityDescription
23 
24 PARALLEL_UPDATES = 1
25 
26 
27 @dataclass(frozen=True, kw_only=True)
29  LaMarzoccoEntityDescription,
30  UpdateEntityDescription,
31 ):
32  """Description of a La Marzocco update entities."""
33 
34  component: FirmwareType
35 
36 
37 ENTITIES: tuple[LaMarzoccoUpdateEntityDescription, ...] = (
39  key="machine_firmware",
40  translation_key="machine_firmware",
41  device_class=UpdateDeviceClass.FIRMWARE,
42  component=FirmwareType.MACHINE,
43  entity_category=EntityCategory.DIAGNOSTIC,
44  ),
46  key="gateway_firmware",
47  translation_key="gateway_firmware",
48  device_class=UpdateDeviceClass.FIRMWARE,
49  component=FirmwareType.GATEWAY,
50  entity_category=EntityCategory.DIAGNOSTIC,
51  ),
52 )
53 
54 
56  hass: HomeAssistant,
57  entry: LaMarzoccoConfigEntry,
58  async_add_entities: AddEntitiesCallback,
59 ) -> None:
60  """Create update entities."""
61 
62  coordinator = entry.runtime_data
64  LaMarzoccoUpdateEntity(coordinator, description)
65  for description in ENTITIES
66  if description.supported_fn(coordinator)
67  )
68 
69 
71  """Entity representing the update state."""
72 
73  entity_description: LaMarzoccoUpdateEntityDescription
74  _attr_supported_features = UpdateEntityFeature.INSTALL
75 
76  @property
77  def installed_version(self) -> str | None:
78  """Return the current firmware version."""
79  return self.coordinator.device.firmware[
80  self.entity_descriptionentity_description.component
81  ].current_version
82 
83  @property
84  def latest_version(self) -> str:
85  """Return the latest firmware version."""
86  return self.coordinator.device.firmware[
87  self.entity_descriptionentity_description.component
88  ].latest_version
89 
90  @property
91  def release_url(self) -> str | None:
92  """Return the release notes URL."""
93  return "https://support-iot.lamarzocco.com/firmware-updates/"
94 
95  async def async_install(
96  self, version: str | None, backup: bool, **kwargs: Any
97  ) -> None:
98  """Install an update."""
99  self._attr_in_progress_attr_in_progress_attr_in_progress = True
100  self.async_write_ha_stateasync_write_ha_state()
101  try:
102  success = await self.coordinator.device.update_firmware(
103  self.entity_descriptionentity_description.component
104  )
105  except RequestNotSuccessful as exc:
106  raise HomeAssistantError(
107  translation_domain=DOMAIN,
108  translation_key="update_failed",
109  translation_placeholders={
110  "key": self.entity_descriptionentity_description.key,
111  },
112  ) from exc
113  if not success:
114  raise HomeAssistantError(
115  translation_domain=DOMAIN,
116  translation_key="update_failed",
117  translation_placeholders={
118  "key": self.entity_descriptionentity_description.key,
119  },
120  )
121  self._attr_in_progress_attr_in_progress_attr_in_progress = False
122  await self.coordinator.async_request_refresh()
None async_install(self, str|None version, bool backup, **Any kwargs)
Definition: update.py:97
None async_setup_entry(HomeAssistant hass, LaMarzoccoConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: update.py:59