Skip to content

Commit 25f64a2

Browse files
authored
Do not specify the code_format when a code is not required (home-assistant#148698)
1 parent dcbdce4 commit 25f64a2

File tree

2 files changed

+150
-7
lines changed

2 files changed

+150
-7
lines changed

homeassistant/components/risco/alarm_control_panel.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ async def async_setup_entry(
8282
class RiscoAlarm(AlarmControlPanelEntity):
8383
"""Representation of a Risco cloud partition."""
8484

85-
_attr_code_format = CodeFormat.NUMBER
8685
_attr_has_entity_name = True
8786
_attr_name = None
8887

@@ -100,8 +99,13 @@ def __init__(
10099
self._partition_id = partition_id
101100
self._partition = partition
102101
self._code = code
103-
self._attr_code_arm_required = options[CONF_CODE_ARM_REQUIRED]
104-
self._code_disarm_required = options[CONF_CODE_DISARM_REQUIRED]
102+
arm_required = options[CONF_CODE_ARM_REQUIRED]
103+
disarm_required = options[CONF_CODE_DISARM_REQUIRED]
104+
self._attr_code_arm_required = arm_required
105+
self._code_disarm_required = disarm_required
106+
self._attr_code_format = (
107+
CodeFormat.NUMBER if arm_required or disarm_required else None
108+
)
105109
self._risco_to_ha = options[CONF_RISCO_STATES_TO_HA]
106110
self._ha_to_risco = options[CONF_HA_STATES_TO_RISCO]
107111
for state in self._ha_to_risco:

tests/components/risco/test_alarm_control_panel.py

Lines changed: 143 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
SECOND_LOCAL_ENTITY_ID = "alarm_control_panel.name_1"
3636

3737
CODES_REQUIRED_OPTIONS = {"code_arm_required": True, "code_disarm_required": True}
38+
CODES_NOT_REQUIRED_OPTIONS = {"code_arm_required": False, "code_disarm_required": False}
3839
TEST_RISCO_TO_HA = {
3940
"arm": AlarmControlPanelState.ARMED_AWAY,
4041
"partial_arm": AlarmControlPanelState.ARMED_HOME,
@@ -388,7 +389,8 @@ async def test_cloud_sets_full_custom_mapping(
388389

389390

390391
@pytest.mark.parametrize(
391-
"options", [{**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS}]
392+
"options",
393+
[{**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS}],
392394
)
393395
async def test_cloud_sets_with_correct_code(
394396
hass: HomeAssistant, two_part_cloud_alarm, setup_risco_cloud
@@ -452,7 +454,58 @@ async def test_cloud_sets_with_correct_code(
452454

453455

454456
@pytest.mark.parametrize(
455-
"options", [{**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS}]
457+
"options",
458+
[{**CUSTOM_MAPPING_OPTIONS, **CODES_NOT_REQUIRED_OPTIONS}],
459+
)
460+
async def test_cloud_sets_without_code(
461+
hass: HomeAssistant, two_part_cloud_alarm, setup_risco_cloud
462+
) -> None:
463+
"""Test settings the various modes when code is not required."""
464+
await _test_cloud_service_call(
465+
hass, SERVICE_ALARM_DISARM, "disarm", FIRST_CLOUD_ENTITY_ID, 0
466+
)
467+
await _test_cloud_service_call(
468+
hass, SERVICE_ALARM_DISARM, "disarm", SECOND_CLOUD_ENTITY_ID, 1
469+
)
470+
await _test_cloud_service_call(
471+
hass, SERVICE_ALARM_ARM_AWAY, "arm", FIRST_CLOUD_ENTITY_ID, 0
472+
)
473+
await _test_cloud_service_call(
474+
hass, SERVICE_ALARM_ARM_AWAY, "arm", SECOND_CLOUD_ENTITY_ID, 1
475+
)
476+
await _test_cloud_service_call(
477+
hass, SERVICE_ALARM_ARM_HOME, "partial_arm", FIRST_CLOUD_ENTITY_ID, 0
478+
)
479+
await _test_cloud_service_call(
480+
hass, SERVICE_ALARM_ARM_HOME, "partial_arm", SECOND_CLOUD_ENTITY_ID, 1
481+
)
482+
await _test_cloud_service_call(
483+
hass, SERVICE_ALARM_ARM_NIGHT, "group_arm", FIRST_CLOUD_ENTITY_ID, 0, "C"
484+
)
485+
await _test_cloud_service_call(
486+
hass, SERVICE_ALARM_ARM_NIGHT, "group_arm", SECOND_CLOUD_ENTITY_ID, 1, "C"
487+
)
488+
with pytest.raises(HomeAssistantError):
489+
await _test_cloud_no_service_call(
490+
hass,
491+
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
492+
"partial_arm",
493+
FIRST_CLOUD_ENTITY_ID,
494+
0,
495+
)
496+
with pytest.raises(HomeAssistantError):
497+
await _test_cloud_no_service_call(
498+
hass,
499+
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
500+
"partial_arm",
501+
SECOND_CLOUD_ENTITY_ID,
502+
1,
503+
)
504+
505+
506+
@pytest.mark.parametrize(
507+
"options",
508+
[{**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS}],
456509
)
457510
async def test_cloud_sets_with_incorrect_code(
458511
hass: HomeAssistant, two_part_cloud_alarm, setup_risco_cloud
@@ -837,7 +890,8 @@ async def test_local_sets_full_custom_mapping(
837890

838891

839892
@pytest.mark.parametrize(
840-
"options", [{**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS}]
893+
"options",
894+
[{**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS}],
841895
)
842896
async def test_local_sets_with_correct_code(
843897
hass: HomeAssistant, two_part_local_alarm, setup_risco_local
@@ -931,7 +985,8 @@ async def test_local_sets_with_correct_code(
931985

932986

933987
@pytest.mark.parametrize(
934-
"options", [{**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS}]
988+
"options",
989+
[{**CUSTOM_MAPPING_OPTIONS, **CODES_REQUIRED_OPTIONS}],
935990
)
936991
async def test_local_sets_with_incorrect_code(
937992
hass: HomeAssistant, two_part_local_alarm, setup_risco_local
@@ -1020,3 +1075,87 @@ async def test_local_sets_with_incorrect_code(
10201075
two_part_local_alarm[1],
10211076
**code,
10221077
)
1078+
1079+
1080+
@pytest.mark.parametrize(
1081+
"options",
1082+
[{**CUSTOM_MAPPING_OPTIONS, **CODES_NOT_REQUIRED_OPTIONS}],
1083+
)
1084+
async def test_local_sets_without_code(
1085+
hass: HomeAssistant, two_part_local_alarm, setup_risco_local
1086+
) -> None:
1087+
"""Test settings the various modes when code is not required."""
1088+
await _test_local_service_call(
1089+
hass,
1090+
SERVICE_ALARM_DISARM,
1091+
"disarm",
1092+
FIRST_LOCAL_ENTITY_ID,
1093+
two_part_local_alarm[0],
1094+
)
1095+
await _test_local_service_call(
1096+
hass,
1097+
SERVICE_ALARM_DISARM,
1098+
"disarm",
1099+
SECOND_LOCAL_ENTITY_ID,
1100+
two_part_local_alarm[1],
1101+
)
1102+
await _test_local_service_call(
1103+
hass,
1104+
SERVICE_ALARM_ARM_AWAY,
1105+
"arm",
1106+
FIRST_LOCAL_ENTITY_ID,
1107+
two_part_local_alarm[0],
1108+
)
1109+
await _test_local_service_call(
1110+
hass,
1111+
SERVICE_ALARM_ARM_AWAY,
1112+
"arm",
1113+
SECOND_LOCAL_ENTITY_ID,
1114+
two_part_local_alarm[1],
1115+
)
1116+
await _test_local_service_call(
1117+
hass,
1118+
SERVICE_ALARM_ARM_HOME,
1119+
"partial_arm",
1120+
FIRST_LOCAL_ENTITY_ID,
1121+
two_part_local_alarm[0],
1122+
)
1123+
await _test_local_service_call(
1124+
hass,
1125+
SERVICE_ALARM_ARM_HOME,
1126+
"partial_arm",
1127+
SECOND_LOCAL_ENTITY_ID,
1128+
two_part_local_alarm[1],
1129+
)
1130+
await _test_local_service_call(
1131+
hass,
1132+
SERVICE_ALARM_ARM_NIGHT,
1133+
"group_arm",
1134+
FIRST_LOCAL_ENTITY_ID,
1135+
two_part_local_alarm[0],
1136+
"C",
1137+
)
1138+
await _test_local_service_call(
1139+
hass,
1140+
SERVICE_ALARM_ARM_NIGHT,
1141+
"group_arm",
1142+
SECOND_LOCAL_ENTITY_ID,
1143+
two_part_local_alarm[1],
1144+
"C",
1145+
)
1146+
with pytest.raises(HomeAssistantError):
1147+
await _test_local_no_service_call(
1148+
hass,
1149+
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
1150+
"partial_arm",
1151+
FIRST_LOCAL_ENTITY_ID,
1152+
two_part_local_alarm[0],
1153+
)
1154+
with pytest.raises(HomeAssistantError):
1155+
await _test_local_no_service_call(
1156+
hass,
1157+
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
1158+
"partial_arm",
1159+
SECOND_LOCAL_ENTITY_ID,
1160+
two_part_local_alarm[1],
1161+
)

0 commit comments

Comments
 (0)