Use TargetKey in the lock, fan and alarm_control_panel tests (#195946)

This commit is contained in:
epenet
2026-12-02 12:26:08 +00:00
committed by GitHub
parent 897d8a3c2b
commit 66b1a26f86
8 changed files with 54 additions and 53 deletions
+1 -1
View File
@@ -55,7 +55,7 @@ async def turn_fan_on(
preset_mode: str | None = None,
) -> None:
"""Turn the fan on at the specified speed."""
service_data = {ATTR_ENTITY_ID: fan_id}
service_data = {TargetKey.ENTITY_ID: fan_id}
if preset_mode:
service_data[fan.FanServiceArgument.PRESET_MODE] = preset_mode
if percentage is not None:
+2 -2
View File
@@ -18,7 +18,7 @@ from homeassistant.components.homee.const import (
PRESET_MANUAL,
PRESET_SUMMER,
)
from homeassistant.const import ATTR_ENTITY_ID, Platform, TargetKey
from homeassistant.const import Platform, TargetKey
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import entity_registry as er
@@ -131,7 +131,7 @@ async def test_fan_services(
mock_homee.nodes = [build_mock_node("fan.json")]
await setup_integration(hass, mock_config_entry)
OPTIONS = {ATTR_ENTITY_ID: "fan.test_fan"}
OPTIONS = {TargetKey.ENTITY_ID: "fan.test_fan"}
OPTIONS.update(options)
await hass.services.async_call(
@@ -33,12 +33,12 @@ from homeassistant.components.homekit.type_air_purifiers import (
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
ATTR_SUPPORTED_FEATURES,
ATTR_UNIT_OF_MEASUREMENT,
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
TargetKey,
UnitOfTemperature,
)
from homeassistant.core import Event, HomeAssistant
@@ -135,7 +135,7 @@ async def test_fan_auto_manual(
assert acc.char_target_air_purifier_state.value == TARGET_STATE_AUTO
assert len(call_set_preset_mode) == 1
assert call_set_preset_mode[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_preset_mode[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_preset_mode[0].data[FanServiceArgument.PRESET_MODE] == auto_preset
assert len(events) == 1
assert events[-1].data["service"] == "set_preset_mode"
@@ -155,7 +155,7 @@ async def test_fan_auto_manual(
await hass.async_block_till_done()
assert acc.char_target_air_purifier_state.value == TARGET_STATE_MANUAL
assert len(call_set_percentage) == 1
assert call_set_percentage[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_percentage[0].data[TargetKey.ENTITY_ID] == entity_id
assert events[-1].data["service"] == "set_percentage"
assert len(events) == 2
@@ -268,7 +268,7 @@ async def test_air_purifier_single_preset_mode(
)
await hass.async_block_till_done()
assert call_set_percentage[0]
assert call_set_percentage[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_percentage[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_percentage[0].data[FanServiceArgument.PERCENTAGE] == 42
assert len(events) == 1
assert events[-1].data["service"] == "set_percentage"
@@ -287,7 +287,7 @@ async def test_air_purifier_single_preset_mode(
)
await hass.async_block_till_done()
assert call_set_preset_mode[0]
assert call_set_preset_mode[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_preset_mode[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_preset_mode[0].data[FanServiceArgument.PRESET_MODE] == "auto"
assert events[-1].data["service"] == "set_preset_mode"
assert len(events) == 2
+20 -20
View File
@@ -21,12 +21,12 @@ from homeassistant.components.homekit.const import (
)
from homeassistant.components.homekit.type_fans import Fan
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_SUPPORTED_FEATURES,
EVENT_HOMEASSISTANT_START,
STATE_OFF,
STATE_ON,
STATE_UNKNOWN,
TargetKey,
)
from homeassistant.core import CoreState, Event, HomeAssistant
from homeassistant.helpers import entity_registry as er
@@ -84,7 +84,7 @@ async def test_fan_basic(hass: HomeAssistant, hk_driver, events: list[Event]) ->
)
await hass.async_block_till_done()
assert call_turn_on
assert call_turn_on[0].data[ATTR_ENTITY_ID] == entity_id
assert call_turn_on[0].data[TargetKey.ENTITY_ID] == entity_id
assert len(events) == 1
assert events[-1].data[ATTR_VALUE] is None
@@ -105,7 +105,7 @@ async def test_fan_basic(hass: HomeAssistant, hk_driver, events: list[Event]) ->
)
await hass.async_block_till_done()
assert call_turn_off
assert call_turn_off[0].data[ATTR_ENTITY_ID] == entity_id
assert call_turn_off[0].data[TargetKey.ENTITY_ID] == entity_id
assert len(events) == 2
assert events[-1].data[ATTR_VALUE] is None
@@ -164,7 +164,7 @@ async def test_fan_direction(
)
await hass.async_block_till_done()
assert call_set_direction[0]
assert call_set_direction[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_direction[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_direction[0].data[FanServiceArgument.DIRECTION] == DIRECTION_FORWARD
assert len(events) == 1
assert events[-1].data[ATTR_VALUE] == DIRECTION_FORWARD
@@ -184,7 +184,7 @@ async def test_fan_direction(
acc.char_direction.client_update_value(1)
await hass.async_block_till_done()
assert call_set_direction[1]
assert call_set_direction[1].data[ATTR_ENTITY_ID] == entity_id
assert call_set_direction[1].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_direction[1].data[FanServiceArgument.DIRECTION] == DIRECTION_REVERSE
assert len(events) == 2
assert events[-1].data[ATTR_VALUE] == DIRECTION_REVERSE
@@ -245,7 +245,7 @@ async def test_fan_oscillate(
acc.char_swing.client_update_value(0)
await hass.async_block_till_done()
assert call_oscillate[0]
assert call_oscillate[0].data[ATTR_ENTITY_ID] == entity_id
assert call_oscillate[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_oscillate[0].data[FanServiceArgument.OSCILLATING] is False
assert len(events) == 1
assert events[-1].data[ATTR_VALUE] is False
@@ -265,7 +265,7 @@ async def test_fan_oscillate(
acc.char_swing.client_update_value(1)
await hass.async_block_till_done()
assert call_oscillate[1]
assert call_oscillate[1].data[ATTR_ENTITY_ID] == entity_id
assert call_oscillate[1].data[TargetKey.ENTITY_ID] == entity_id
assert call_oscillate[1].data[FanServiceArgument.OSCILLATING] is True
assert len(events) == 2
assert events[-1].data[ATTR_VALUE] is True
@@ -332,7 +332,7 @@ async def test_fan_speed(hass: HomeAssistant, hk_driver, events: list[Event]) ->
assert acc.char_active.value == 1
assert call_set_percentage[0]
assert call_set_percentage[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_percentage[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_percentage[0].data[FanServiceArgument.PERCENTAGE] == 42
assert len(events) == 1
assert events[-1].data[ATTR_VALUE] == 42
@@ -370,7 +370,7 @@ async def test_fan_speed(hass: HomeAssistant, hk_driver, events: list[Event]) ->
assert acc.char_active.value == 1
assert call_turn_on[0]
assert call_turn_on[0].data[ATTR_ENTITY_ID] == entity_id
assert call_turn_on[0].data[TargetKey.ENTITY_ID] == entity_id
async def test_fan_set_all_one_shot(
@@ -458,13 +458,13 @@ async def test_fan_set_all_one_shot(
await hass.async_block_till_done()
assert not call_turn_on
assert call_set_percentage[0]
assert call_set_percentage[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_percentage[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_percentage[0].data[FanServiceArgument.PERCENTAGE] == 42
assert call_oscillate[0]
assert call_oscillate[0].data[ATTR_ENTITY_ID] == entity_id
assert call_oscillate[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_oscillate[0].data[FanServiceArgument.OSCILLATING] is True
assert call_set_direction[0]
assert call_set_direction[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_direction[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_direction[0].data[FanServiceArgument.DIRECTION] == DIRECTION_REVERSE
assert len(events) == 3
@@ -518,13 +518,13 @@ async def test_fan_set_all_one_shot(
await hass.async_block_till_done()
assert len(events) == 6
assert call_set_percentage[1]
assert call_set_percentage[1].data[ATTR_ENTITY_ID] == entity_id
assert call_set_percentage[1].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_percentage[1].data[FanServiceArgument.PERCENTAGE] == 42
assert call_oscillate[1]
assert call_oscillate[1].data[ATTR_ENTITY_ID] == entity_id
assert call_oscillate[1].data[TargetKey.ENTITY_ID] == entity_id
assert call_oscillate[1].data[FanServiceArgument.OSCILLATING] is True
assert call_set_direction[1]
assert call_set_direction[1].data[ATTR_ENTITY_ID] == entity_id
assert call_set_direction[1].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_direction[1].data[FanServiceArgument.DIRECTION] == DIRECTION_REVERSE
assert events[-3].data[ATTR_VALUE] is True
@@ -562,7 +562,7 @@ async def test_fan_set_all_one_shot(
assert len(events) == 7
assert call_turn_off
assert call_turn_off[0].data[ATTR_ENTITY_ID] == entity_id
assert call_turn_off[0].data[TargetKey.ENTITY_ID] == entity_id
assert len(call_set_percentage) == 2
assert len(call_oscillate) == 2
assert len(call_set_direction) == 2
@@ -671,7 +671,7 @@ async def test_fan_multiple_preset_modes(
)
await hass.async_block_till_done()
assert call_set_preset_mode[0]
assert call_set_preset_mode[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_preset_mode[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_preset_mode[0].data[FanServiceArgument.PRESET_MODE] == "auto"
assert len(events) == 1
assert events[-1].data["service"] == "set_preset_mode"
@@ -690,7 +690,7 @@ async def test_fan_multiple_preset_modes(
)
await hass.async_block_till_done()
assert call_turn_on[0]
assert call_turn_on[0].data[ATTR_ENTITY_ID] == entity_id
assert call_turn_on[0].data[TargetKey.ENTITY_ID] == entity_id
assert events[-1].data["service"] == "turn_on"
assert len(events) == 2
@@ -741,7 +741,7 @@ async def test_fan_single_preset_mode(
)
await hass.async_block_till_done()
assert call_turn_on[0]
assert call_turn_on[0].data[ATTR_ENTITY_ID] == entity_id
assert call_turn_on[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_turn_on[0].data[FanServiceArgument.PERCENTAGE] == 42
assert len(events) == 1
assert events[-1].data["service"] == "turn_on"
@@ -760,7 +760,7 @@ async def test_fan_single_preset_mode(
)
await hass.async_block_till_done()
assert call_set_preset_mode[0]
assert call_set_preset_mode[0].data[ATTR_ENTITY_ID] == entity_id
assert call_set_preset_mode[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_set_preset_mode[0].data[FanServiceArgument.PRESET_MODE] == "smart"
assert events[-1].data["service"] == "set_preset_mode"
assert len(events) == 2
+4 -4
View File
@@ -25,11 +25,11 @@ from homeassistant.components.lock import (
from homeassistant.const import (
ATTR_CODE,
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
TargetKey,
)
from homeassistant.core import Event, HomeAssistant
from homeassistant.setup import async_setup_component
@@ -117,7 +117,7 @@ async def test_lock_unlock(hass: HomeAssistant, hk_driver, events: list[Event])
acc.char_target_state.client_update_value(1)
await hass.async_block_till_done()
assert call_lock
assert call_lock[0].data[ATTR_ENTITY_ID] == entity_id
assert call_lock[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_lock[0].data[LockServiceArgument.CODE] == code
assert acc.char_target_state.value == 1
assert len(events) == 1
@@ -126,7 +126,7 @@ async def test_lock_unlock(hass: HomeAssistant, hk_driver, events: list[Event])
acc.char_target_state.client_update_value(0)
await hass.async_block_till_done()
assert call_unlock
assert call_unlock[0].data[ATTR_ENTITY_ID] == entity_id
assert call_unlock[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_unlock[0].data[LockServiceArgument.CODE] == code
assert acc.char_target_state.value == 0
assert len(events) == 2
@@ -150,7 +150,7 @@ async def test_no_code(
acc.char_target_state.client_update_value(1)
await hass.async_block_till_done()
assert call_lock
assert call_lock[0].data[ATTR_ENTITY_ID] == entity_id
assert call_lock[0].data[TargetKey.ENTITY_ID] == entity_id
assert LockServiceArgument.CODE not in call_lock[0].data
assert acc.char_target_state.value == 1
assert len(events) == 1
@@ -16,12 +16,7 @@ from homeassistant.components.alarm_control_panel import (
from homeassistant.components.homekit.accessories import HomeBridge, HomeDriver
from homeassistant.components.homekit.const import ATTR_VALUE, PROP_VALID_VALUES
from homeassistant.components.homekit.type_security_systems import SecuritySystem
from homeassistant.const import (
ATTR_CODE,
ATTR_ENTITY_ID,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.const import ATTR_CODE, STATE_UNAVAILABLE, STATE_UNKNOWN, TargetKey
from homeassistant.core import Event, HomeAssistant, State
PANEL_ENTITY_ID = "alarm_control_panel.test"
@@ -95,7 +90,7 @@ async def test_switch_set_state(
acc.char_target_state.client_update_value(0)
await hass.async_block_till_done()
assert call_arm_home
assert call_arm_home[0].data[ATTR_ENTITY_ID] == entity_id
assert call_arm_home[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_arm_home[0].data[AlarmControlPanelServiceArgument.CODE] == code
assert acc.char_target_state.value == 0
assert len(events) == 1
@@ -104,7 +99,7 @@ async def test_switch_set_state(
acc.char_target_state.client_update_value(1)
await hass.async_block_till_done()
assert call_arm_away
assert call_arm_away[0].data[ATTR_ENTITY_ID] == entity_id
assert call_arm_away[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_arm_away[0].data[AlarmControlPanelServiceArgument.CODE] == code
assert acc.char_target_state.value == 1
assert len(events) == 2
@@ -113,7 +108,7 @@ async def test_switch_set_state(
acc.char_target_state.client_update_value(2)
await hass.async_block_till_done()
assert call_arm_night
assert call_arm_night[0].data[ATTR_ENTITY_ID] == entity_id
assert call_arm_night[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_arm_night[0].data[AlarmControlPanelServiceArgument.CODE] == code
assert acc.char_target_state.value == 2
assert len(events) == 3
@@ -122,7 +117,7 @@ async def test_switch_set_state(
acc.char_target_state.client_update_value(3)
await hass.async_block_till_done()
assert call_disarm
assert call_disarm[0].data[ATTR_ENTITY_ID] == entity_id
assert call_disarm[0].data[TargetKey.ENTITY_ID] == entity_id
assert call_disarm[0].data[AlarmControlPanelServiceArgument.CODE] == code
assert acc.char_target_state.value == 3
assert len(events) == 4
@@ -148,7 +143,7 @@ async def test_no_alarm_code(
acc.char_target_state.client_update_value(0)
await hass.async_block_till_done()
assert call_arm_home
assert call_arm_home[0].data[ATTR_ENTITY_ID] == entity_id
assert call_arm_home[0].data[TargetKey.ENTITY_ID] == entity_id
assert AlarmControlPanelServiceArgument.CODE not in call_arm_home[0].data
assert acc.char_target_state.value == 0
assert len(events) == 1
+4 -2
View File
@@ -21,7 +21,6 @@ from homeassistant.components.snooz.const import DOMAIN
from homeassistant.components.snooz.services import SnoozService, SnoozServiceArgument
from homeassistant.const import (
ATTR_ASSUMED_STATE,
ATTR_ENTITY_ID,
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
@@ -89,7 +88,10 @@ async def test_transition(
await hass.services.async_call(
DOMAIN,
service,
{ATTR_ENTITY_ID: [snooz_fan_entity_id], SnoozServiceArgument.DURATION: 1},
{
TargetKey.ENTITY_ID: [snooz_fan_entity_id],
SnoozServiceArgument.DURATION: 1,
},
blocking=True,
)
+12 -8
View File
@@ -10,7 +10,7 @@ from syrupy.assertion import SnapshotAssertion
from yalexs.manager.activity import INITIAL_LOCK_RESYNC_TIME
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN, LockService, LockState
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, TargetKey
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceNotSupported
from homeassistant.helpers import device_registry as dr, entity_registry as er
@@ -117,7 +117,7 @@ async def test_one_lock_operation(
assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name"
data = {
ATTR_ENTITY_ID: "lock.online_with_doorsense_name_online_with_doorsense_name"
TargetKey.ENTITY_ID: "lock.online_with_doorsense_name_online_with_doorsense_name"
}
await hass.services.async_call(LOCK_DOMAIN, LockService.UNLOCK, data, blocking=True)
@@ -156,7 +156,9 @@ async def test_open_lock_operation(hass: HomeAssistant) -> None:
== LockState.LOCKED
)
data = {ATTR_ENTITY_ID: "lock.online_with_unlatch_name_online_with_unlatch_name"}
data = {
TargetKey.ENTITY_ID: "lock.online_with_unlatch_name_online_with_unlatch_name"
}
await hass.services.async_call(LOCK_DOMAIN, LockService.OPEN, data, blocking=True)
assert (
@@ -182,7 +184,9 @@ async def test_open_lock_operation_socketio_connected(
== LockState.LOCKED
)
data = {ATTR_ENTITY_ID: "lock.online_with_unlatch_name_online_with_unlatch_name"}
data = {
TargetKey.ENTITY_ID: "lock.online_with_unlatch_name_online_with_unlatch_name"
}
await hass.services.async_call(LOCK_DOMAIN, LockService.OPEN, data, blocking=True)
listener = list(socketio._listeners)[0]
@@ -225,7 +229,7 @@ async def test_one_lock_operation_socketio_connected(
assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name"
data = {
ATTR_ENTITY_ID: "lock.online_with_doorsense_name_online_with_doorsense_name"
TargetKey.ENTITY_ID: "lock.online_with_doorsense_name_online_with_doorsense_name"
}
await hass.services.async_call(LOCK_DOMAIN, LockService.UNLOCK, data, blocking=True)
@@ -319,7 +323,7 @@ async def test_lock_jammed(hass: HomeAssistant) -> None:
assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name"
data = {
ATTR_ENTITY_ID: "lock.online_with_doorsense_name_online_with_doorsense_name"
TargetKey.ENTITY_ID: "lock.online_with_doorsense_name_online_with_doorsense_name"
}
await hass.services.async_call(LOCK_DOMAIN, LockService.UNLOCK, data, blocking=True)
@@ -354,7 +358,7 @@ async def test_lock_throws_exception_on_unknown_status_code(
assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name"
data = {
ATTR_ENTITY_ID: "lock.online_with_doorsense_name_online_with_doorsense_name"
TargetKey.ENTITY_ID: "lock.online_with_doorsense_name_online_with_doorsense_name"
}
with pytest.raises(ClientResponseError):
await hass.services.async_call(
@@ -514,7 +518,7 @@ async def test_open_throws_hass_service_not_supported_error(
mocked_lock_detail = await _mock_operative_yale_lock_detail(hass)
await _create_yale_with_devices(hass, [mocked_lock_detail])
entity_id = "lock.a6697750d607098bae8d6baa11ef8063_name_a6697750d607098bae8d6baa11ef8063_name"
data = {ATTR_ENTITY_ID: entity_id}
data = {TargetKey.ENTITY_ID: entity_id}
with pytest.raises(
ServiceNotSupported,
match=(