From daa4ddabfe9ca16f46cd6ab45021f5c819ff75c9 Mon Sep 17 00:00:00 2001 From: Ravaka Razafimanantsoa <3774520+SeraphicRav@users.noreply.github.com> Date: Sun, 22 Jun 2025 21:12:09 +0900 Subject: [PATCH] Switchbot Cloud: Fix device type filtering in sensor (#146945) * Add Smart Lock Ultra support and fix device type filtering in sensor integration * Adding fix in binary sensor * Fix --------- Co-authored-by: Joostlek --- .../switchbot_cloud/binary_sensor.py | 1 + .../components/switchbot_cloud/sensor.py | 1 + .../switchbot_cloud/test_binary_sensor.py | 39 +++++++++++++++++++ .../components/switchbot_cloud/test_sensor.py | 26 +++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 tests/components/switchbot_cloud/test_binary_sensor.py diff --git a/homeassistant/components/switchbot_cloud/binary_sensor.py b/homeassistant/components/switchbot_cloud/binary_sensor.py index 14278072c83..752c428fa6c 100644 --- a/homeassistant/components/switchbot_cloud/binary_sensor.py +++ b/homeassistant/components/switchbot_cloud/binary_sensor.py @@ -69,6 +69,7 @@ async def async_setup_entry( for description in BINARY_SENSOR_DESCRIPTIONS_BY_DEVICE_TYPES[ device.device_type ] + if device.device_type in BINARY_SENSOR_DESCRIPTIONS_BY_DEVICE_TYPES ) diff --git a/homeassistant/components/switchbot_cloud/sensor.py b/homeassistant/components/switchbot_cloud/sensor.py index 9975bd49186..9920717a8d7 100644 --- a/homeassistant/components/switchbot_cloud/sensor.py +++ b/homeassistant/components/switchbot_cloud/sensor.py @@ -151,6 +151,7 @@ async def async_setup_entry( SwitchBotCloudSensor(data.api, device, coordinator, description) for device, coordinator in data.devices.sensors for description in SENSOR_DESCRIPTIONS_BY_DEVICE_TYPES[device.device_type] + if device.device_type in SENSOR_DESCRIPTIONS_BY_DEVICE_TYPES ) diff --git a/tests/components/switchbot_cloud/test_binary_sensor.py b/tests/components/switchbot_cloud/test_binary_sensor.py new file mode 100644 index 00000000000..753653af9a8 --- /dev/null +++ b/tests/components/switchbot_cloud/test_binary_sensor.py @@ -0,0 +1,39 @@ +"""Test for the switchbot_cloud binary sensors.""" + +from unittest.mock import patch + +from switchbot_api import Device + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import configure_integration + + +async def test_unsupported_device_type( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_list_devices, + mock_get_status, +) -> None: + """Test that unsupported device types do not create sensors.""" + mock_list_devices.return_value = [ + Device( + version="V1.0", + deviceId="unsupported-id-1", + deviceName="unsupported-device", + deviceType="UnsupportedDevice", + hubDeviceId="test-hub-id", + ), + ] + mock_get_status.return_value = {} + + with patch( + "homeassistant.components.switchbot_cloud.PLATFORMS", [Platform.BINARY_SENSOR] + ): + entry = await configure_integration(hass) + + # Assert no binary sensor entities were created for unsupported device type + entities = er.async_entries_for_config_entry(entity_registry, entry.entry_id) + assert len([e for e in entities if e.domain == "binary_sensor"]) == 0 diff --git a/tests/components/switchbot_cloud/test_sensor.py b/tests/components/switchbot_cloud/test_sensor.py index 440e71f3124..99b6acc7401 100644 --- a/tests/components/switchbot_cloud/test_sensor.py +++ b/tests/components/switchbot_cloud/test_sensor.py @@ -67,3 +67,29 @@ async def test_meter_no_coordinator_data( entry = await configure_integration(hass) await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) + + +async def test_unsupported_device_type( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_list_devices, + mock_get_status, +) -> None: + """Test that unsupported device types do not create sensors.""" + mock_list_devices.return_value = [ + Device( + version="V1.0", + deviceId="unsupported-id-1", + deviceName="unsupported-device", + deviceType="UnsupportedDevice", + hubDeviceId="test-hub-id", + ), + ] + mock_get_status.return_value = {} + + with patch("homeassistant.components.switchbot_cloud.PLATFORMS", [Platform.SENSOR]): + entry = await configure_integration(hass) + + # Assert no sensor entities were created for unsupported device type + entities = er.async_entries_for_config_entry(entity_registry, entry.entry_id) + assert len([e for e in entities if e.domain == "sensor"]) == 0