Skip to content

Commit daa4dda

Browse files
Switchbot Cloud: Fix device type filtering in sensor (home-assistant#146945)
* Add Smart Lock Ultra support and fix device type filtering in sensor integration * Adding fix in binary sensor * Fix --------- Co-authored-by: Joostlek <[email protected]>
1 parent 8cead00 commit daa4dda

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

homeassistant/components/switchbot_cloud/binary_sensor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ async def async_setup_entry(
6969
for description in BINARY_SENSOR_DESCRIPTIONS_BY_DEVICE_TYPES[
7070
device.device_type
7171
]
72+
if device.device_type in BINARY_SENSOR_DESCRIPTIONS_BY_DEVICE_TYPES
7273
)
7374

7475

homeassistant/components/switchbot_cloud/sensor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ async def async_setup_entry(
151151
SwitchBotCloudSensor(data.api, device, coordinator, description)
152152
for device, coordinator in data.devices.sensors
153153
for description in SENSOR_DESCRIPTIONS_BY_DEVICE_TYPES[device.device_type]
154+
if device.device_type in SENSOR_DESCRIPTIONS_BY_DEVICE_TYPES
154155
)
155156

156157

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Test for the switchbot_cloud binary sensors."""
2+
3+
from unittest.mock import patch
4+
5+
from switchbot_api import Device
6+
7+
from homeassistant.const import Platform
8+
from homeassistant.core import HomeAssistant
9+
from homeassistant.helpers import entity_registry as er
10+
11+
from . import configure_integration
12+
13+
14+
async def test_unsupported_device_type(
15+
hass: HomeAssistant,
16+
entity_registry: er.EntityRegistry,
17+
mock_list_devices,
18+
mock_get_status,
19+
) -> None:
20+
"""Test that unsupported device types do not create sensors."""
21+
mock_list_devices.return_value = [
22+
Device(
23+
version="V1.0",
24+
deviceId="unsupported-id-1",
25+
deviceName="unsupported-device",
26+
deviceType="UnsupportedDevice",
27+
hubDeviceId="test-hub-id",
28+
),
29+
]
30+
mock_get_status.return_value = {}
31+
32+
with patch(
33+
"homeassistant.components.switchbot_cloud.PLATFORMS", [Platform.BINARY_SENSOR]
34+
):
35+
entry = await configure_integration(hass)
36+
37+
# Assert no binary sensor entities were created for unsupported device type
38+
entities = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
39+
assert len([e for e in entities if e.domain == "binary_sensor"]) == 0

tests/components/switchbot_cloud/test_sensor.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,29 @@ async def test_meter_no_coordinator_data(
6767
entry = await configure_integration(hass)
6868

6969
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
70+
71+
72+
async def test_unsupported_device_type(
73+
hass: HomeAssistant,
74+
entity_registry: er.EntityRegistry,
75+
mock_list_devices,
76+
mock_get_status,
77+
) -> None:
78+
"""Test that unsupported device types do not create sensors."""
79+
mock_list_devices.return_value = [
80+
Device(
81+
version="V1.0",
82+
deviceId="unsupported-id-1",
83+
deviceName="unsupported-device",
84+
deviceType="UnsupportedDevice",
85+
hubDeviceId="test-hub-id",
86+
),
87+
]
88+
mock_get_status.return_value = {}
89+
90+
with patch("homeassistant.components.switchbot_cloud.PLATFORMS", [Platform.SENSOR]):
91+
entry = await configure_integration(hass)
92+
93+
# Assert no sensor entities were created for unsupported device type
94+
entities = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
95+
assert len([e for e in entities if e.domain == "sensor"]) == 0

0 commit comments

Comments
 (0)