Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for UpCloud."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import upcloud_api
9 
10 from homeassistant.const import CONF_USERNAME, STATE_OFF, STATE_ON, STATE_PROBLEM
11 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
12 from homeassistant.helpers.update_coordinator import CoordinatorEntity
13 
14 from .const import DOMAIN
15 from .coordinator import UpCloudDataUpdateCoordinator
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 ATTR_CORE_NUMBER = "core_number"
20 ATTR_HOSTNAME = "hostname"
21 ATTR_MEMORY_AMOUNT = "memory_amount"
22 ATTR_TITLE = "title"
23 ATTR_UUID = "uuid"
24 ATTR_ZONE = "zone"
25 
26 DEFAULT_COMPONENT_NAME = "UpCloud {}"
27 
28 STATE_MAP = {"error": STATE_PROBLEM, "started": STATE_ON, "stopped": STATE_OFF}
29 
30 
31 class UpCloudServerEntity(CoordinatorEntity[UpCloudDataUpdateCoordinator]):
32  """Entity class for UpCloud servers."""
33 
34  def __init__(
35  self,
36  coordinator: UpCloudDataUpdateCoordinator,
37  uuid: str,
38  ) -> None:
39  """Initialize the UpCloud server entity."""
40  super().__init__(coordinator)
41  self.uuiduuid = uuid
42 
43  @property
44  def _server(self) -> upcloud_api.Server:
45  return self.coordinator.data[self.uuiduuid]
46 
47  @property
48  def unique_id(self) -> str:
49  """Return unique ID for the entity."""
50  return self.uuiduuid
51 
52  @property
53  def name(self) -> str:
54  """Return the name of the component."""
55  try:
56  return DEFAULT_COMPONENT_NAME.format(self._server_server.title)
57  except (AttributeError, KeyError, TypeError):
58  return DEFAULT_COMPONENT_NAME.format(self.uuiduuid)
59 
60  @property
61  def icon(self) -> str:
62  """Return the icon of this server."""
63  return "mdi:server" if self.is_onis_on else "mdi:server-off"
64 
65  @property
66  def is_on(self) -> bool:
67  """Return true if the server is on."""
68  try:
69  return STATE_MAP.get(self._server_server.state, self._server_server.state) == STATE_ON # type: ignore[no-any-return]
70  except AttributeError:
71  return False
72 
73  @property
74  def available(self) -> bool:
75  """Return True if entity is available."""
76  return super().available and STATE_MAP.get(
77  self._server_server.state, self._server_server.state
78  ) in (STATE_ON, STATE_OFF)
79 
80  @property
81  def extra_state_attributes(self) -> dict[str, Any]:
82  """Return the state attributes of the UpCloud server."""
83  return {
84  x: getattr(self._server_server, x, None)
85  for x in (
86  ATTR_UUID,
87  ATTR_TITLE,
88  ATTR_HOSTNAME,
89  ATTR_ZONE,
90  ATTR_CORE_NUMBER,
91  ATTR_MEMORY_AMOUNT,
92  )
93  }
94 
95  @property
96  def device_info(self) -> DeviceInfo:
97  """Return info for device registry."""
98  assert self.coordinator.config_entry is not None
99  return DeviceInfo(
100  configuration_url="https://hub.upcloud.com",
101  model="Control Panel",
102  entry_type=DeviceEntryType.SERVICE,
103  identifiers={
104  (DOMAIN, f"{self.coordinator.config_entry.data[CONF_USERNAME]}@hub")
105  },
106  manufacturer="UpCloud Ltd",
107  )
None __init__(self, UpCloudDataUpdateCoordinator coordinator, str uuid)
Definition: entity.py:38