mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Fix uuid issue in Lutron (#110524)
This commit is contained in:
parent
31ce43212a
commit
88f72009c6
@ -16,8 +16,8 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
import homeassistant.helpers.device_registry as dr
|
|
||||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
@ -186,6 +186,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
lutron_client.connect()
|
lutron_client.connect()
|
||||||
_LOGGER.info("Connected to main repeater at %s", host)
|
_LOGGER.info("Connected to main repeater at %s", host)
|
||||||
|
|
||||||
|
entity_registry = er.async_get(hass)
|
||||||
|
device_registry = dr.async_get(hass)
|
||||||
|
|
||||||
entry_data = LutronData(
|
entry_data = LutronData(
|
||||||
client=lutron_client,
|
client=lutron_client,
|
||||||
binary_sensors=[],
|
binary_sensors=[],
|
||||||
@ -201,17 +204,39 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
for area in lutron_client.areas:
|
for area in lutron_client.areas:
|
||||||
_LOGGER.debug("Working on area %s", area.name)
|
_LOGGER.debug("Working on area %s", area.name)
|
||||||
for output in area.outputs:
|
for output in area.outputs:
|
||||||
|
platform = None
|
||||||
_LOGGER.debug("Working on output %s", output.type)
|
_LOGGER.debug("Working on output %s", output.type)
|
||||||
if output.type == "SYSTEM_SHADE":
|
if output.type == "SYSTEM_SHADE":
|
||||||
entry_data.covers.append((area.name, output))
|
entry_data.covers.append((area.name, output))
|
||||||
|
platform = Platform.COVER
|
||||||
elif output.type == "CEILING_FAN_TYPE":
|
elif output.type == "CEILING_FAN_TYPE":
|
||||||
entry_data.fans.append((area.name, output))
|
entry_data.fans.append((area.name, output))
|
||||||
|
platform = Platform.FAN
|
||||||
# Deprecated, should be removed in 2024.8
|
# Deprecated, should be removed in 2024.8
|
||||||
entry_data.lights.append((area.name, output))
|
entry_data.lights.append((area.name, output))
|
||||||
elif output.is_dimmable:
|
elif output.is_dimmable:
|
||||||
entry_data.lights.append((area.name, output))
|
entry_data.lights.append((area.name, output))
|
||||||
|
platform = Platform.LIGHT
|
||||||
else:
|
else:
|
||||||
entry_data.switches.append((area.name, output))
|
entry_data.switches.append((area.name, output))
|
||||||
|
platform = Platform.SWITCH
|
||||||
|
|
||||||
|
_async_check_entity_unique_id(
|
||||||
|
hass,
|
||||||
|
entity_registry,
|
||||||
|
platform,
|
||||||
|
output.uuid,
|
||||||
|
output.legacy_uuid,
|
||||||
|
entry_data.client.guid,
|
||||||
|
)
|
||||||
|
_async_check_device_identifiers(
|
||||||
|
hass,
|
||||||
|
device_registry,
|
||||||
|
output.uuid,
|
||||||
|
output.legacy_uuid,
|
||||||
|
entry_data.client.guid,
|
||||||
|
)
|
||||||
|
|
||||||
for keypad in area.keypads:
|
for keypad in area.keypads:
|
||||||
for button in keypad.buttons:
|
for button in keypad.buttons:
|
||||||
# If the button has a function assigned to it, add it as a scene
|
# If the button has a function assigned to it, add it as a scene
|
||||||
@ -228,11 +253,46 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
)
|
)
|
||||||
entry_data.scenes.append((area.name, keypad, button, led))
|
entry_data.scenes.append((area.name, keypad, button, led))
|
||||||
|
|
||||||
|
platform = Platform.SCENE
|
||||||
|
_async_check_entity_unique_id(
|
||||||
|
hass,
|
||||||
|
entity_registry,
|
||||||
|
platform,
|
||||||
|
button.uuid,
|
||||||
|
button.legacy_uuid,
|
||||||
|
entry_data.client.guid,
|
||||||
|
)
|
||||||
|
if led is not None:
|
||||||
|
platform = Platform.SWITCH
|
||||||
|
_async_check_entity_unique_id(
|
||||||
|
hass,
|
||||||
|
entity_registry,
|
||||||
|
platform,
|
||||||
|
led.uuid,
|
||||||
|
led.legacy_uuid,
|
||||||
|
entry_data.client.guid,
|
||||||
|
)
|
||||||
|
|
||||||
entry_data.buttons.append(LutronButton(hass, area.name, keypad, button))
|
entry_data.buttons.append(LutronButton(hass, area.name, keypad, button))
|
||||||
if area.occupancy_group is not None:
|
if area.occupancy_group is not None:
|
||||||
entry_data.binary_sensors.append((area.name, area.occupancy_group))
|
entry_data.binary_sensors.append((area.name, area.occupancy_group))
|
||||||
|
platform = Platform.BINARY_SENSOR
|
||||||
|
_async_check_entity_unique_id(
|
||||||
|
hass,
|
||||||
|
entity_registry,
|
||||||
|
platform,
|
||||||
|
area.occupancy_group.uuid,
|
||||||
|
area.occupancy_group.legacy_uuid,
|
||||||
|
entry_data.client.guid,
|
||||||
|
)
|
||||||
|
_async_check_device_identifiers(
|
||||||
|
hass,
|
||||||
|
device_registry,
|
||||||
|
area.occupancy_group.uuid,
|
||||||
|
area.occupancy_group.legacy_uuid,
|
||||||
|
entry_data.client.guid,
|
||||||
|
)
|
||||||
|
|
||||||
device_registry = dr.async_get(hass)
|
|
||||||
device_registry.async_get_or_create(
|
device_registry.async_get_or_create(
|
||||||
config_entry_id=config_entry.entry_id,
|
config_entry_id=config_entry.entry_id,
|
||||||
identifiers={(DOMAIN, lutron_client.guid)},
|
identifiers={(DOMAIN, lutron_client.guid)},
|
||||||
@ -247,6 +307,52 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def _async_check_entity_unique_id(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
platform: str,
|
||||||
|
uuid: str,
|
||||||
|
legacy_uuid: str,
|
||||||
|
controller_guid: str,
|
||||||
|
) -> None:
|
||||||
|
"""If uuid becomes available update to use it."""
|
||||||
|
|
||||||
|
if not uuid:
|
||||||
|
return
|
||||||
|
|
||||||
|
unique_id = f"{controller_guid}_{legacy_uuid}"
|
||||||
|
entity_id = entity_registry.async_get_entity_id(
|
||||||
|
domain=platform, platform=DOMAIN, unique_id=unique_id
|
||||||
|
)
|
||||||
|
|
||||||
|
if entity_id:
|
||||||
|
new_unique_id = f"{controller_guid}_{uuid}"
|
||||||
|
_LOGGER.debug("Updating entity id from %s to %s", unique_id, new_unique_id)
|
||||||
|
entity_registry.async_update_entity(entity_id, new_unique_id=new_unique_id)
|
||||||
|
|
||||||
|
|
||||||
|
def _async_check_device_identifiers(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
device_registry: dr.DeviceRegistry,
|
||||||
|
uuid: str,
|
||||||
|
legacy_uuid: str,
|
||||||
|
controller_guid: str,
|
||||||
|
) -> None:
|
||||||
|
"""If uuid becomes available update to use it."""
|
||||||
|
|
||||||
|
if not uuid:
|
||||||
|
return
|
||||||
|
|
||||||
|
unique_id = f"{controller_guid}_{legacy_uuid}"
|
||||||
|
device = device_registry.async_get_device(identifiers={(DOMAIN, unique_id)})
|
||||||
|
if device:
|
||||||
|
new_unique_id = f"{controller_guid}_{uuid}"
|
||||||
|
_LOGGER.debug("Updating device id from %s to %s", unique_id, new_unique_id)
|
||||||
|
device_registry.async_update_device(
|
||||||
|
device.id, new_identifiers={(DOMAIN, new_unique_id)}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Clean up resources and entities associated with the integration."""
|
"""Clean up resources and entities associated with the integration."""
|
||||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
@ -41,11 +41,11 @@ class LutronBaseEntity(Entity):
|
|||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str | None:
|
def unique_id(self) -> str:
|
||||||
"""Return a unique ID."""
|
"""Return a unique ID."""
|
||||||
# Temporary fix for https://github.com/thecynic/pylutron/issues/70
|
|
||||||
if self._lutron_device.uuid is None:
|
if self._lutron_device.uuid is None:
|
||||||
return None
|
return f"{self._controller.guid}_{self._lutron_device.legacy_uuid}"
|
||||||
return f"{self._controller.guid}_{self._lutron_device.uuid}"
|
return f"{self._controller.guid}_{self._lutron_device.uuid}"
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
@ -63,7 +63,7 @@ class LutronDevice(LutronBaseEntity):
|
|||||||
"""Initialize the device."""
|
"""Initialize the device."""
|
||||||
super().__init__(area_name, lutron_device, controller)
|
super().__init__(area_name, lutron_device, controller)
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, lutron_device.uuid)},
|
identifiers={(DOMAIN, self.unique_id)},
|
||||||
manufacturer="Lutron",
|
manufacturer="Lutron",
|
||||||
name=lutron_device.name,
|
name=lutron_device.name,
|
||||||
suggested_area=area_name,
|
suggested_area=area_name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user