diff --git a/homeassistant/components/withings/sensor.py b/homeassistant/components/withings/sensor.py index ad6ba6f2bd42..9d79d45513ed 100644 --- a/homeassistant/components/withings/sensor.py +++ b/homeassistant/components/withings/sensor.py @@ -8,6 +8,7 @@ from typing import Any, override from aiowithings import ( Activity, Device, + DeviceModel, Goals, MeasurementPosition, MeasurementType, @@ -695,6 +696,7 @@ class WithingsDeviceSensorEntityDescription(SensorEntityDescription): """Immutable class for describing withings data.""" value_fn: Callable[[Device], StateType] + exists_fn: Callable[[Device], bool] = lambda _: True DEVICE_SENSORS = [ @@ -704,6 +706,8 @@ DEVICE_SENSORS = [ options=["low", "medium", "high"], device_class=SensorDeviceClass.ENUM, value_fn=lambda device: device.battery, + # The Aura Sensor V2 is a wired Sleep Analyzer, so it has no battery + exists_fn=lambda device: device.model is not DeviceModel.AURA_SENSOR_V2, ) ] @@ -886,10 +890,27 @@ async def async_setup_entry( ) ): continue - async_add_entities( + withings_device = device_coordinator.data[device_id] + if device_entities := [ WithingsDeviceSensor(device_coordinator, description, device_id) for description in DEVICE_SENSORS - ) + if description.exists_fn(withings_device) + ]: + async_add_entities(device_entities) + else: + # Register devices without sensors, so they still show up + device_registry.async_get_or_create( + config_entry_id=entry.entry_id, + identifiers={(DOMAIN, device_id)}, + manufacturer="Withings", + name=withings_device.raw_model, + model=withings_device.raw_model, + via_device_id=dr.async_get_device_id_by_identifier( + hass, + (DOMAIN, str(entry.unique_id)), + config_entry_id=entry.entry_id, + ), + ) current_devices.add(device_id) if old_devices: diff --git a/tests/components/withings/test_sensor.py b/tests/components/withings/test_sensor.py index f95e40a58160..088ab8aa07ef 100644 --- a/tests/components/withings/test_sensor.py +++ b/tests/components/withings/test_sensor.py @@ -4,7 +4,13 @@ from dataclasses import replace from datetime import timedelta from unittest.mock import AsyncMock, patch -from aiowithings import Goals, MeasurementGroup, NotificationCategory +from aiowithings import ( + DeviceModel, + DeviceType, + Goals, + MeasurementGroup, + NotificationCategory, +) from freezegun.api import FrozenDateTimeFactory import pytest from pytest_homeassistant.common import ( @@ -591,6 +597,53 @@ async def test_device_sensors_created_when_device_data_received( ) +async def test_no_battery_sensor_for_aura_sensor_v2( + hass: HomeAssistant, + withings: AsyncMock, + polling_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test the wired Aura Sensor V2 gets a device, but no battery sensor.""" + device = replace( + load_device_fixture()[0], + device_type=DeviceType.SLEEP_MONITOR, + model=DeviceModel.AURA_SENSOR_V2, + raw_model="Aura Sensor V2", + ) + withings.get_devices.return_value = [device] + await setup_integration(hass, polling_config_entry, False) + + assert ( + entity_registry.async_get_entity_id( + "sensor", DOMAIN, f"{device.device_id}_battery" + ) + is None + ) + device_entry = device_registry.async_get_device_by_identifier( + (DOMAIN, device.device_id), polling_config_entry.entry_id + ) + assert device_entry + assert device_entry.name == "Aura Sensor V2" + assert device_entry.via_device_id == ( + device_registry.async_get_device_by_identifier( + (DOMAIN, str(polling_config_entry.unique_id)), + polling_config_entry.entry_id, + ).id + ) + + withings.get_devices.return_value = [] + + freezer.tick(timedelta(hours=1)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert not device_registry.async_get_device_by_identifier( + (DOMAIN, device.device_id), polling_config_entry.entry_id + ) + + async def test_device_two_config_entries( hass: HomeAssistant, withings: AsyncMock,