Skip to content

Commit

Permalink
Fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST committed Mar 15, 2024
1 parent 80464bc commit a9a6002
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def test_switch_change_alarm_state(hass: HomeAssistant) -> None:
await hass.services.async_call(
"alarm_control_panel",
"alarm_arm_home",
{"entity_id": "alarm_control_panel.testdevice"},
{"entity_id": "alarm_control_panel.testdevice", "code": "1234"},
blocking=True,
)
helper.async_assert_service_values(
Expand All @@ -47,7 +47,7 @@ async def test_switch_change_alarm_state(hass: HomeAssistant) -> None:
await hass.services.async_call(
"alarm_control_panel",
"alarm_arm_away",
{"entity_id": "alarm_control_panel.testdevice"},
{"entity_id": "alarm_control_panel.testdevice", "code": "1234"},
blocking=True,
)
helper.async_assert_service_values(
Expand All @@ -60,7 +60,7 @@ async def test_switch_change_alarm_state(hass: HomeAssistant) -> None:
await hass.services.async_call(
"alarm_control_panel",
"alarm_arm_night",
{"entity_id": "alarm_control_panel.testdevice"},
{"entity_id": "alarm_control_panel.testdevice", "code": "1234"},
blocking=True,
)
helper.async_assert_service_values(
Expand All @@ -73,7 +73,7 @@ async def test_switch_change_alarm_state(hass: HomeAssistant) -> None:
await hass.services.async_call(
"alarm_control_panel",
"alarm_disarm",
{"entity_id": "alarm_control_panel.testdevice"},
{"entity_id": "alarm_control_panel.testdevice", "code": "1234"},
blocking=True,
)
helper.async_assert_service_values(
Expand Down
2 changes: 1 addition & 1 deletion tests/components/manual/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ async def test_with_specific_pending(
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
{ATTR_ENTITY_ID: "alarm_control_panel.test", ATTR_CODE: "1234"},
blocking=True,
)

Expand Down
8 changes: 4 additions & 4 deletions tests/components/manual_mqtt/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ async def test_with_specific_pending(
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
{ATTR_ENTITY_ID: "alarm_control_panel.test", ATTR_CODE: "1234"},
blocking=True,
)

Expand Down Expand Up @@ -1442,7 +1442,7 @@ async def test_state_changes_are_published_to_mqtt(
mqtt_mock.async_publish.reset_mock()

# Arm in home mode
await common.async_alarm_arm_home(hass)
await common.async_alarm_arm_home(hass, "1234")
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
"alarm/state", STATE_ALARM_PENDING, 0, True
Expand All @@ -1462,7 +1462,7 @@ async def test_state_changes_are_published_to_mqtt(
mqtt_mock.async_publish.reset_mock()

# Arm in away mode
await common.async_alarm_arm_away(hass)
await common.async_alarm_arm_away(hass, "1234")
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
"alarm/state", STATE_ALARM_PENDING, 0, True
Expand All @@ -1482,7 +1482,7 @@ async def test_state_changes_are_published_to_mqtt(
mqtt_mock.async_publish.reset_mock()

# Arm in night mode
await common.async_alarm_arm_night(hass)
await common.async_alarm_arm_night(hass, "1234")
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
"alarm/state", STATE_ALARM_PENDING, 0, True
Expand Down
161 changes: 109 additions & 52 deletions tests/components/mqtt/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError

from .test_common import (
help_custom_config,
Expand Down Expand Up @@ -97,6 +97,17 @@
}
}

DEFAULT_CONFIG_CODE_NOT_REQUIRED = {
mqtt.DOMAIN: {
alarm_control_panel.DOMAIN: {
"name": "test",
"state_topic": "alarm/state",
"command_topic": "alarm/command",
"code_arm_required": False,
}
}
}

DEFAULT_CONFIG_CODE = {
mqtt.DOMAIN: {
alarm_control_panel.DOMAIN: {
Expand Down Expand Up @@ -309,13 +320,17 @@ async def test_supported_features(
@pytest.mark.parametrize(
("hass_config", "service", "payload"),
[
(DEFAULT_CONFIG, SERVICE_ALARM_ARM_HOME, "ARM_HOME"),
(DEFAULT_CONFIG, SERVICE_ALARM_ARM_AWAY, "ARM_AWAY"),
(DEFAULT_CONFIG, SERVICE_ALARM_ARM_NIGHT, "ARM_NIGHT"),
(DEFAULT_CONFIG, SERVICE_ALARM_ARM_VACATION, "ARM_VACATION"),
(DEFAULT_CONFIG, SERVICE_ALARM_ARM_CUSTOM_BYPASS, "ARM_CUSTOM_BYPASS"),
(DEFAULT_CONFIG, SERVICE_ALARM_DISARM, "DISARM"),
(DEFAULT_CONFIG, SERVICE_ALARM_TRIGGER, "TRIGGER"),
(DEFAULT_CONFIG_CODE_NOT_REQUIRED, SERVICE_ALARM_ARM_HOME, "ARM_HOME"),
(DEFAULT_CONFIG_CODE_NOT_REQUIRED, SERVICE_ALARM_ARM_AWAY, "ARM_AWAY"),
(DEFAULT_CONFIG_CODE_NOT_REQUIRED, SERVICE_ALARM_ARM_NIGHT, "ARM_NIGHT"),
(DEFAULT_CONFIG_CODE_NOT_REQUIRED, SERVICE_ALARM_ARM_VACATION, "ARM_VACATION"),
(
DEFAULT_CONFIG_CODE_NOT_REQUIRED,
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
"ARM_CUSTOM_BYPASS",
),
(DEFAULT_CONFIG_CODE_NOT_REQUIRED, SERVICE_ALARM_DISARM, "DISARM"),
(DEFAULT_CONFIG_CODE_NOT_REQUIRED, SERVICE_ALARM_TRIGGER, "TRIGGER"),
],
)
async def test_publish_mqtt_no_code(
Expand All @@ -338,34 +353,49 @@ async def test_publish_mqtt_no_code(


@pytest.mark.parametrize(
("hass_config", "service", "payload"),
("hass_config", "service", "payload", "can_raise"),
[
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_ARM_HOME, "ARM_HOME"),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_ARM_AWAY, "ARM_AWAY"),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_ARM_NIGHT, "ARM_NIGHT"),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_ARM_VACATION, "ARM_VACATION"),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_ARM_CUSTOM_BYPASS, "ARM_CUSTOM_BYPASS"),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_DISARM, "DISARM"),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_TRIGGER, "TRIGGER"),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_ARM_HOME, "ARM_HOME", True),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_ARM_AWAY, "ARM_AWAY", True),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_ARM_NIGHT, "ARM_NIGHT", True),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_ARM_VACATION, "ARM_VACATION", True),
(
DEFAULT_CONFIG_CODE,
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
"ARM_CUSTOM_BYPASS",
True,
),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_DISARM, "DISARM", False),
(DEFAULT_CONFIG_CODE, SERVICE_ALARM_TRIGGER, "TRIGGER", False),
],
)
async def test_publish_mqtt_with_code(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
service,
payload,
service: str,
payload: str,
can_raise: bool,
) -> None:
"""Test publishing of MQTT messages when code is configured."""
mqtt_mock = await mqtt_mock_entry()
call_count = mqtt_mock.async_publish.call_count

# No code provided, should not publish
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
blocking=True,
)
if can_raise:
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
blocking=True,
)
else:
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
blocking=True,
)
assert mqtt_mock.async_publish.call_count == call_count

# Wrong code provided, should not publish
Expand All @@ -388,38 +418,49 @@ async def test_publish_mqtt_with_code(


@pytest.mark.parametrize(
("hass_config", "service", "payload"),
("hass_config", "service", "payload", "can_raise"),
[
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_ARM_HOME, "ARM_HOME"),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_ARM_AWAY, "ARM_AWAY"),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_ARM_NIGHT, "ARM_NIGHT"),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_ARM_VACATION, "ARM_VACATION"),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_ARM_HOME, "ARM_HOME", True),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_ARM_AWAY, "ARM_AWAY", True),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_ARM_NIGHT, "ARM_NIGHT", True),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_ARM_VACATION, "ARM_VACATION", True),
(
DEFAULT_CONFIG_REMOTE_CODE,
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
"ARM_CUSTOM_BYPASS",
True,
),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_DISARM, "DISARM"),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_TRIGGER, "TRIGGER"),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_DISARM, "DISARM", False),
(DEFAULT_CONFIG_REMOTE_CODE, SERVICE_ALARM_TRIGGER, "TRIGGER", False),
],
)
async def test_publish_mqtt_with_remote_code(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
service,
payload,
service: str,
payload: str,
can_raise: bool,
) -> None:
"""Test publishing of MQTT messages when remode code is configured."""
mqtt_mock = await mqtt_mock_entry()
call_count = mqtt_mock.async_publish.call_count

# No code provided, should not publish
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
blocking=True,
)
if can_raise:
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
blocking=True,
)
else:
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
blocking=True,
)
assert mqtt_mock.async_publish.call_count == call_count

# Any code numbered provided, should publish
Expand All @@ -433,38 +474,54 @@ async def test_publish_mqtt_with_remote_code(


@pytest.mark.parametrize(
("hass_config", "service", "payload"),
("hass_config", "service", "payload", "can_raise"),
[
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_ARM_HOME, "ARM_HOME"),
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_ARM_AWAY, "ARM_AWAY"),
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_ARM_NIGHT, "ARM_NIGHT"),
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_ARM_VACATION, "ARM_VACATION"),
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_ARM_HOME, "ARM_HOME", True),
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_ARM_AWAY, "ARM_AWAY", True),
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_ARM_NIGHT, "ARM_NIGHT", True),
(
DEFAULT_CONFIG_REMOTE_CODE_TEXT,
SERVICE_ALARM_ARM_VACATION,
"ARM_VACATION",
True,
),
(
DEFAULT_CONFIG_REMOTE_CODE_TEXT,
SERVICE_ALARM_ARM_CUSTOM_BYPASS,
"ARM_CUSTOM_BYPASS",
True,
),
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_DISARM, "DISARM"),
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_TRIGGER, "TRIGGER"),
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_DISARM, "DISARM", False),
(DEFAULT_CONFIG_REMOTE_CODE_TEXT, SERVICE_ALARM_TRIGGER, "TRIGGER", False),
],
)
async def test_publish_mqtt_with_remote_code_text(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
service: str,
payload: str,
can_raise: bool,
) -> None:
"""Test publishing of MQTT messages when remote text code is configured."""
mqtt_mock = await mqtt_mock_entry()
call_count = mqtt_mock.async_publish.call_count

# No code provided, should not publish
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
blocking=True,
)
if can_raise:
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
blocking=True,
)
else:
await hass.services.async_call(
alarm_control_panel.DOMAIN,
service,
{ATTR_ENTITY_ID: "alarm_control_panel.test"},
blocking=True,
)
assert mqtt_mock.async_publish.call_count == call_count

# Any code numbered provided, should publish
Expand Down

0 comments on commit a9a6002

Please sign in to comment.