From 616b031df8777eecb0fbe0c04ba23a962b15cd19 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 16 Aug 2025 11:00:08 +0200 Subject: [PATCH] Use constants in Tuya tests (#150739) --- tests/components/tuya/test_climate.py | 29 +++++++++++++----------- tests/components/tuya/test_cover.py | 25 +++++++++++--------- tests/components/tuya/test_humidifier.py | 25 ++++++++++---------- tests/components/tuya/test_light.py | 22 ++++++++++-------- tests/components/tuya/test_number.py | 18 +++++++++------ tests/components/tuya/test_select.py | 11 +++++---- tests/components/tuya/test_vacuum.py | 4 ++-- tests/components/tuya/test_valve.py | 6 ++--- 8 files changed, 77 insertions(+), 63 deletions(-) diff --git a/tests/components/tuya/test_climate.py b/tests/components/tuya/test_climate.py index 47c59267881..a0da9359ea3 100644 --- a/tests/components/tuya/test_climate.py +++ b/tests/components/tuya/test_climate.py @@ -9,13 +9,16 @@ from syrupy.assertion import SnapshotAssertion from tuya_sharing import CustomerDevice from homeassistant.components.climate import ( + ATTR_FAN_MODE, + ATTR_HUMIDITY, + ATTR_TEMPERATURE, DOMAIN as CLIMATE_DOMAIN, SERVICE_SET_FAN_MODE, SERVICE_SET_HUMIDITY, SERVICE_SET_TEMPERATURE, ) from homeassistant.components.tuya import ManagerCompat -from homeassistant.const import Platform +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceNotSupported from homeassistant.helpers import entity_registry as er @@ -60,11 +63,11 @@ async def test_set_temperature( CLIMATE_DOMAIN, SERVICE_SET_TEMPERATURE, { - "entity_id": entity_id, - "temperature": 22.7, + ATTR_ENTITY_ID: entity_id, + ATTR_TEMPERATURE: 22.7, }, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, [{"code": "temp_set", "value": 22}] ) @@ -86,16 +89,16 @@ async def test_fan_mode_windspeed( state = hass.states.get(entity_id) assert state is not None, f"{entity_id} does not exist" - assert state.attributes["fan_mode"] == 1 + assert state.attributes[ATTR_FAN_MODE] == 1 await hass.services.async_call( CLIMATE_DOMAIN, SERVICE_SET_FAN_MODE, { - "entity_id": entity_id, - "fan_mode": 2, + ATTR_ENTITY_ID: entity_id, + ATTR_FAN_MODE: 2, }, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, [{"code": "windspeed", "value": "2"}] ) @@ -122,14 +125,14 @@ async def test_fan_mode_no_valid_code( state = hass.states.get(entity_id) assert state is not None, f"{entity_id} does not exist" - assert state.attributes.get("fan_mode") is None + assert state.attributes.get(ATTR_FAN_MODE) is None with pytest.raises(ServiceNotSupported): await hass.services.async_call( CLIMATE_DOMAIN, SERVICE_SET_FAN_MODE, { - "entity_id": entity_id, - "fan_mode": 2, + ATTR_ENTITY_ID: entity_id, + ATTR_FAN_MODE: 2, }, blocking=True, ) @@ -156,8 +159,8 @@ async def test_set_humidity_not_supported( CLIMATE_DOMAIN, SERVICE_SET_HUMIDITY, { - "entity_id": entity_id, - "humidity": 50, + ATTR_ENTITY_ID: entity_id, + ATTR_HUMIDITY: 50, }, blocking=True, ) diff --git a/tests/components/tuya/test_cover.py b/tests/components/tuya/test_cover.py index 5b4610a6875..7206aaf1cff 100644 --- a/tests/components/tuya/test_cover.py +++ b/tests/components/tuya/test_cover.py @@ -9,6 +9,9 @@ from syrupy.assertion import SnapshotAssertion from tuya_sharing import CustomerDevice from homeassistant.components.cover import ( + ATTR_CURRENT_POSITION, + ATTR_POSITION, + ATTR_TILT_POSITION, DOMAIN as COVER_DOMAIN, SERVICE_CLOSE_COVER, SERVICE_OPEN_COVER, @@ -16,7 +19,7 @@ from homeassistant.components.cover import ( SERVICE_SET_COVER_TILT_POSITION, ) from homeassistant.components.tuya import ManagerCompat -from homeassistant.const import Platform +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceNotSupported from homeassistant.helpers import entity_registry as er @@ -62,10 +65,10 @@ async def test_open_service( COVER_DOMAIN, SERVICE_OPEN_COVER, { - "entity_id": entity_id, + ATTR_ENTITY_ID: entity_id, }, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, [ @@ -96,10 +99,10 @@ async def test_close_service( COVER_DOMAIN, SERVICE_CLOSE_COVER, { - "entity_id": entity_id, + ATTR_ENTITY_ID: entity_id, }, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, [ @@ -129,11 +132,11 @@ async def test_set_position( COVER_DOMAIN, SERVICE_SET_COVER_POSITION, { - "entity_id": entity_id, - "position": 25, + ATTR_ENTITY_ID: entity_id, + ATTR_POSITION: 25, }, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, [ @@ -173,7 +176,7 @@ async def test_percent_state_on_cover( state = hass.states.get(entity_id) assert state is not None, f"{entity_id} does not exist" - assert state.attributes["current_position"] == percent_state + assert state.attributes[ATTR_CURRENT_POSITION] == percent_state @pytest.mark.parametrize( @@ -197,8 +200,8 @@ async def test_set_tilt_position_not_supported( COVER_DOMAIN, SERVICE_SET_COVER_TILT_POSITION, { - "entity_id": entity_id, - "tilt_position": 50, + ATTR_ENTITY_ID: entity_id, + ATTR_TILT_POSITION: 50, }, blocking=True, ) diff --git a/tests/components/tuya/test_humidifier.py b/tests/components/tuya/test_humidifier.py index 33f715176bd..c38e5521990 100644 --- a/tests/components/tuya/test_humidifier.py +++ b/tests/components/tuya/test_humidifier.py @@ -9,13 +9,14 @@ from syrupy.assertion import SnapshotAssertion from tuya_sharing import CustomerDevice from homeassistant.components.humidifier import ( + ATTR_HUMIDITY, DOMAIN as HUMIDIFIER_DOMAIN, SERVICE_SET_HUMIDITY, SERVICE_TURN_OFF, SERVICE_TURN_ON, ) from homeassistant.components.tuya import ManagerCompat -from homeassistant.const import Platform +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers import entity_registry as er @@ -59,9 +60,9 @@ async def test_turn_on( await hass.services.async_call( HUMIDIFIER_DOMAIN, SERVICE_TURN_ON, - {"entity_id": entity_id}, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, [{"code": "switch", "value": True}] ) @@ -86,9 +87,9 @@ async def test_turn_off( await hass.services.async_call( HUMIDIFIER_DOMAIN, SERVICE_TURN_OFF, - {"entity_id": entity_id}, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, [{"code": "switch", "value": False}] ) @@ -114,11 +115,11 @@ async def test_set_humidity( HUMIDIFIER_DOMAIN, SERVICE_SET_HUMIDITY, { - "entity_id": entity_id, - "humidity": 50, + ATTR_ENTITY_ID: entity_id, + ATTR_HUMIDITY: 50, }, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, [{"code": "dehumidify_set_value", "value": 50}] ) @@ -149,7 +150,7 @@ async def test_turn_on_unsupported( await hass.services.async_call( HUMIDIFIER_DOMAIN, SERVICE_TURN_ON, - {"entity_id": entity_id}, + {ATTR_ENTITY_ID: entity_id}, blocking=True, ) assert err.value.translation_key == "action_dpcode_not_found" @@ -184,7 +185,7 @@ async def test_turn_off_unsupported( await hass.services.async_call( HUMIDIFIER_DOMAIN, SERVICE_TURN_OFF, - {"entity_id": entity_id}, + {ATTR_ENTITY_ID: entity_id}, blocking=True, ) assert err.value.translation_key == "action_dpcode_not_found" @@ -220,8 +221,8 @@ async def test_set_humidity_unsupported( HUMIDIFIER_DOMAIN, SERVICE_SET_HUMIDITY, { - "entity_id": entity_id, - "humidity": 50, + ATTR_ENTITY_ID: entity_id, + ATTR_HUMIDITY: 50, }, blocking=True, ) diff --git a/tests/components/tuya/test_light.py b/tests/components/tuya/test_light.py index 6c36f9ef838..e87eb139385 100644 --- a/tests/components/tuya/test_light.py +++ b/tests/components/tuya/test_light.py @@ -10,12 +10,14 @@ from syrupy.assertion import SnapshotAssertion from tuya_sharing import CustomerDevice from homeassistant.components.light import ( + ATTR_BRIGHTNESS, + ATTR_WHITE, DOMAIN as LIGHT_DOMAIN, SERVICE_TURN_OFF, SERVICE_TURN_ON, ) from homeassistant.components.tuya import ManagerCompat -from homeassistant.const import Platform +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -48,7 +50,7 @@ async def test_platform_setup_and_discovery( [ ( { - "white": True, + ATTR_WHITE: True, }, [ {"code": "switch_led", "value": True}, @@ -58,7 +60,7 @@ async def test_platform_setup_and_discovery( ), ( { - "brightness": 150, + ATTR_BRIGHTNESS: 150, }, [ {"code": "switch_led", "value": True}, @@ -67,8 +69,8 @@ async def test_platform_setup_and_discovery( ), ( { - "white": True, - "brightness": 150, + ATTR_WHITE: True, + ATTR_BRIGHTNESS: 150, }, [ {"code": "switch_led", "value": True}, @@ -78,7 +80,7 @@ async def test_platform_setup_and_discovery( ), ( { - "white": 150, + ATTR_WHITE: 150, }, [ {"code": "switch_led", "value": True}, @@ -106,11 +108,11 @@ async def test_turn_on_white( LIGHT_DOMAIN, SERVICE_TURN_ON, { - "entity_id": entity_id, + ATTR_ENTITY_ID: entity_id, **turn_on_input, }, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, expected_commands, @@ -137,10 +139,10 @@ async def test_turn_off( LIGHT_DOMAIN, SERVICE_TURN_OFF, { - "entity_id": entity_id, + ATTR_ENTITY_ID: entity_id, }, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, [{"code": "switch_led", "value": False}] ) diff --git a/tests/components/tuya/test_number.py b/tests/components/tuya/test_number.py index 58cfe3635ea..89124fdf65f 100644 --- a/tests/components/tuya/test_number.py +++ b/tests/components/tuya/test_number.py @@ -8,9 +8,13 @@ import pytest from syrupy.assertion import SnapshotAssertion from tuya_sharing import CustomerDevice -from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN, SERVICE_SET_VALUE +from homeassistant.components.number import ( + ATTR_VALUE, + DOMAIN as NUMBER_DOMAIN, + SERVICE_SET_VALUE, +) from homeassistant.components.tuya import ManagerCompat -from homeassistant.const import Platform +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers import entity_registry as er @@ -55,11 +59,11 @@ async def test_set_value( NUMBER_DOMAIN, SERVICE_SET_VALUE, { - "entity_id": entity_id, - "value": 18, + ATTR_ENTITY_ID: entity_id, + ATTR_VALUE: 18, }, + blocking=True, ) - await hass.async_block_till_done() mock_manager.send_commands.assert_called_once_with( mock_device.id, [{"code": "delay_set", "value": 18}] ) @@ -91,8 +95,8 @@ async def test_set_value_no_function( NUMBER_DOMAIN, SERVICE_SET_VALUE, { - "entity_id": entity_id, - "value": 18, + ATTR_ENTITY_ID: entity_id, + ATTR_VALUE: 18, }, blocking=True, ) diff --git a/tests/components/tuya/test_select.py b/tests/components/tuya/test_select.py index ed585e4568f..c35963528d4 100644 --- a/tests/components/tuya/test_select.py +++ b/tests/components/tuya/test_select.py @@ -9,11 +9,12 @@ from syrupy.assertion import SnapshotAssertion from tuya_sharing import CustomerDevice from homeassistant.components.select import ( + ATTR_OPTION, DOMAIN as SELECT_DOMAIN, SERVICE_SELECT_OPTION, ) from homeassistant.components.tuya import ManagerCompat -from homeassistant.const import Platform +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers import entity_registry as er @@ -58,8 +59,8 @@ async def test_select_option( SELECT_DOMAIN, SERVICE_SELECT_OPTION, { - "entity_id": entity_id, - "option": "forward", + ATTR_ENTITY_ID: entity_id, + ATTR_OPTION: "forward", }, blocking=True, ) @@ -89,8 +90,8 @@ async def test_select_invalid_option( SELECT_DOMAIN, SERVICE_SELECT_OPTION, { - "entity_id": entity_id, - "option": "hello", + ATTR_ENTITY_ID: entity_id, + ATTR_OPTION: "hello", }, blocking=True, ) diff --git a/tests/components/tuya/test_vacuum.py b/tests/components/tuya/test_vacuum.py index 5098927d1b4..5ee5b965137 100644 --- a/tests/components/tuya/test_vacuum.py +++ b/tests/components/tuya/test_vacuum.py @@ -13,7 +13,7 @@ from homeassistant.components.vacuum import ( DOMAIN as VACUUM_DOMAIN, SERVICE_RETURN_TO_BASE, ) -from homeassistant.const import Platform +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -58,7 +58,7 @@ async def test_return_home( VACUUM_DOMAIN, SERVICE_RETURN_TO_BASE, { - "entity_id": entity_id, + ATTR_ENTITY_ID: entity_id, }, blocking=True, ) diff --git a/tests/components/tuya/test_valve.py b/tests/components/tuya/test_valve.py index 9c00f0f4c75..73ccfba7fc4 100644 --- a/tests/components/tuya/test_valve.py +++ b/tests/components/tuya/test_valve.py @@ -14,7 +14,7 @@ from homeassistant.components.valve import ( SERVICE_CLOSE_VALVE, SERVICE_OPEN_VALVE, ) -from homeassistant.const import Platform +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -58,7 +58,7 @@ async def test_open_valve( VALVE_DOMAIN, SERVICE_OPEN_VALVE, { - "entity_id": entity_id, + ATTR_ENTITY_ID: entity_id, }, blocking=True, ) @@ -87,7 +87,7 @@ async def test_close_valve( VALVE_DOMAIN, SERVICE_CLOSE_VALVE, { - "entity_id": entity_id, + ATTR_ENTITY_ID: entity_id, }, blocking=True, )