diff --git a/.coveragerc b/.coveragerc index 5a360e89226..c95f47815e8 100644 --- a/.coveragerc +++ b/.coveragerc @@ -121,7 +121,6 @@ omit = homeassistant/components/buienradar/util.py homeassistant/components/buienradar/weather.py homeassistant/components/caldav/calendar.py - homeassistant/components/canary/alarm_control_panel.py homeassistant/components/canary/camera.py homeassistant/components/cast/* homeassistant/components/cert_expiry/helper.py diff --git a/tests/components/canary/__init__.py b/tests/components/canary/__init__.py index 2ad275165d6..e8effcb4c3f 100644 --- a/tests/components/canary/__init__.py +++ b/tests/components/canary/__init__.py @@ -3,24 +3,6 @@ from unittest.mock import MagicMock, PropertyMock from canary.api import SensorType -from homeassistant.components.homeassistant import ( - DOMAIN as HA_DOMAIN, - SERVICE_UPDATE_ENTITY, -) -from homeassistant.const import ATTR_ENTITY_ID -from homeassistant.core import HomeAssistant - - -async def update_entity(hass: HomeAssistant, entity_id: str) -> None: - """Run an update action for an entity.""" - await hass.services.async_call( - HA_DOMAIN, - SERVICE_UPDATE_ENTITY, - {ATTR_ENTITY_ID: entity_id}, - blocking=True, - ) - await hass.async_block_till_done() - def mock_device(device_id, name, is_online=True, device_type_name=None): """Mock Canary Device class.""" @@ -34,13 +16,17 @@ def mock_device(device_id, name, is_online=True, device_type_name=None): return device -def mock_location(location_id, name, is_celsius=True, devices=None): +def mock_location( + location_id, name, is_celsius=True, devices=None, mode=None, is_private=False +): """Mock Canary Location class.""" location = MagicMock() type(location).location_id = PropertyMock(return_value=location_id) type(location).name = PropertyMock(return_value=name) type(location).is_celsius = PropertyMock(return_value=is_celsius) + type(location).is_private = PropertyMock(return_value=is_private) type(location).devices = PropertyMock(return_value=devices or []) + type(location).mode = PropertyMock(return_value=mode) return location diff --git a/tests/components/canary/test_alarm_control_panel.py b/tests/components/canary/test_alarm_control_panel.py new file mode 100644 index 00000000000..ebf4b0ba385 --- /dev/null +++ b/tests/components/canary/test_alarm_control_panel.py @@ -0,0 +1,170 @@ +"""The tests for the Canary alarm_control_panel platform.""" +from canary.api import LOCATION_MODE_AWAY, LOCATION_MODE_HOME, LOCATION_MODE_NIGHT + +from homeassistant.components.alarm_control_panel import DOMAIN as ALARM_DOMAIN +from homeassistant.components.canary import DOMAIN +from homeassistant.const import ( + SERVICE_ALARM_ARM_AWAY, + SERVICE_ALARM_ARM_HOME, + SERVICE_ALARM_ARM_NIGHT, + SERVICE_ALARM_DISARM, + STATE_ALARM_ARMED_AWAY, + STATE_ALARM_ARMED_HOME, + STATE_ALARM_ARMED_NIGHT, + STATE_ALARM_DISARMED, + STATE_UNKNOWN, +) +from homeassistant.setup import async_setup_component + +from . import mock_device, mock_location, mock_mode + +from tests.async_mock import PropertyMock, patch +from tests.common import mock_registry + + +async def test_alarm_control_panel(hass, canary) -> None: + """Test the creation and values of the alarm_control_panel for Canary.""" + await async_setup_component(hass, "persistent_notification", {}) + + registry = mock_registry(hass) + online_device_at_home = mock_device(20, "Dining Room", True, "Canary Pro") + + mocked_location = mock_location( + location_id=100, + name="Home", + is_celsius=True, + is_private=False, + mode=mock_mode(7, "standby"), + devices=[online_device_at_home], + ) + + instance = canary.return_value + instance.get_locations.return_value = [mocked_location] + + config = {DOMAIN: {"username": "test-username", "password": "test-password"}} + with patch( + "homeassistant.components.canary.CANARY_COMPONENTS", ["alarm_control_panel"] + ): + assert await async_setup_component(hass, DOMAIN, config) + await hass.async_block_till_done() + + entity_id = "alarm_control_panel.home" + entity_entry = registry.async_get(entity_id) + assert not entity_entry + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_UNKNOWN + assert not state.attributes["private"] + + # test private system + type(mocked_location).is_private = PropertyMock(return_value=True) + + await hass.helpers.entity_component.async_update_entity(entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_ALARM_DISARMED + assert state.attributes["private"] + + type(mocked_location).is_private = PropertyMock(return_value=False) + + # test armed home + type(mocked_location).mode = PropertyMock( + return_value=mock_mode(4, LOCATION_MODE_HOME) + ) + + await hass.helpers.entity_component.async_update_entity(entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_ALARM_ARMED_HOME + + # test armed away + type(mocked_location).mode = PropertyMock( + return_value=mock_mode(5, LOCATION_MODE_AWAY) + ) + + await hass.helpers.entity_component.async_update_entity(entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_ALARM_ARMED_AWAY + + # test armed night + type(mocked_location).mode = PropertyMock( + return_value=mock_mode(6, LOCATION_MODE_NIGHT) + ) + + await hass.helpers.entity_component.async_update_entity(entity_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_ALARM_ARMED_NIGHT + + +async def test_alarm_control_panel_services(hass, canary) -> None: + """Test the services of the alarm_control_panel for Canary.""" + await async_setup_component(hass, "persistent_notification", {}) + + online_device_at_home = mock_device(20, "Dining Room", True, "Canary Pro") + + mocked_location = mock_location( + location_id=100, + name="Home", + is_celsius=True, + mode=mock_mode(1, "disarmed"), + devices=[online_device_at_home], + ) + + instance = canary.return_value + instance.get_locations.return_value = [mocked_location] + + config = {DOMAIN: {"username": "test-username", "password": "test-password"}} + with patch( + "homeassistant.components.canary.CANARY_COMPONENTS", ["alarm_control_panel"] + ): + assert await async_setup_component(hass, DOMAIN, config) + await hass.async_block_till_done() + + entity_id = "alarm_control_panel.home" + + # test arm away + await hass.services.async_call( + ALARM_DOMAIN, + SERVICE_ALARM_ARM_AWAY, + service_data={"entity_id": entity_id}, + blocking=True, + ) + instance.set_location_mode.assert_called_with(100, LOCATION_MODE_AWAY, False) + + # test arm home + await hass.services.async_call( + ALARM_DOMAIN, + SERVICE_ALARM_ARM_HOME, + service_data={"entity_id": entity_id}, + blocking=True, + ) + instance.set_location_mode.assert_called_with(100, LOCATION_MODE_HOME, False) + + # test arm night + await hass.services.async_call( + ALARM_DOMAIN, + SERVICE_ALARM_ARM_NIGHT, + service_data={"entity_id": entity_id}, + blocking=True, + ) + instance.set_location_mode.assert_called_with(100, LOCATION_MODE_NIGHT, False) + + # test disarm + await hass.services.async_call( + ALARM_DOMAIN, + SERVICE_ALARM_DISARM, + service_data={"entity_id": entity_id}, + blocking=True, + ) + instance.set_location_mode.assert_called_with(100, "disarmed", True)