1 """Component for interacting with a Lutron RadioRA 2 system."""
3 from dataclasses
import dataclass
6 from pylutron
import Button, Keypad, Led, Lutron, OccupancyGroup, Output
13 from .const
import DOMAIN
16 Platform.BINARY_SENSOR,
25 _LOGGER = logging.getLogger(__name__)
28 ATTR_ACTION =
"action"
29 ATTR_FULL_ID =
"full_id"
33 @dataclass(slots=True, kw_only=True)
35 """Storage class for platform global data."""
38 binary_sensors: list[tuple[str, OccupancyGroup]]
39 buttons: list[tuple[str, Keypad, Button]]
40 covers: list[tuple[str, Output]]
41 fans: list[tuple[str, Output]]
42 lights: list[tuple[str, Output]]
43 scenes: list[tuple[str, Keypad, Button, Led]]
44 switches: list[tuple[str, Output]]
48 """Set up the Lutron integration."""
50 host = config_entry.data[CONF_HOST]
51 uid = config_entry.data[CONF_USERNAME]
52 pwd = config_entry.data[CONF_PASSWORD]
54 lutron_client = Lutron(host, uid, pwd)
55 await hass.async_add_executor_job(lutron_client.load_xml_db)
56 lutron_client.connect()
57 _LOGGER.debug(
"Connected to main repeater at %s", host)
59 entity_registry = er.async_get(hass)
60 device_registry = dr.async_get(hass)
73 _LOGGER.debug(
"Start adding devices")
74 for area
in lutron_client.areas:
75 _LOGGER.debug(
"Working on area %s", area.name)
76 for output
in area.outputs:
78 _LOGGER.debug(
"Working on output %s", output.type)
79 if output.type ==
"SYSTEM_SHADE":
80 entry_data.covers.append((area.name, output))
81 platform = Platform.COVER
82 elif output.type ==
"CEILING_FAN_TYPE":
83 entry_data.fans.append((area.name, output))
84 platform = Platform.FAN
85 elif output.is_dimmable:
86 entry_data.lights.append((area.name, output))
87 platform = Platform.LIGHT
89 entry_data.switches.append((area.name, output))
90 platform = Platform.SWITCH
98 entry_data.client.guid,
105 entry_data.client.guid,
108 for keypad
in area.keypads:
109 for button
in keypad.buttons:
111 if button.name !=
"Unknown Button" and button.button_type
in (
114 "SingleSceneRaiseLower",
119 (led
for led
in keypad.leds
if led.number == button.number),
122 entry_data.scenes.append((area.name, keypad, button, led))
124 platform = Platform.SCENE
131 entry_data.client.guid,
134 platform = Platform.SWITCH
141 entry_data.client.guid,
143 if button.button_type:
144 entry_data.buttons.append((area.name, keypad, button))
145 if area.occupancy_group
is not None:
146 entry_data.binary_sensors.append((area.name, area.occupancy_group))
147 platform = Platform.BINARY_SENSOR
152 area.occupancy_group.uuid,
153 area.occupancy_group.legacy_uuid,
154 entry_data.client.guid,
159 area.occupancy_group.uuid,
160 area.occupancy_group.legacy_uuid,
161 entry_data.client.guid,
164 device_registry.async_get_or_create(
165 config_entry_id=config_entry.entry_id,
166 identifiers={(DOMAIN, lutron_client.guid)},
167 manufacturer=
"Lutron",
168 name=
"Main repeater",
171 hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = entry_data
173 await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
180 entity_registry: er.EntityRegistry,
184 controller_guid: str,
186 """If uuid becomes available update to use it."""
191 unique_id = f
"{controller_guid}_{legacy_uuid}"
192 entity_id = entity_registry.async_get_entity_id(
193 domain=platform, platform=DOMAIN, unique_id=unique_id
197 new_unique_id = f
"{controller_guid}_{uuid}"
198 _LOGGER.debug(
"Updating entity id from %s to %s", unique_id, new_unique_id)
199 entity_registry.async_update_entity(entity_id, new_unique_id=new_unique_id)
204 device_registry: dr.DeviceRegistry,
207 controller_guid: str,
209 """If uuid becomes available update to use it."""
214 unique_id = f
"{controller_guid}_{legacy_uuid}"
215 device = device_registry.async_get_device(identifiers={(DOMAIN, unique_id)})
217 new_unique_id = f
"{controller_guid}_{uuid}"
218 _LOGGER.debug(
"Updating device id from %s to %s", unique_id, new_unique_id)
219 device_registry.async_update_device(
220 device.id, new_identifiers={(DOMAIN, new_unique_id)}
225 """Clean up resources and entities associated with the integration."""
226 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None _async_check_device_identifiers(HomeAssistant hass, dr.DeviceRegistry device_registry, str uuid, str legacy_uuid, str controller_guid)
None _async_check_entity_unique_id(HomeAssistant hass, er.EntityRegistry entity_registry, str platform, str uuid, str legacy_uuid, str controller_guid)
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)