Skip to content

Make sure SmartThings light can deal with unknown states #140190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions homeassistant/components/smartthings/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,43 @@
"""Update entity attributes when the device status has changed."""
# Brightness and transition
if brightness_supported(self._attr_supported_color_modes):
self._attr_brightness = int(
convert_scale(
self.get_attribute_value(Capability.SWITCH_LEVEL, Attribute.LEVEL),
100,
255,
0,
if (
brightness := self.get_attribute_value(
Capability.SWITCH_LEVEL, Attribute.LEVEL
)
) is None:
self._attr_brightness = None
else:
self._attr_brightness = int(
convert_scale(
brightness,
100,
255,
0,
)
)
)
# Color Temperature
if ColorMode.COLOR_TEMP in self._attr_supported_color_modes:
self._attr_color_temp_kelvin = self.get_attribute_value(
Capability.COLOR_TEMPERATURE, Attribute.COLOR_TEMPERATURE
)
# Color
if ColorMode.HS in self._attr_supported_color_modes:
self._attr_hs_color = (
convert_scale(
self.get_attribute_value(Capability.COLOR_CONTROL, Attribute.HUE),
100,
360,
),
self.get_attribute_value(
Capability.COLOR_CONTROL, Attribute.SATURATION
),
)
if (
hue := self.get_attribute_value(Capability.COLOR_CONTROL, Attribute.HUE)
) is None:
self._attr_hs_color = None

Check warning on line 178 in homeassistant/components/smartthings/light.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/smartthings/light.py#L178

Added line #L178 was not covered by tests
else:
self._attr_hs_color = (
convert_scale(
hue,
100,
360,
),
self.get_attribute_value(
Capability.COLOR_CONTROL, Attribute.SATURATION
),
)

async def async_set_color(self, hs_color):
"""Set the color of the device."""
Expand Down Expand Up @@ -220,6 +232,10 @@
super()._update_handler(event)

@property
def is_on(self) -> bool:
def is_on(self) -> bool | None:
"""Return true if light is on."""
return self.get_attribute_value(Capability.SWITCH, Attribute.SWITCH) == "on"
if (
state := self.get_attribute_value(Capability.SWITCH, Attribute.SWITCH)
) is None:
return None
return state == "on"
1 change: 1 addition & 0 deletions tests/components/smartthings/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def mock_smartthings() -> Generator[AsyncMock]:
"generic_ef00_v1",
"bosch_radiator_thermostat_ii",
"im_speaker_ai_0001",
"abl_light_b_001",
"tplink_p110",
]
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"components": {
"main": {
"switchLevel": {
"levelRange": {
"value": null
},
"level": {
"value": null
}
},
"switch": {
"switch": {
"value": null
}
},
"colorTemperature": {
"colorTemperatureRange": {
"value": null
},
"colorTemperature": {
"value": null
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"items": [
{
"deviceId": "7c16163e-c94e-482f-95f6-139ae0cd9d5e",
"name": "ABL Wafer Down Light(BLE)",
"label": "Kitchen Light 5",
"manufacturerName": "Samsung Electronics",
"presentationId": "ABL-LIGHT-B-001",
"deviceManufacturerCode": "Samsung Electronics",
"locationId": "6c314222-8baf-48a0-9442-5b1102a8757f",
"ownerId": "f24ff388-700c-7d1e-91f2-1c37ae68ce2b",
"components": [
{
"id": "main",
"label": "main",
"capabilities": [
{
"id": "switch",
"version": 1
},
{
"id": "colorTemperature",
"version": 1
},
{
"id": "switchLevel",
"version": 1
}
],
"categories": [
{
"name": "Light",
"categoryType": "manufacturer"
}
]
}
],
"createTime": "2025-03-08T22:40:25.073Z",
"profile": {
"id": "65f5db53-9a78-4b19-8e40-d32187cd59ab"
},
"bleD2D": {
"encryptionKey": "f593369dcea915f6352a4a42cd4b2ea6",
"cipher": "AES_128-CBC-PKCS7Padding",
"advertisingId": "b13d7192",
"identifier": "88-57-1d-7c-cb-cf",
"configurationUrl": "https://apis.samsungiotcloud.com/v1/miniature/profile/65f5db53-9a78-4b19-8e40-d32187cd59ab",
"bleDeviceType": "BLE",
"metadata": null
},
"type": "BLE_D2D",
"restrictionTier": 0,
"allowed": null,
"executionContext": "CLOUD",
"relationships": []
}
],
"_links": {}
}
33 changes: 33 additions & 0 deletions tests/components/smartthings/snapshots/test_init.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@
# name: test_button_event[button]
<Event smartthings.button[L]: component_id=main, device_id=c4bdd19f-85d1-4d58-8f9c-e75ac3cf113b, location_id=abc, value=pushed, name=button, data=None>
# ---
# name: test_devices[abl_light_b_001]
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'config_entries_subentries': <ANY>,
'configuration_url': 'https://account.smartthings.com',
'connections': set({
}),
'disabled_by': None,
'entry_type': None,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'smartthings',
'7c16163e-c94e-482f-95f6-139ae0cd9d5e',
),
}),
'is_new': False,
'labels': set({
}),
'manufacturer': None,
'model': None,
'model_id': None,
'name': 'Kitchen Light 5',
'name_by_user': None,
'primary_config_entry': <ANY>,
'serial_number': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_devices[aeotec_home_energy_meter_gen5]
DeviceRegistryEntrySnapshot({
'area_id': 'toilet',
Expand Down
70 changes: 70 additions & 0 deletions tests/components/smartthings/snapshots/test_light.ambr
Original file line number Diff line number Diff line change
@@ -1,4 +1,74 @@
# serializer version: 1
# name: test_all_entities[abl_light_b_001][light.kitchen_light_5-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max_color_temp_kelvin': 9000,
'max_mireds': 500,
'min_color_temp_kelvin': 2000,
'min_mireds': 111,
'supported_color_modes': list([
<ColorMode.COLOR_TEMP: 'color_temp'>,
]),
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'light',
'entity_category': None,
'entity_id': 'light.kitchen_light_5',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': None,
'platform': 'smartthings',
'previous_unique_id': None,
'supported_features': <LightEntityFeature: 32>,
'translation_key': None,
'unique_id': '7c16163e-c94e-482f-95f6-139ae0cd9d5e',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[abl_light_b_001][light.kitchen_light_5-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'brightness': None,
'color_mode': None,
'color_temp': None,
'color_temp_kelvin': None,
'friendly_name': 'Kitchen Light 5',
'hs_color': None,
'max_color_temp_kelvin': 9000,
'max_mireds': 500,
'min_color_temp_kelvin': 2000,
'min_mireds': 111,
'rgb_color': None,
'supported_color_modes': list([
<ColorMode.COLOR_TEMP: 'color_temp'>,
]),
'supported_features': <LightEntityFeature: 32>,
'xy_color': None,
}),
'context': <ANY>,
'entity_id': 'light.kitchen_light_5',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_all_entities[centralite][light.dimmer_debian-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
Expand Down