Move wirelesstag shared constants to separate module (#126192)

This commit is contained in:
epenet 2024-09-18 10:55:55 +02:00 committed by GitHub
parent 93de46b50e
commit 3d9aa60e4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 54 additions and 50 deletions

View File

@ -6,7 +6,6 @@ from requests.exceptions import ConnectTimeout, HTTPError
import voluptuous as vol import voluptuous as vol
from wirelesstagpy import WirelessTags from wirelesstagpy import WirelessTags
from wirelesstagpy.exceptions import WirelessTagsException from wirelesstagpy.exceptions import WirelessTagsException
from wirelesstagpy.sensortag import SensorTag
from homeassistant.components import persistent_notification from homeassistant.components import persistent_notification
from homeassistant.const import ( from homeassistant.const import (
@ -19,12 +18,13 @@ from homeassistant.const import (
UnitOfElectricPotential, UnitOfElectricPotential,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN, SIGNAL_BINARY_EVENT_UPDATE, SIGNAL_TAG_UPDATE
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -39,17 +39,8 @@ ATTR_TAG_POWER_CONSUMPTION = "power_consumption"
NOTIFICATION_ID = "wirelesstag_notification" NOTIFICATION_ID = "wirelesstag_notification"
NOTIFICATION_TITLE = "Wireless Sensor Tag Setup" NOTIFICATION_TITLE = "Wireless Sensor Tag Setup"
DOMAIN = "wirelesstag"
DEFAULT_ENTITY_NAMESPACE = "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( CONFIG_SCHEMA = vol.Schema(
{ {
DOMAIN: vol.Schema( DOMAIN: vol.Schema(
@ -129,22 +120,6 @@ class WirelessTagPlatform:
self.api.start_monitoring(push_callback) 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: def setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Wireless Sensor Tag component.""" """Set up the Wireless Sensor Tag component."""
conf = config[DOMAIN] conf = config[DOMAIN]

View File

@ -15,12 +15,9 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import ( from . import WirelessTagBaseSensor
DOMAIN as WIRELESSTAG_DOMAIN, from .const import DOMAIN, SIGNAL_BINARY_EVENT_UPDATE
SIGNAL_BINARY_EVENT_UPDATE, from .util import async_migrate_unique_id
WirelessTagBaseSensor,
async_migrate_unique_id,
)
# On means in range, Off means out of range # On means in range, Off means out of range
SENSOR_PRESENCE = "presence" SENSOR_PRESENCE = "presence"
@ -84,7 +81,7 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the platform for a WirelessTags.""" """Set up the platform for a WirelessTags."""
platform = hass.data[WIRELESSTAG_DOMAIN] platform = hass.data[DOMAIN]
sensors = [] sensors = []
tags = platform.tags tags = platform.tags

View File

@ -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_{}_{}_{}"

View File

@ -20,12 +20,9 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import ( from . import WirelessTagBaseSensor
DOMAIN as WIRELESSTAG_DOMAIN, from .const import DOMAIN, SIGNAL_TAG_UPDATE
SIGNAL_TAG_UPDATE, from .util import async_migrate_unique_id
WirelessTagBaseSensor,
async_migrate_unique_id,
)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -81,7 +78,7 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the sensor platform.""" """Set up the sensor platform."""
platform = hass.data[WIRELESSTAG_DOMAIN] platform = hass.data[DOMAIN]
sensors = [] sensors = []
tags = platform.tags tags = platform.tags
for tag in tags.values(): for tag in tags.values():
@ -113,9 +110,7 @@ class WirelessTagSensor(WirelessTagBaseSensor, SensorEntity):
# sensor.wirelesstag_bedroom_temperature # sensor.wirelesstag_bedroom_temperature
# and not as sensor.bedroom for temperature and # and not as sensor.bedroom for temperature and
# sensor.bedroom_2 for humidity # sensor.bedroom_2 for humidity
self.entity_id = ( self.entity_id = f"sensor.{DOMAIN}_{self.underscored_name}_{self._sensor_type}"
f"sensor.{WIRELESSTAG_DOMAIN}_{self.underscored_name}_{self._sensor_type}"
)
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Register callbacks.""" """Register callbacks."""

View File

@ -17,11 +17,9 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import ( from . import WirelessTagBaseSensor
DOMAIN as WIRELESSTAG_DOMAIN, from .const import DOMAIN
WirelessTagBaseSensor, from .util import async_migrate_unique_id
async_migrate_unique_id,
)
SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = ( SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
SwitchEntityDescription( SwitchEntityDescription(
@ -64,7 +62,7 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up switches for a Wireless Sensor Tags.""" """Set up switches for a Wireless Sensor Tags."""
platform = hass.data[WIRELESSTAG_DOMAIN] platform = hass.data[DOMAIN]
tags = platform.load_tags() tags = platform.load_tags()
monitored_conditions = config[CONF_MONITORED_CONDITIONS] monitored_conditions = config[CONF_MONITORED_CONDITIONS]

View File

@ -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)