Fix tuya light supported color modes (#149793)

Co-authored-by: Erik <erik@montnemery.com>
This commit is contained in:
epenet 2025-08-01 09:13:53 +02:00 committed by GitHub
parent 4d59e8cd80
commit 8b53b26333
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 377 additions and 13 deletions

View File

@ -16,6 +16,7 @@ from homeassistant.components.light import (
ColorMode, ColorMode,
LightEntity, LightEntity,
LightEntityDescription, LightEntityDescription,
color_supported,
filter_supported_color_modes, filter_supported_color_modes,
) )
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
@ -530,19 +531,6 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
description.brightness_min, dptype=DPType.INTEGER description.brightness_min, dptype=DPType.INTEGER
) )
if int_type := self.find_dpcode(
description.color_temp, dptype=DPType.INTEGER, prefer_function=True
):
self._color_temp = int_type
color_modes.add(ColorMode.COLOR_TEMP)
# If entity does not have color_temp, check if it has work_mode "white"
elif color_mode_enum := self.find_dpcode(
description.color_mode, dptype=DPType.ENUM, prefer_function=True
):
if WorkMode.WHITE.value in color_mode_enum.range:
color_modes.add(ColorMode.WHITE)
self._white_color_mode = ColorMode.WHITE
if ( if (
dpcode := self.find_dpcode(description.color_data, prefer_function=True) dpcode := self.find_dpcode(description.color_data, prefer_function=True)
) and self.get_dptype(dpcode) == DPType.JSON: ) and self.get_dptype(dpcode) == DPType.JSON:
@ -568,6 +556,26 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
): ):
self._color_data_type = DEFAULT_COLOR_TYPE_DATA_V2 self._color_data_type = DEFAULT_COLOR_TYPE_DATA_V2
# Check if the light has color temperature
if int_type := self.find_dpcode(
description.color_temp, dptype=DPType.INTEGER, prefer_function=True
):
self._color_temp = int_type
color_modes.add(ColorMode.COLOR_TEMP)
# If light has color but does not have color_temp, check if it has
# work_mode "white"
elif (
color_supported(color_modes)
and (
color_mode_enum := self.find_dpcode(
description.color_mode, dptype=DPType.ENUM, prefer_function=True
)
)
and WorkMode.WHITE.value in color_mode_enum.range
):
color_modes.add(ColorMode.WHITE)
self._white_color_mode = ColorMode.WHITE
self._attr_supported_color_modes = filter_supported_color_modes(color_modes) self._attr_supported_color_modes = filter_supported_color_modes(color_modes)
if len(self._attr_supported_color_modes) == 1: if len(self._attr_supported_color_modes) == 1:
# If the light supports only a single color mode, set it now # If the light supports only a single color mode, set it now

View File

@ -149,6 +149,12 @@ DEVICE_MOCKS = {
Platform.SELECT, Platform.SELECT,
Platform.SWITCH, Platform.SWITCH,
], ],
"tyndj_pyakuuoc": [
# https://github.com/home-assistant/core/issues/149704
Platform.LIGHT,
Platform.SENSOR,
Platform.SWITCH,
],
"wk_air_conditioner": [ "wk_air_conditioner": [
# https://github.com/home-assistant/core/issues/146263 # https://github.com/home-assistant/core/issues/146263
Platform.CLIMATE, Platform.CLIMATE,

View File

@ -0,0 +1,145 @@
{
"endpoint": "https://apigw.tuyaeu.com",
"terminal_id": "1753247726209KOaaPc",
"mqtt_connected": true,
"disabled_by": null,
"disabled_polling": false,
"id": "bfdb773e4ae317e3915h2i",
"name": "Solar zijpad",
"category": "tyndj",
"product_id": "pyakuuoc",
"product_name": "Solar flood light App panel",
"online": false,
"sub": true,
"time_zone": "+08:00",
"active_time": "2023-03-08T13:24:06+00:00",
"create_time": "2023-03-08T13:24:06+00:00",
"update_time": "2023-03-08T13:24:06+00:00",
"function": {
"switch_led": {
"type": "Boolean",
"value": {}
},
"work_mode": {
"type": "Enum",
"value": {
"range": ["white", "colour", "scene", "music"]
}
},
"bright_value": {
"type": "Integer",
"value": {
"min": 10,
"max": 1000,
"scale": 0,
"step": 1
}
},
"scene_data": {
"type": "String",
"value": {
"maxlen": 255
}
},
"countdown": {
"type": "Integer",
"value": {
"min": 0,
"max": 86400,
"scale": 0,
"step": 1
}
},
"music_data": {
"type": "String",
"value": {
"maxlen": 255
}
},
"switch_save_energy": {
"type": "Boolean",
"value": {}
},
"device_mode": {
"type": "Enum",
"value": {
"range": ["manual", "auto"]
}
}
},
"status_range": {
"switch_led": {
"type": "Boolean",
"value": {}
},
"work_mode": {
"type": "Enum",
"value": {
"range": ["white", "colour", "scene", "music"]
}
},
"bright_value": {
"type": "Integer",
"value": {
"min": 10,
"max": 1000,
"scale": 0,
"step": 1
}
},
"scene_data": {
"type": "String",
"value": {
"maxlen": 255
}
},
"countdown": {
"type": "Integer",
"value": {
"min": 0,
"max": 86400,
"scale": 0,
"step": 1
}
},
"switch_save_energy": {
"type": "Boolean",
"value": {}
},
"battery_percentage": {
"type": "Integer",
"value": {
"unit": "%",
"min": 0,
"max": 100,
"scale": 0,
"step": 1
}
},
"device_mode": {
"type": "Enum",
"value": {
"range": ["manual", "auto"]
}
},
"battery_state": {
"type": "Enum",
"value": {
"range": ["low", "middle", "high"]
}
}
},
"status": {
"switch_led": false,
"work_mode": "white",
"bright_value": 10,
"scene_data": "",
"countdown": 0,
"switch_save_energy": false,
"battery_percentage": 0,
"device_mode": "manual",
"battery_state": "low"
},
"set_up": true,
"support_local": true
}

View File

@ -249,3 +249,59 @@
'state': 'on', 'state': 'on',
}) })
# --- # ---
# name: test_platform_setup_and_discovery[tyndj_pyakuuoc][light.solar_zijpad-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
}),
'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.solar_zijpad',
'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': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'tuya.bfdb773e4ae317e3915h2iswitch_led',
'unit_of_measurement': None,
})
# ---
# name: test_platform_setup_and_discovery[tyndj_pyakuuoc][light.solar_zijpad-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Solar zijpad',
'supported_color_modes': list([
<ColorMode.BRIGHTNESS: 'brightness'>,
]),
'supported_features': <LightEntityFeature: 0>,
}),
'context': <ANY>,
'entity_id': 'light.solar_zijpad',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
})
# ---

View File

@ -2233,6 +2233,107 @@
'state': '0.0', 'state': '0.0',
}) })
# --- # ---
# name: test_platform_setup_and_discovery[tyndj_pyakuuoc][sensor.solar_zijpad_battery-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.solar_zijpad_battery',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.BATTERY: 'battery'>,
'original_icon': None,
'original_name': 'Battery',
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'battery',
'unique_id': 'tuya.bfdb773e4ae317e3915h2ibattery_percentage',
'unit_of_measurement': '%',
})
# ---
# name: test_platform_setup_and_discovery[tyndj_pyakuuoc][sensor.solar_zijpad_battery-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'battery',
'friendly_name': 'Solar zijpad Battery',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'sensor.solar_zijpad_battery',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
})
# ---
# name: test_platform_setup_and_discovery[tyndj_pyakuuoc][sensor.solar_zijpad_battery_state-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.solar_zijpad_battery_state',
'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': 'Battery state',
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'battery_state',
'unique_id': 'tuya.bfdb773e4ae317e3915h2ibattery_state',
'unit_of_measurement': None,
})
# ---
# name: test_platform_setup_and_discovery[tyndj_pyakuuoc][sensor.solar_zijpad_battery_state-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Solar zijpad Battery state',
}),
'context': <ANY>,
'entity_id': 'sensor.solar_zijpad_battery_state',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
})
# ---
# name: test_platform_setup_and_discovery[wk_wifi_smart_gas_boiler_thermostat][sensor.wifi_smart_gas_boiler_thermostat_battery-entry] # name: test_platform_setup_and_discovery[wk_wifi_smart_gas_boiler_thermostat][sensor.wifi_smart_gas_boiler_thermostat_battery-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({

View File

@ -1161,6 +1161,54 @@
'state': 'unavailable', 'state': 'unavailable',
}) })
# --- # ---
# name: test_platform_setup_and_discovery[tyndj_pyakuuoc][switch.solar_zijpad_energy_saving-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.solar_zijpad_energy_saving',
'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': 'Energy saving',
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'energy_saving',
'unique_id': 'tuya.bfdb773e4ae317e3915h2iswitch_save_energy',
'unit_of_measurement': None,
})
# ---
# name: test_platform_setup_and_discovery[tyndj_pyakuuoc][switch.solar_zijpad_energy_saving-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Solar zijpad Energy saving',
}),
'context': <ANY>,
'entity_id': 'switch.solar_zijpad_energy_saving',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
})
# ---
# name: test_platform_setup_and_discovery[wk_air_conditioner][switch.clima_cucina_child_lock-entry] # name: test_platform_setup_and_discovery[wk_air_conditioner][switch.clima_cucina_child_lock-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({