|
7 | 7 |
|
8 | 8 | from homeassistant.components.fan import FanEntity, FanEntityFeature |
9 | 9 | from homeassistant.const import Platform |
10 | | -from homeassistant.core import HomeAssistant |
| 10 | +from homeassistant.core import HomeAssistant, callback |
11 | 11 | from homeassistant.helpers.entity import DeviceInfo |
12 | 12 | from homeassistant.helpers.entity_platform import AddEntitiesCallback |
13 | 13 | from homeassistant.util.percentage import ( |
|
20 | 20 | from pyisyox.programs import Program |
21 | 21 |
|
22 | 22 | from .const import _LOGGER |
23 | | -from .entity import ISYNodeEntity, ISYProgramEntity |
| 23 | +from .entity import ISYNodeEntity, ISYProgramEntity, NodeEventType |
24 | 24 | from .models import IsyConfigEntry |
25 | 25 |
|
26 | 26 | SPEED_RANGE = (1, 255) # off is not included |
@@ -57,26 +57,33 @@ class ISYFanEntity(ISYNodeEntity, FanEntity): |
57 | 57 | ) |
58 | 58 | _node: Node |
59 | 59 |
|
60 | | - @property |
61 | | - def percentage(self) -> int | None: |
62 | | - """Return the current speed percentage.""" |
63 | | - if self._node.status is None: |
64 | | - return None |
65 | | - return ranged_value_to_percentage(SPEED_RANGE, self._node.status) |
66 | | - |
67 | | - @property |
68 | | - def speed_count(self) -> int: |
69 | | - """Return the number of speeds the fan supports.""" |
70 | | - if self._node.protocol == Protocol.INSTEON: |
71 | | - return 3 |
72 | | - return int_states_in_range(SPEED_RANGE) |
73 | | - |
74 | | - @property |
75 | | - def is_on(self) -> bool | None: |
76 | | - """Get if the fan is on.""" |
| 60 | + def __init__(self, node: Node, device_info: DeviceInfo | None = None) -> None: |
| 61 | + """Initialize the ISY fan entity.""" |
| 62 | + super().__init__(node=node, device_info=device_info) |
| 63 | + self._attr_speed_count = ( |
| 64 | + 3 if node.protocol == Protocol.INSTEON else int_states_in_range(SPEED_RANGE) |
| 65 | + ) |
| 66 | + |
| 67 | + async def async_added_to_hass(self) -> None: |
| 68 | + """Subscribe to events and set initial state.""" |
| 69 | + await super().async_added_to_hass() |
| 70 | + self._update_fan_attrs() |
| 71 | + |
| 72 | + def _update_fan_attrs(self) -> None: |
77 | 73 | if self._node.status is None: |
78 | | - return None |
79 | | - return bool(self._node.status != 0) |
| 74 | + self._attr_is_on = None |
| 75 | + self._attr_percentage = None |
| 76 | + else: |
| 77 | + self._attr_is_on = bool(self._node.status != 0) |
| 78 | + self._attr_percentage = ranged_value_to_percentage( |
| 79 | + SPEED_RANGE, self._node.status |
| 80 | + ) |
| 81 | + |
| 82 | + @callback |
| 83 | + def async_on_update(self, event: NodeEventType, key: str) -> None: |
| 84 | + """Handle a control event from the ISY Node.""" |
| 85 | + self._update_fan_attrs() |
| 86 | + super().async_on_update(event, key) |
80 | 87 |
|
81 | 88 | async def async_set_percentage(self, percentage: int) -> None: |
82 | 89 | """Set node to speed percentage for the ISY fan device.""" |
@@ -106,24 +113,29 @@ class ISYFanProgramEntity(ISYProgramEntity, FanEntity): |
106 | 113 | """Representation of an ISY fan program.""" |
107 | 114 |
|
108 | 115 | _attr_supported_features = FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON |
| 116 | + _attr_speed_count = int_states_in_range(SPEED_RANGE) |
109 | 117 | _actions: Program |
110 | 118 |
|
111 | | - @property |
112 | | - def percentage(self) -> int | None: |
113 | | - """Return the current speed percentage.""" |
| 119 | + async def async_added_to_hass(self) -> None: |
| 120 | + """Subscribe to events and set initial state.""" |
| 121 | + await super().async_added_to_hass() |
| 122 | + self._update_fan_attrs() |
| 123 | + |
| 124 | + def _update_fan_attrs(self) -> None: |
114 | 125 | if self._node.status is None: |
115 | | - return None |
116 | | - return ranged_value_to_percentage(SPEED_RANGE, float(self._node.status)) |
117 | | - |
118 | | - @property |
119 | | - def speed_count(self) -> int: |
120 | | - """Return the number of speeds the fan supports.""" |
121 | | - return int_states_in_range(SPEED_RANGE) |
122 | | - |
123 | | - @property |
124 | | - def is_on(self) -> bool: |
125 | | - """Get if the fan is on.""" |
126 | | - return bool(self._node.status != 0) |
| 126 | + self._attr_is_on = None |
| 127 | + self._attr_percentage = None |
| 128 | + else: |
| 129 | + self._attr_is_on = bool(self._node.status != 0) |
| 130 | + self._attr_percentage = ranged_value_to_percentage( |
| 131 | + SPEED_RANGE, float(self._node.status) |
| 132 | + ) |
| 133 | + |
| 134 | + @callback |
| 135 | + def async_on_update(self, event: NodeEventType, key: str) -> None: |
| 136 | + """Handle the update event from the ISY Node.""" |
| 137 | + self._update_fan_attrs() |
| 138 | + super().async_on_update(event, key) |
127 | 139 |
|
128 | 140 | async def async_turn_off(self, **kwargs: Any) -> None: |
129 | 141 | """Send the turn on command to ISY fan program.""" |
|
0 commit comments