Skip to content

Commit b5c7bdd

Browse files
joostlekfrenck
authored andcommitted
Make sure SmartThings light can deal with unknown states (#140190)
* Fix * add comment * Make light unknown * Make light unknown
1 parent 38e6133 commit b5c7bdd

File tree

6 files changed

+225
-19
lines changed

6 files changed

+225
-19
lines changed

homeassistant/components/smartthings/light.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,31 +147,43 @@ def _update_attr(self) -> None:
147147
"""Update entity attributes when the device status has changed."""
148148
# Brightness and transition
149149
if brightness_supported(self._attr_supported_color_modes):
150-
self._attr_brightness = int(
151-
convert_scale(
152-
self.get_attribute_value(Capability.SWITCH_LEVEL, Attribute.LEVEL),
153-
100,
154-
255,
155-
0,
150+
if (
151+
brightness := self.get_attribute_value(
152+
Capability.SWITCH_LEVEL, Attribute.LEVEL
153+
)
154+
) is None:
155+
self._attr_brightness = None
156+
else:
157+
self._attr_brightness = int(
158+
convert_scale(
159+
brightness,
160+
100,
161+
255,
162+
0,
163+
)
156164
)
157-
)
158165
# Color Temperature
159166
if ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
160167
self._attr_color_temp_kelvin = self.get_attribute_value(
161168
Capability.COLOR_TEMPERATURE, Attribute.COLOR_TEMPERATURE
162169
)
163170
# Color
164171
if ColorMode.HS in self._attr_supported_color_modes:
165-
self._attr_hs_color = (
166-
convert_scale(
167-
self.get_attribute_value(Capability.COLOR_CONTROL, Attribute.HUE),
168-
100,
169-
360,
170-
),
171-
self.get_attribute_value(
172-
Capability.COLOR_CONTROL, Attribute.SATURATION
173-
),
174-
)
172+
if (
173+
hue := self.get_attribute_value(Capability.COLOR_CONTROL, Attribute.HUE)
174+
) is None:
175+
self._attr_hs_color = None
176+
else:
177+
self._attr_hs_color = (
178+
convert_scale(
179+
hue,
180+
100,
181+
360,
182+
),
183+
self.get_attribute_value(
184+
Capability.COLOR_CONTROL, Attribute.SATURATION
185+
),
186+
)
175187

176188
async def async_set_color(self, hs_color):
177189
"""Set the color of the device."""
@@ -217,6 +229,10 @@ def _update_handler(self, event: DeviceEvent) -> None:
217229
super()._update_handler(event)
218230

219231
@property
220-
def is_on(self) -> bool:
232+
def is_on(self) -> bool | None:
221233
"""Return true if light is on."""
222-
return self.get_attribute_value(Capability.SWITCH, Attribute.SWITCH) == "on"
234+
if (
235+
state := self.get_attribute_value(Capability.SWITCH, Attribute.SWITCH)
236+
) is None:
237+
return None
238+
return state == "on"

tests/components/smartthings/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def mock_smartthings() -> Generator[AsyncMock]:
123123
"generic_ef00_v1",
124124
"bosch_radiator_thermostat_ii",
125125
"im_speaker_ai_0001",
126+
"abl_light_b_001",
126127
"tplink_p110",
127128
]
128129
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"components": {
3+
"main": {
4+
"switchLevel": {
5+
"levelRange": {
6+
"value": null
7+
},
8+
"level": {
9+
"value": null
10+
}
11+
},
12+
"switch": {
13+
"switch": {
14+
"value": null
15+
}
16+
},
17+
"colorTemperature": {
18+
"colorTemperatureRange": {
19+
"value": null
20+
},
21+
"colorTemperature": {
22+
"value": null
23+
}
24+
}
25+
}
26+
}
27+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"items": [
3+
{
4+
"deviceId": "7c16163e-c94e-482f-95f6-139ae0cd9d5e",
5+
"name": "ABL Wafer Down Light(BLE)",
6+
"label": "Kitchen Light 5",
7+
"manufacturerName": "Samsung Electronics",
8+
"presentationId": "ABL-LIGHT-B-001",
9+
"deviceManufacturerCode": "Samsung Electronics",
10+
"locationId": "6c314222-8baf-48a0-9442-5b1102a8757f",
11+
"ownerId": "f24ff388-700c-7d1e-91f2-1c37ae68ce2b",
12+
"components": [
13+
{
14+
"id": "main",
15+
"label": "main",
16+
"capabilities": [
17+
{
18+
"id": "switch",
19+
"version": 1
20+
},
21+
{
22+
"id": "colorTemperature",
23+
"version": 1
24+
},
25+
{
26+
"id": "switchLevel",
27+
"version": 1
28+
}
29+
],
30+
"categories": [
31+
{
32+
"name": "Light",
33+
"categoryType": "manufacturer"
34+
}
35+
]
36+
}
37+
],
38+
"createTime": "2025-03-08T22:40:25.073Z",
39+
"profile": {
40+
"id": "65f5db53-9a78-4b19-8e40-d32187cd59ab"
41+
},
42+
"bleD2D": {
43+
"encryptionKey": "f593369dcea915f6352a4a42cd4b2ea6",
44+
"cipher": "AES_128-CBC-PKCS7Padding",
45+
"advertisingId": "b13d7192",
46+
"identifier": "88-57-1d-7c-cb-cf",
47+
"configurationUrl": "https://apis.samsungiotcloud.com/v1/miniature/profile/65f5db53-9a78-4b19-8e40-d32187cd59ab",
48+
"bleDeviceType": "BLE",
49+
"metadata": null
50+
},
51+
"type": "BLE_D2D",
52+
"restrictionTier": 0,
53+
"allowed": null,
54+
"executionContext": "CLOUD",
55+
"relationships": []
56+
}
57+
],
58+
"_links": {}
59+
}

tests/components/smartthings/snapshots/test_init.ambr

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,39 @@
22
# name: test_button_event[button]
33
<Event smartthings.button[L]: component_id=main, device_id=c4bdd19f-85d1-4d58-8f9c-e75ac3cf113b, location_id=abc, value=pushed, name=button, data=None>
44
# ---
5+
# name: test_devices[abl_light_b_001]
6+
DeviceRegistryEntrySnapshot({
7+
'area_id': None,
8+
'config_entries': <ANY>,
9+
'config_entries_subentries': <ANY>,
10+
'configuration_url': 'https://account.smartthings.com',
11+
'connections': set({
12+
}),
13+
'disabled_by': None,
14+
'entry_type': None,
15+
'hw_version': None,
16+
'id': <ANY>,
17+
'identifiers': set({
18+
tuple(
19+
'smartthings',
20+
'7c16163e-c94e-482f-95f6-139ae0cd9d5e',
21+
),
22+
}),
23+
'is_new': False,
24+
'labels': set({
25+
}),
26+
'manufacturer': None,
27+
'model': None,
28+
'model_id': None,
29+
'name': 'Kitchen Light 5',
30+
'name_by_user': None,
31+
'primary_config_entry': <ANY>,
32+
'serial_number': None,
33+
'suggested_area': None,
34+
'sw_version': None,
35+
'via_device_id': None,
36+
})
37+
# ---
538
# name: test_devices[aeotec_home_energy_meter_gen5]
639
DeviceRegistryEntrySnapshot({
740
'area_id': None,

tests/components/smartthings/snapshots/test_light.ambr

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,74 @@
11
# serializer version: 1
2+
# name: test_all_entities[abl_light_b_001][light.kitchen_light_5-entry]
3+
EntityRegistryEntrySnapshot({
4+
'aliases': set({
5+
}),
6+
'area_id': None,
7+
'capabilities': dict({
8+
'max_color_temp_kelvin': 9000,
9+
'max_mireds': 500,
10+
'min_color_temp_kelvin': 2000,
11+
'min_mireds': 111,
12+
'supported_color_modes': list([
13+
<ColorMode.COLOR_TEMP: 'color_temp'>,
14+
]),
15+
}),
16+
'config_entry_id': <ANY>,
17+
'config_subentry_id': <ANY>,
18+
'device_class': None,
19+
'device_id': <ANY>,
20+
'disabled_by': None,
21+
'domain': 'light',
22+
'entity_category': None,
23+
'entity_id': 'light.kitchen_light_5',
24+
'has_entity_name': True,
25+
'hidden_by': None,
26+
'icon': None,
27+
'id': <ANY>,
28+
'labels': set({
29+
}),
30+
'name': None,
31+
'options': dict({
32+
}),
33+
'original_device_class': None,
34+
'original_icon': None,
35+
'original_name': None,
36+
'platform': 'smartthings',
37+
'previous_unique_id': None,
38+
'supported_features': <LightEntityFeature: 32>,
39+
'translation_key': None,
40+
'unique_id': '7c16163e-c94e-482f-95f6-139ae0cd9d5e',
41+
'unit_of_measurement': None,
42+
})
43+
# ---
44+
# name: test_all_entities[abl_light_b_001][light.kitchen_light_5-state]
45+
StateSnapshot({
46+
'attributes': ReadOnlyDict({
47+
'brightness': None,
48+
'color_mode': None,
49+
'color_temp': None,
50+
'color_temp_kelvin': None,
51+
'friendly_name': 'Kitchen Light 5',
52+
'hs_color': None,
53+
'max_color_temp_kelvin': 9000,
54+
'max_mireds': 500,
55+
'min_color_temp_kelvin': 2000,
56+
'min_mireds': 111,
57+
'rgb_color': None,
58+
'supported_color_modes': list([
59+
<ColorMode.COLOR_TEMP: 'color_temp'>,
60+
]),
61+
'supported_features': <LightEntityFeature: 32>,
62+
'xy_color': None,
63+
}),
64+
'context': <ANY>,
65+
'entity_id': 'light.kitchen_light_5',
66+
'last_changed': <ANY>,
67+
'last_reported': <ANY>,
68+
'last_updated': <ANY>,
69+
'state': 'unknown',
70+
})
71+
# ---
272
# name: test_all_entities[centralite][light.dimmer_debian-entry]
373
EntityRegistryEntrySnapshot({
474
'aliases': set({

0 commit comments

Comments
 (0)