From 1244bffcb48d7337a9d7a0da518959fe4b31a230 Mon Sep 17 00:00:00 2001 From: Marjo Wenzel van Lier Date: Tue, 15 Apr 2025 00:30:11 +0200 Subject: [PATCH] fix(entity): Ensure entities from multiple devices register correctly - Modify sensor and switch setup to use `extend` instead of list reassignment. This prevents overwriting entities from previously processed devices. - Update `OwletBaseEntity` initialisation to correctly store the coordinator instance. - Refine device information retrieval using `getattr` for enhanced robustness and provide more specific device details (e.g., serial number in name). This change addresses a bug where, in setups with multiple Owlet devices, only the entities belonging to the last device in the configuration were registered. Using `extend` ensures all entities across all devices are correctly added. Device information presentation is also improved. --- custom_components/owlet/binary_sensor.py | 4 ++-- custom_components/owlet/entity.py | 14 +++++++++----- custom_components/owlet/sensor.py | 4 ++-- custom_components/owlet/switch.py | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/custom_components/owlet/binary_sensor.py b/custom_components/owlet/binary_sensor.py index 0dd12ac..2d19978 100644 --- a/custom_components/owlet/binary_sensor.py +++ b/custom_components/owlet/binary_sensor.py @@ -106,11 +106,11 @@ async def async_setup_entry( sensors = [] for coordinator in coordinators: - sensors = [ + sensors.extend([ OwletBinarySensor(coordinator, sensor) for sensor in SENSORS if sensor.key in coordinator.sock.properties - ] + ]) if OwletAwakeSensor.entity_description.key in coordinator.sock.properties: sensors.append(OwletAwakeSensor(coordinator)) diff --git a/custom_components/owlet/entity.py b/custom_components/owlet/entity.py index b14f97b..3ee745c 100644 --- a/custom_components/owlet/entity.py +++ b/custom_components/owlet/entity.py @@ -19,6 +19,7 @@ class OwletBaseEntity(CoordinatorEntity[OwletCoordinator], Entity): ) -> None: """Initialize the base entity.""" super().__init__(coordinator) + self.coordinator = coordinator self.sock = coordinator.sock @property @@ -26,9 +27,12 @@ class OwletBaseEntity(CoordinatorEntity[OwletCoordinator], Entity): """Return the device info of the device.""" return DeviceInfo( identifiers={(DOMAIN, self.sock.serial)}, - name="Owlet Baby Care Sock", - manufacturer=MANUFACTURER, - model=self.sock.model, - sw_version=self.sock.sw_version, - hw_version=f"{self.sock.version}r{self.sock.revision}", + name=f"Owlet Sock {self.sock.serial}", + connections={("mac", getattr(self.sock, "mac", "unknown"))}, + suggested_area="Nursery", + configuration_url="https://my.owletcare.com/", + manufacturer="Owlet Baby Care", + model=getattr(self.sock, "model", None), + sw_version=getattr(self.sock, "sw_version", None), + hw_version=getattr(self.sock, "hw_version", "3r8"), ) diff --git a/custom_components/owlet/sensor.py b/custom_components/owlet/sensor.py index f054771..71294ef 100644 --- a/custom_components/owlet/sensor.py +++ b/custom_components/owlet/sensor.py @@ -115,11 +115,11 @@ async def async_setup_entry( sensors = [] for coordinator in coordinators: - sensors = [ + sensors.extend([ OwletSensor(coordinator, sensor) for sensor in SENSORS if sensor.key in coordinator.sock.properties - ] + ]) if OwletSleepSensor.entity_description.key in coordinator.sock.properties: sensors.append(OwletSleepSensor(coordinator)) diff --git a/custom_components/owlet/switch.py b/custom_components/owlet/switch.py index 39be5a9..fbda24f 100644 --- a/custom_components/owlet/switch.py +++ b/custom_components/owlet/switch.py @@ -52,7 +52,7 @@ async def async_setup_entry( switches = [] for coordinator in coordinators: - switches = [OwletBaseSwitch(coordinator, switch) for switch in SWITCHES] + switches.extend([OwletBaseSwitch(coordinator, switch) for switch in SWITCHES]) async_add_entities(switches)