Skip to content

Commit

Permalink
Merge pull request #249 from loopj/device-serial-numbers
Browse files Browse the repository at this point in the history
Populate serial_number for Master and StationObject devices
  • Loading branch information
loopj authored Jan 8, 2025
2 parents 06f3f61 + f684d96 commit 3b8acdf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
13 changes: 12 additions & 1 deletion custom_components/vantage/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

from aiovantage import Vantage, VantageEvent
from aiovantage.controllers import BaseController
from aiovantage.models import LocationObject, Master, Parent, SystemObject
from aiovantage.models import (
LocationObject,
Master,
Parent,
StationObject,
SystemObject,
)

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
Expand Down Expand Up @@ -99,6 +105,11 @@ def vantage_device_info(client: Vantage, obj: SystemObject) -> DeviceInfo:
):
device_info["suggested_area"] = area.name

# Attach serial number for devices that have one
if isinstance(obj, Master) or isinstance(obj, StationObject):
if obj.serial_number:
device_info["serial_number"] = str(obj.serial_number)

# Set up device relationships
if not isinstance(obj, Master):
if (
Expand Down
23 changes: 23 additions & 0 deletions custom_components/vantage/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import entity_registry as er

from .const import LOGGER

Expand All @@ -11,6 +12,7 @@ async def async_migrate_data(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Run all Vantage data migrations."""

async_delete_back_boxes(hass, entry)
async_delete_serial_number_entities(hass, entry)


def async_delete_back_boxes(hass: HomeAssistant, entry: ConfigEntry) -> None:
Expand All @@ -28,3 +30,24 @@ def async_delete_back_boxes(hass: HomeAssistant, entry: ConfigEntry) -> None:

for device in back_box_devices:
dev_reg.async_remove_device(device.id)


def async_delete_serial_number_entities(
hass: HomeAssistant, entry: ConfigEntry
) -> None:
"""Delete serial number entities from the entity registry."""
ent_reg = er.async_get(hass)

serial_number_entities = [
entity
for entity in er.async_entries_for_config_entry(ent_reg, entry.entry_id)
if entity.unique_id.endswith(":serial_number")
]

if serial_number_entities:
LOGGER.debug(
f"Deleting {len(serial_number_entities)} serial number entities from the registry."
)

for entity in serial_number_entities:
ent_reg.async_remove(entity.entity_id)
18 changes: 0 additions & 18 deletions custom_components/vantage/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ async def async_setup_entry(
register_items(vantage.anemo_sensors, VantageWindSensor)
register_items(vantage.light_sensors, VantageLightSensor)
register_items(vantage.omni_sensors, VantageOmniSensor)
register_items(vantage.masters, VantageMasterSerial)
register_items(vantage.masters, VantageMasterIP)


Expand Down Expand Up @@ -130,23 +129,6 @@ def native_value(self) -> int | Decimal | None:
return self.obj.level


class VantageMasterSerial(VantageEntity[Master], SensorEntity):
"""Vantage controller serial number sensor entity."""

_attr_icon = "mdi:barcode"
_attr_entity_category = EntityCategory.DIAGNOSTIC

@property
def name(self) -> str | None:
"""Return the name of the entity."""
return "Serial Number"

def __post_init__(self) -> None:
"""Initialize a Vantage master serial number."""
self._attr_unique_id = f"{self.obj.id}:serial_number"
self._attr_native_value = str(self.obj.serial_number)


class VantageMasterIP(VantageEntity[Master], SensorEntity):
"""Vantage controller IP address sensor entity."""

Expand Down

0 comments on commit 3b8acdf

Please sign in to comment.