mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Migrate glances
unique_id to new format (#74033)
This commit is contained in:
parent
768b98ae77
commit
9a9fea4423
@ -1,8 +1,9 @@
|
|||||||
"""Support gathering system information of hosts which are running glances."""
|
"""Support gathering system information of hosts which are running glances."""
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant, callback
|
||||||
|
from homeassistant.helpers import entity_registry
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -18,14 +19,34 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Glances sensors."""
|
"""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]
|
name = config_entry.data[CONF_NAME]
|
||||||
dev = []
|
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:
|
for description in SENSOR_TYPES:
|
||||||
if description.type == "fs":
|
if description.type == "fs":
|
||||||
# fs will provide a list of disks attached
|
# fs will provide a list of disks attached
|
||||||
for disk in client.api.data[description.type]:
|
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(
|
dev.append(
|
||||||
GlancesSensor(
|
GlancesSensor(
|
||||||
client,
|
client,
|
||||||
@ -38,6 +59,11 @@ async def async_setup_entry(
|
|||||||
# sensors will provide temp for different devices
|
# sensors will provide temp for different devices
|
||||||
for sensor in client.api.data[description.type]:
|
for sensor in client.api.data[description.type]:
|
||||||
if sensor["type"] == description.key:
|
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(
|
dev.append(
|
||||||
GlancesSensor(
|
GlancesSensor(
|
||||||
client,
|
client,
|
||||||
@ -48,8 +74,18 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
elif description.type == "raid":
|
elif description.type == "raid":
|
||||||
for raid_device in client.api.data[description.type]:
|
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))
|
dev.append(GlancesSensor(client, name, raid_device, description))
|
||||||
elif client.api.data[description.type]:
|
elif client.api.data[description.type]:
|
||||||
|
_migrate_old_unique_ids(
|
||||||
|
hass,
|
||||||
|
f"{client.host}-{name} {description.name_suffix}",
|
||||||
|
f"-{description.key}",
|
||||||
|
)
|
||||||
dev.append(
|
dev.append(
|
||||||
GlancesSensor(
|
GlancesSensor(
|
||||||
client,
|
client,
|
||||||
@ -87,11 +123,7 @@ class GlancesSensor(SensorEntity):
|
|||||||
manufacturer="Glances",
|
manufacturer="Glances",
|
||||||
name=name,
|
name=name,
|
||||||
)
|
)
|
||||||
|
self._attr_unique_id = f"{self.glances_data.config_entry.entry_id}-{sensor_name_prefix}-{description.key}"
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Set unique_id for sensor."""
|
|
||||||
return f"{self.glances_data.host}-{self.name}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user