1 """Support for ISY select entities."""
3 from __future__
import annotations
5 from typing
import cast
7 from pyisy.constants
import (
11 COMMAND_FRIENDLY_NAME,
19 UOM_INDEX
as ISY_UOM_INDEX,
22 from pyisy.helpers
import EventListener, NodeProperty
23 from pyisy.nodes
import Node, NodeChangedEvent
40 from .const
import _LOGGER, DOMAIN, UOM_INDEX
41 from .entity
import ISYAuxControlEntity
42 from .models
import IsyData
46 """Return a formatted ramp rate time string."""
48 return f
"{(float(i)/60):.1f} {UnitOfTime.MINUTES}"
49 return f
"{i} {UnitOfTime.SECONDS}"
52 RAMP_RATE_OPTIONS = [
time_string(rate)
for rate
in INSTEON_RAMP_RATES.values()]
53 BACKLIGHT_MEMORY_FILTER = {
"memory": DEV_BL_ADDR,
"cmd1": DEV_CMD_MEMORY_WRITE}
58 config_entry: ConfigEntry,
59 async_add_entities: AddEntitiesCallback,
61 """Set up ISY/IoX select entities from config entry."""
62 isy_data: IsyData = hass.data[DOMAIN][config_entry.entry_id]
63 device_info = isy_data.devices
65 ISYAuxControlIndexSelectEntity
66 | ISYRampRateSelectEntity
67 | ISYBacklightSelectEntity
70 for node, control
in isy_data.aux_properties[Platform.SELECT]:
71 name = COMMAND_FRIENDLY_NAME.get(control, control).replace(
"_",
" ").title()
72 if node.address != node.primary_node:
73 name = f
"{node.name} {name}"
76 if control == PROP_RAMP_RATE:
77 options = RAMP_RATE_OPTIONS
78 elif control == CMD_BACKLIGHT:
79 options = BACKLIGHT_INDEX
80 elif uom := node.aux_properties[control].uom == UOM_INDEX:
81 if options_dict := UOM_TO_STATES.get(uom):
82 options =
list(options_dict.values())
85 key=f
"{node.address}_{control}",
87 entity_category=EntityCategory.CONFIG,
93 "unique_id": f
"{isy_data.uid_base(node)}_{control}",
94 "description": description,
95 "device_info": device_info.get(node.primary_node),
98 if control == PROP_RAMP_RATE:
101 if control == CMD_BACKLIGHT:
104 if node.uom == UOM_INDEX
and options:
109 "ISY missing node index unit definitions for %s: %s", node.name, name
115 """Representation of a ISY/IoX Aux Control Ramp Rate Select entity."""
119 """Return the selected entity option to represent the entity state."""
120 node_prop: NodeProperty = self.
_node_node.aux_properties[self.
_control_control]
121 if node_prop.value == ISY_VALUE_UNKNOWN:
124 return RAMP_RATE_OPTIONS[
int(node_prop.value)]
127 """Change the selected option."""
129 await self.
_node_node.set_ramp_rate(RAMP_RATE_OPTIONS.index(option))
133 """Representation of a ISY/IoX Aux Control Index Select entity."""
137 """Return the selected entity option to represent the entity state."""
138 node_prop: NodeProperty = self.
_node_node.aux_properties[self.
_control_control]
139 if node_prop.value == ISY_VALUE_UNKNOWN:
142 if options_dict := UOM_TO_STATES.get(node_prop.uom):
143 return cast(str, options_dict.get(node_prop.value, node_prop.value))
144 return cast(str, node_prop.formatted)
147 """Change the selected option."""
148 node_prop: NodeProperty = self.
_node_node.aux_properties[self.
_control_control]
150 await self.
_node_node.send_cmd(
151 self.
_control_control, val=self.
optionsoptions.index(option), uom=node_prop.uom
156 """Representation of a ISY/IoX Backlight Select entity."""
158 _assumed_state =
True
165 description: SelectEntityDescription,
166 device_info: DeviceInfo |
None,
168 """Initialize the ISY Backlight Select entity."""
169 super().
__init__(node, control, unique_id, description, device_info)
174 """Load the last known state when added to hass."""
178 )
and last_state.state
not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
185 TAG_ADDRESS: self.
_node_node.address,
186 ATTR_ACTION: DEV_MEMORY,
193 """Handle a memory write event from the ISY Node."""
194 if not (BACKLIGHT_MEMORY_FILTER.items() <= event.event_info.items()):
196 option = BACKLIGHT_INDEX[event.event_info[
"value"]]
203 """Change the selected option."""
205 if not await self.
_node_node.send_cmd(
206 CMD_BACKLIGHT, val=BACKLIGHT_INDEX.index(option), uom=ISY_UOM_INDEX
209 f
"Could not set backlight to {option} for {self._node.address}"
str|None current_option(self)
None async_select_option(self, str option)
None async_added_to_hass(self)
None __init__(self, Node node, str control, str unique_id, SelectEntityDescription description, DeviceInfo|None device_info)
None async_on_memory_write(self, NodeChangedEvent event, str key)
None async_select_option(self, str option)
None async_select_option(self, str option)
str|None current_option(self)
None async_write_ha_state(self)
State|None async_get_last_state(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)