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
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]

View file

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

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.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."""

View file

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

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)