Migrate glances unique_id to new format (#74033)

This commit is contained in:
Rami Mosleh 2022-06-30 22:10:19 +03:00 committed by GitHub
parent 768b98ae77
commit 9a9fea4423
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,8 +1,9 @@
"""Support gathering system information of hosts which are running glances."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, STATE_UNAVAILABLE
from homeassistant.const import CONF_NAME, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -18,14 +19,34 @@ async def async_setup_entry(
) -> None:
"""Set up the Glances sensors."""
client = hass.data[DOMAIN][config_entry.entry_id]
client: GlancesData = hass.data[DOMAIN][config_entry.entry_id]
name = config_entry.data[CONF_NAME]
dev = []
@callback
def _migrate_old_unique_ids(
hass: HomeAssistant, old_unique_id: str, new_key: str
) -> None:
"""Migrate unique IDs to the new format."""
ent_reg = entity_registry.async_get(hass)
if entity_id := ent_reg.async_get_entity_id(
Platform.SENSOR, DOMAIN, old_unique_id
):
ent_reg.async_update_entity(
entity_id, new_unique_id=f"{config_entry.entry_id}-{new_key}"
)
for description in SENSOR_TYPES:
if description.type == "fs":
# fs will provide a list of disks attached
for disk in client.api.data[description.type]:
_migrate_old_unique_ids(
hass,
f"{client.host}-{name} {disk['mnt_point']} {description.name_suffix}",
f"{disk['mnt_point']}-{description.key}",
)
dev.append(
GlancesSensor(
client,
@ -38,6 +59,11 @@ async def async_setup_entry(
# sensors will provide temp for different devices
for sensor in client.api.data[description.type]:
if sensor["type"] == description.key:
_migrate_old_unique_ids(
hass,
f"{client.host}-{name} {sensor['label']} {description.name_suffix}",
f"{sensor['label']}-{description.key}",
)
dev.append(
GlancesSensor(
client,
@ -48,8 +74,18 @@ async def async_setup_entry(
)
elif description.type == "raid":
for raid_device in client.api.data[description.type]:
_migrate_old_unique_ids(
hass,
f"{client.host}-{name} {raid_device} {description.name_suffix}",
f"{raid_device}-{description.key}",
)
dev.append(GlancesSensor(client, name, raid_device, description))
elif client.api.data[description.type]:
_migrate_old_unique_ids(
hass,
f"{client.host}-{name} {description.name_suffix}",
f"-{description.key}",
)
dev.append(
GlancesSensor(
client,
@ -87,11 +123,7 @@ class GlancesSensor(SensorEntity):
manufacturer="Glances",
name=name,
)
@property
def unique_id(self):
"""Set unique_id for sensor."""
return f"{self.glances_data.host}-{self.name}"
self._attr_unique_id = f"{self.glances_data.config_entry.entry_id}-{sensor_name_prefix}-{description.key}"
@property
def available(self):