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

Move wirelesstag shared constants to separate module #126192

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions homeassistant/components/wirelesstag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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 @@ -19,12 +18,13 @@
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__)


Expand All @@ -39,17 +39,8 @@
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(
Expand Down Expand Up @@ -129,22 +120,6 @@ 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
) -> 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]
Expand Down
11 changes: 4 additions & 7 deletions homeassistant/components/wirelesstag/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
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"
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions homeassistant/components/wirelesstag/const.py
Original file line number Diff line number Diff line change
@@ -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_{}_{}_{}"
15 changes: 5 additions & 10 deletions homeassistant/components/wirelesstag/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
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__)

Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -113,9 +110,7 @@ def __init__(self, api, tag, description):
# 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."""
Expand Down
10 changes: 4 additions & 6 deletions homeassistant/components/wirelesstag/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
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(
Expand Down Expand Up @@ -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]
Expand Down
28 changes: 28 additions & 0 deletions homeassistant/components/wirelesstag/util.py
Original file line number Diff line number Diff line change
@@ -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)