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
16 changes: 16 additions & 0 deletions homeassistant/components/wirelesstag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import voluptuous as vol
from wirelesstagpy import WirelessTags
from wirelesstagpy.exceptions import WirelessTagsException
from wirelesstagpy.sensortag import SensorTag

from homeassistant.components import persistent_notification
from homeassistant.const import (
Expand All @@ -17,6 +18,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 @@ -126,6 +128,20 @@ def push_callback(tags_spec, event_spec):
self.api.start_monitoring(push_callback)


def async_migrate_unique_id(hass: HomeAssistant, tag: SensorTag, domain: str, key: str):
MartinHjelmare 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(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}"
if entity_id := registry.async_get_entity_id(domain, DOMAIN, old_unique_id):
_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."""
conf = config[DOMAIN]
Expand Down
12 changes: 7 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 All @@ -15,6 +15,7 @@
DOMAIN as WIRELESSTAG_DOMAIN,
SIGNAL_BINARY_EVENT_UPDATE,
WirelessTagBaseSensor,
async_migrate_unique_id,
)

# On means in range, Off means out of range
Expand Down Expand Up @@ -72,10 +73,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 +88,10 @@ 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:
async_migrate_unique_id(hass, 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 +102,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
18 changes: 12 additions & 6 deletions homeassistant/components/wirelesstag/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
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
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

from . import DOMAIN as WIRELESSTAG_DOMAIN, SIGNAL_TAG_UPDATE, WirelessTagBaseSensor
from . import (
DOMAIN as WIRELESSTAG_DOMAIN,
SIGNAL_TAG_UPDATE,
WirelessTagBaseSensor,
async_migrate_unique_id,
)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -68,10 +73,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 +88,10 @@ def setup_platform(
if key not in tag.allowed_sensor_types:
continue
description = SENSOR_TYPES[key]
async_migrate_unique_id(hass, 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 +106,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
32 changes: 19 additions & 13 deletions homeassistant/components/wirelesstag/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
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
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

from . import DOMAIN as WIRELESSTAG_DOMAIN, WirelessTagBaseSensor
from . import (
DOMAIN as WIRELESSTAG_DOMAIN,
WirelessTagBaseSensor,
async_migrate_unique_id,
)

SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
SwitchEntityDescription(
Expand Down Expand Up @@ -52,26 +56,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
):
async_migrate_unique_id(hass, 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 +88,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