From 3d9aa60e4ed0234684c988c5dc659f59750b9b42 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 18 Sep 2024 10:55:55 +0200 Subject: [PATCH] Move wirelesstag shared constants to separate module (#126192) --- .../components/wirelesstag/__init__.py | 29 ++----------------- .../components/wirelesstag/binary_sensor.py | 11 +++---- homeassistant/components/wirelesstag/const.py | 11 +++++++ .../components/wirelesstag/sensor.py | 15 ++++------ .../components/wirelesstag/switch.py | 10 +++---- homeassistant/components/wirelesstag/util.py | 28 ++++++++++++++++++ 6 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 homeassistant/components/wirelesstag/const.py create mode 100644 homeassistant/components/wirelesstag/util.py diff --git a/homeassistant/components/wirelesstag/__init__.py b/homeassistant/components/wirelesstag/__init__.py index 710255153c2..2bd2fbebac9 100644 --- a/homeassistant/components/wirelesstag/__init__.py +++ b/homeassistant/components/wirelesstag/__init__.py @@ -6,7 +6,6 @@ from requests.exceptions import ConnectTimeout, HTTPError 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 ( @@ -19,12 +18,13 @@ from homeassistant.const import ( 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 from homeassistant.helpers.typing import ConfigType +from .const import DOMAIN, SIGNAL_BINARY_EVENT_UPDATE, SIGNAL_TAG_UPDATE + _LOGGER = logging.getLogger(__name__) @@ -39,17 +39,8 @@ ATTR_TAG_POWER_CONSUMPTION = "power_consumption" NOTIFICATION_ID = "wirelesstag_notification" NOTIFICATION_TITLE = "Wireless Sensor Tag Setup" -DOMAIN = "wirelesstag" DEFAULT_ENTITY_NAMESPACE = "wirelesstag" -# Template for signal - first parameter is tag_id, -# second, tag manager mac address -SIGNAL_TAG_UPDATE = "wirelesstag.tag_info_updated_{}_{}" - -# Template for signal - tag_id, sensor type and -# tag manager mac address -SIGNAL_BINARY_EVENT_UPDATE = "wirelesstag.binary_event_updated_{}_{}_{}" - CONFIG_SCHEMA = vol.Schema( { DOMAIN: vol.Schema( @@ -129,22 +120,6 @@ class WirelessTagPlatform: self.api.start_monitoring(push_callback) -def async_migrate_unique_id( - hass: HomeAssistant, tag: SensorTag, domain: str, key: str -) -> None: - """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] diff --git a/homeassistant/components/wirelesstag/binary_sensor.py b/homeassistant/components/wirelesstag/binary_sensor.py index 052f6547dd2..cd8f058cce4 100644 --- a/homeassistant/components/wirelesstag/binary_sensor.py +++ b/homeassistant/components/wirelesstag/binary_sensor.py @@ -15,12 +15,9 @@ 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_BINARY_EVENT_UPDATE, - WirelessTagBaseSensor, - async_migrate_unique_id, -) +from . import WirelessTagBaseSensor +from .const import DOMAIN, SIGNAL_BINARY_EVENT_UPDATE +from .util import async_migrate_unique_id # On means in range, Off means out of range SENSOR_PRESENCE = "presence" @@ -84,7 +81,7 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the platform for a WirelessTags.""" - platform = hass.data[WIRELESSTAG_DOMAIN] + platform = hass.data[DOMAIN] sensors = [] tags = platform.tags diff --git a/homeassistant/components/wirelesstag/const.py b/homeassistant/components/wirelesstag/const.py new file mode 100644 index 00000000000..c1384606bf1 --- /dev/null +++ b/homeassistant/components/wirelesstag/const.py @@ -0,0 +1,11 @@ +"""Support for Wireless Sensor Tags.""" + +DOMAIN = "wirelesstag" + +# Template for signal - first parameter is tag_id, +# second, tag manager mac address +SIGNAL_TAG_UPDATE = "wirelesstag.tag_info_updated_{}_{}" + +# Template for signal - tag_id, sensor type and +# tag manager mac address +SIGNAL_BINARY_EVENT_UPDATE = "wirelesstag.binary_event_updated_{}_{}_{}" diff --git a/homeassistant/components/wirelesstag/sensor.py b/homeassistant/components/wirelesstag/sensor.py index 87906bdc2ae..9f7ed3cc4b0 100644 --- a/homeassistant/components/wirelesstag/sensor.py +++ b/homeassistant/components/wirelesstag/sensor.py @@ -20,12 +20,9 @@ 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, - async_migrate_unique_id, -) +from . import WirelessTagBaseSensor +from .const import DOMAIN, SIGNAL_TAG_UPDATE +from .util import async_migrate_unique_id _LOGGER = logging.getLogger(__name__) @@ -81,7 +78,7 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the sensor platform.""" - platform = hass.data[WIRELESSTAG_DOMAIN] + platform = hass.data[DOMAIN] sensors = [] tags = platform.tags for tag in tags.values(): @@ -113,9 +110,7 @@ class WirelessTagSensor(WirelessTagBaseSensor, SensorEntity): # sensor.wirelesstag_bedroom_temperature # and not as sensor.bedroom for temperature and # sensor.bedroom_2 for humidity - self.entity_id = ( - f"sensor.{WIRELESSTAG_DOMAIN}_{self.underscored_name}_{self._sensor_type}" - ) + self.entity_id = f"sensor.{DOMAIN}_{self.underscored_name}_{self._sensor_type}" async def async_added_to_hass(self) -> None: """Register callbacks.""" diff --git a/homeassistant/components/wirelesstag/switch.py b/homeassistant/components/wirelesstag/switch.py index 239461df4ea..a5323ab3f1d 100644 --- a/homeassistant/components/wirelesstag/switch.py +++ b/homeassistant/components/wirelesstag/switch.py @@ -17,11 +17,9 @@ 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, - async_migrate_unique_id, -) +from . import WirelessTagBaseSensor +from .const import DOMAIN +from .util import async_migrate_unique_id SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = ( SwitchEntityDescription( @@ -64,7 +62,7 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up switches for a Wireless Sensor Tags.""" - platform = hass.data[WIRELESSTAG_DOMAIN] + platform = hass.data[DOMAIN] tags = platform.load_tags() monitored_conditions = config[CONF_MONITORED_CONDITIONS] diff --git a/homeassistant/components/wirelesstag/util.py b/homeassistant/components/wirelesstag/util.py new file mode 100644 index 00000000000..1b5d6551fc4 --- /dev/null +++ b/homeassistant/components/wirelesstag/util.py @@ -0,0 +1,28 @@ +"""Support for Wireless Sensor Tags.""" + +import logging + +from wirelesstagpy.sensortag import SensorTag + +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +def async_migrate_unique_id( + hass: HomeAssistant, tag: SensorTag, domain: str, key: str +) -> None: + """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)