Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for the Fibaro devices."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 from pyfibaro.fibaro_device import DeviceModel
10 
11 from homeassistant.const import ATTR_ARMED, ATTR_BATTERY_LEVEL
12 from homeassistant.helpers.entity import Entity
13 
14 _LOGGER = logging.getLogger(__name__)
15 
16 
18  """Representation of a Fibaro device entity."""
19 
20  _attr_should_poll = False
21 
22  def __init__(self, fibaro_device: DeviceModel) -> None:
23  """Initialize the device."""
24  self.fibaro_devicefibaro_device = fibaro_device
25  self.controllercontroller = fibaro_device.fibaro_controller
26  self.ha_idha_id = fibaro_device.ha_id
27  self._attr_name_attr_name = fibaro_device.friendly_name
28  self._attr_unique_id_attr_unique_id = fibaro_device.unique_id_str
29 
30  self._attr_device_info_attr_device_info = self.controllercontroller.get_device_info(fibaro_device)
31  # propagate hidden attribute set in fibaro home center to HA
32  if not fibaro_device.visible:
33  self._attr_entity_registry_visible_default_attr_entity_registry_visible_default = False
34 
35  async def async_added_to_hass(self) -> None:
36  """Call when entity is added to hass."""
37  self.controllercontroller.register(self.fibaro_devicefibaro_device.fibaro_id, self._update_callback_update_callback)
38 
39  def _update_callback(self) -> None:
40  """Update the state."""
41  self.schedule_update_ha_stateschedule_update_ha_state(True)
42 
43  @property
44  def level(self) -> int | None:
45  """Get the level of Fibaro device."""
46  if self.fibaro_devicefibaro_device.value.has_value:
47  return self.fibaro_devicefibaro_device.value.int_value()
48  return None
49 
50  @property
51  def level2(self) -> int | None:
52  """Get the tilt level of Fibaro device."""
53  if self.fibaro_devicefibaro_device.value_2.has_value:
54  return self.fibaro_devicefibaro_device.value_2.int_value()
55  return None
56 
57  def dont_know_message(self, cmd: str) -> None:
58  """Make a warning in case we don't know how to perform an action."""
59  _LOGGER.warning(
60  "Not sure how to %s: %s (available actions: %s)",
61  cmd,
62  str(self.ha_idha_id),
63  str(self.fibaro_devicefibaro_device.actions),
64  )
65 
66  def set_level(self, level: int) -> None:
67  """Set the level of Fibaro device."""
68  self.actionaction("setValue", level)
69  if self.fibaro_devicefibaro_device.value.has_value:
70  self.fibaro_devicefibaro_device.properties["value"] = level
71  if self.fibaro_devicefibaro_device.has_brightness:
72  self.fibaro_devicefibaro_device.properties["brightness"] = level
73 
74  def set_level2(self, level: int) -> None:
75  """Set the level2 of Fibaro device."""
76  self.actionaction("setValue2", level)
77  if self.fibaro_devicefibaro_device.value_2.has_value:
78  self.fibaro_devicefibaro_device.properties["value2"] = level
79 
80  def call_turn_on(self) -> None:
81  """Turn on the Fibaro device."""
82  self.actionaction("turnOn")
83 
84  def call_turn_off(self) -> None:
85  """Turn off the Fibaro device."""
86  self.actionaction("turnOff")
87 
88  def call_set_color(self, red: int, green: int, blue: int, white: int) -> None:
89  """Set the color of Fibaro device."""
90  red = int(max(0, min(255, red)))
91  green = int(max(0, min(255, green)))
92  blue = int(max(0, min(255, blue)))
93  white = int(max(0, min(255, white)))
94  color_str = f"{red},{green},{blue},{white}"
95  self.fibaro_devicefibaro_device.properties["color"] = color_str
96  self.actionaction("setColor", str(red), str(green), str(blue), str(white))
97 
98  def action(self, cmd: str, *args: Any) -> None:
99  """Perform an action on the Fibaro HC."""
100  if cmd in self.fibaro_devicefibaro_device.actions:
101  self.fibaro_devicefibaro_device.execute_action(cmd, args)
102  _LOGGER.debug("-> %s.%s%s called", str(self.ha_idha_id), str(cmd), str(args))
103  else:
104  self.dont_know_messagedont_know_message(cmd)
105 
106  @property
107  def current_binary_state(self) -> bool:
108  """Return the current binary state."""
109  return self.fibaro_devicefibaro_device.value.bool_value(False)
110 
111  @property
112  def extra_state_attributes(self) -> Mapping[str, Any]:
113  """Return the state attributes of the device."""
114  attr = {"fibaro_id": self.fibaro_devicefibaro_device.fibaro_id}
115 
116  if self.fibaro_devicefibaro_device.has_battery_level:
117  attr[ATTR_BATTERY_LEVEL] = self.fibaro_devicefibaro_device.battery_level
118  if self.fibaro_devicefibaro_device.has_armed:
119  attr[ATTR_ARMED] = self.fibaro_devicefibaro_device.armed
120 
121  return attr
122 
123  def update(self) -> None:
124  """Update the available state of the entity."""
125  if self.fibaro_devicefibaro_device.has_dead:
126  self._attr_available_attr_available = not self.fibaro_devicefibaro_device.dead
None action(self, str cmd, *Any args)
Definition: entity.py:98
None call_set_color(self, int red, int green, int blue, int white)
Definition: entity.py:88
Mapping[str, Any] extra_state_attributes(self)
Definition: entity.py:112
None __init__(self, DeviceModel fibaro_device)
Definition: entity.py:22
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
def register(HomeAssistant hass, Heos controller)
Definition: services.py:29
DeviceInfo get_device_info(str coordinates, str name)
Definition: __init__.py:156