Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wirelesstag unique_id to use uuid instead of tag_id #104394

Merged
merged 12 commits into from
Nov 30, 2023
Merged
15 changes: 15 additions & 0 deletions homeassistant/components/wirelesstag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
UnitOfElectricPotential,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.helpers.entity import Entity
Expand Down Expand Up @@ -125,6 +126,20 @@ def push_callback(tags_spec, event_spec):

self.api.start_monitoring(push_callback)

def async_migrate_unique_id(self, tag, domain, key):
edenhaus marked this conversation as resolved.
Show resolved Hide resolved
"""Migrate old unique id to new one with use of tag's uuid."""
registry = er.async_get(self.hass)
new_unique_id = f"{tag.uuid}_{key}"

if registry.async_get_entity_id(domain, DOMAIN, new_unique_id):
return

old_unique_id = f"{tag.tag_id}_{key}"
entity_id = registry.async_get_entity_id(domain, DOMAIN, old_unique_id)
if entity_id is not None:
sergeymaysak marked this conversation as resolved.
Show resolved Hide resolved
_LOGGER.debug("Updating unique id for %s %s", key, entity_id)
registry.async_update_entity(entity_id, new_unique_id=new_unique_id)


def setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Wireless Sensor Tag component."""
Expand Down
13 changes: 8 additions & 5 deletions homeassistant/components/wirelesstag/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import voluptuous as vol

from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
from homeassistant.const import CONF_MONITORED_CONDITIONS, STATE_OFF, STATE_ON
from homeassistant.const import CONF_MONITORED_CONDITIONS, STATE_OFF, STATE_ON, Platform
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
Expand Down Expand Up @@ -72,10 +72,10 @@
)


def setup_platform(
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the platform for a WirelessTags."""
Expand All @@ -87,9 +87,12 @@ def setup_platform(
allowed_sensor_types = tag.supported_binary_events_types
for sensor_type in config[CONF_MONITORED_CONDITIONS]:
if sensor_type in allowed_sensor_types:
platform.async_migrate_unique_id(
tag, Platform.BINARY_SENSOR, sensor_type
)
sensors.append(WirelessTagBinarySensor(platform, tag, sensor_type))

add_entities(sensors, True)
async_add_entities(sensors, True)


class WirelessTagBinarySensor(WirelessTagBaseSensor, BinarySensorEntity):
Expand All @@ -100,7 +103,7 @@ def __init__(self, api, tag, sensor_type):
super().__init__(api, tag)
self._sensor_type = sensor_type
self._name = f"{self._tag.name} {self.event.human_readable_name}"
self._attr_unique_id = f"{self.tag_id}_{self._sensor_type}"
frenck marked this conversation as resolved.
Show resolved Hide resolved
self._attr_unique_id = f"{self._uuid}_{self._sensor_type}"

async def async_added_to_hass(self) -> None:
"""Register callbacks."""
Expand Down
11 changes: 6 additions & 5 deletions homeassistant/components/wirelesstag/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import CONF_MONITORED_CONDITIONS
from homeassistant.const import CONF_MONITORED_CONDITIONS, Platform
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
Expand Down Expand Up @@ -68,10 +68,10 @@
)


def setup_platform(
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
Expand All @@ -83,9 +83,10 @@ def setup_platform(
if key not in tag.allowed_sensor_types:
continue
description = SENSOR_TYPES[key]
platform.async_migrate_unique_id(tag, Platform.SENSOR, description.key)
sensors.append(WirelessTagSensor(platform, tag, description))

add_entities(sensors, True)
async_add_entities(sensors, True)


class WirelessTagSensor(WirelessTagBaseSensor, SensorEntity):
Expand All @@ -100,7 +101,7 @@ def __init__(self, api, tag, description):
self._sensor_type = description.key
self.entity_description = description
self._name = self._tag.name
self._attr_unique_id = f"{self.tag_id}_{self._sensor_type}"
self._attr_unique_id = f"{self._uuid}_{self._sensor_type}"

# I want to see entity_id as:
# sensor.wirelesstag_bedroom_temperature
Expand Down
26 changes: 14 additions & 12 deletions homeassistant/components/wirelesstag/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
SwitchEntity,
SwitchEntityDescription,
)
from homeassistant.const import CONF_MONITORED_CONDITIONS
from homeassistant.const import CONF_MONITORED_CONDITIONS, Platform
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -52,26 +52,28 @@
)


def setup_platform(
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up switches for a Wireless Sensor Tags."""
platform = hass.data[WIRELESSTAG_DOMAIN]

tags = platform.load_tags()
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
entities = [
WirelessTagSwitch(platform, tag, description)
for tag in tags.values()
for description in SWITCH_TYPES
if description.key in monitored_conditions
and description.key in tag.allowed_monitoring_types
]
entities = []
for tag in tags.values():
for description in SWITCH_TYPES:
if (
description.key in monitored_conditions
and description.key in tag.allowed_monitoring_types
):
platform.async_migrate_unique_id(tag, Platform.SWITCH, description.key)
entities.append(WirelessTagSwitch(platform, tag, description))

add_entities(entities, True)
async_add_entities(entities, True)


class WirelessTagSwitch(WirelessTagBaseSensor, SwitchEntity):
Expand All @@ -82,7 +84,7 @@ def __init__(self, api, tag, description: SwitchEntityDescription) -> None:
super().__init__(api, tag)
self.entity_description = description
self._name = f"{self._tag.name} {description.name}"
self._attr_unique_id = f"{self.tag_id}_{description.key}"
self._attr_unique_id = f"{self._uuid}_{description.key}"

def turn_on(self, **kwargs: Any) -> None:
"""Turn on the switch."""
Expand Down