diff --git a/homeassistant/components/homematicip_cloud/alarm_control_panel.py b/homeassistant/components/homematicip_cloud/alarm_control_panel.py index f70a2193f124..cd1d1750a02e 100644 --- a/homeassistant/components/homematicip_cloud/alarm_control_panel.py +++ b/homeassistant/components/homematicip_cloud/alarm_control_panel.py @@ -48,6 +48,7 @@ class HomematicipAlarmControlPanelEntity(AlarmControlPanelEntity): def __init__(self, hap: HomematicipHAP) -> None: """Initialize the alarm control panel.""" + self._hap = hap self._home: AsyncHome = hap.home @property @@ -97,20 +98,26 @@ class HomematicipAlarmControlPanelEntity(AlarmControlPanelEntity): result = await self._home.set_security_zones_activation_async( internal, external ) + problems = sorted( + label.strip() + for label in self._home.get_security_zone_activation_problems(result) + ) # a request-based panel answers 200 without arming when a sensor blocks it - if result.success: + if result.success and not problems: + # the access point does not push a refusal, so the reply is published here + self._hap.async_set_arming_blocked(False) return - problems = self._home.get_security_zone_activation_problems(result) if not problems: raise HomeAssistantError( translation_domain=DOMAIN, translation_key="alarm_activation_failed", ) + self._hap.async_set_arming_blocked(True) raise HomeAssistantError( translation_domain=DOMAIN, translation_key="alarm_activation_blocked", - translation_placeholders={"devices": ", ".join(sorted(problems))}, + translation_placeholders={"devices": ", ".join(problems)}, ) @override diff --git a/homeassistant/components/homematicip_cloud/binary_sensor.py b/homeassistant/components/homematicip_cloud/binary_sensor.py index e43caf3c0be3..681d97f27744 100644 --- a/homeassistant/components/homematicip_cloud/binary_sensor.py +++ b/homeassistant/components/homematicip_cloud/binary_sensor.py @@ -45,11 +45,13 @@ from homeassistant.components.binary_sensor import ( BinarySensorEntity, BinarySensorEntityDescription, ) +from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN +from .const import DOMAIN, SIGNAL_ARMING_BLOCKED from .entity import HomematicipGenericEntity from .hap import HomematicIPConfigEntry, HomematicipHAP from .helpers import ( @@ -330,7 +332,10 @@ async def async_setup_entry( ) -> None: """Set up the HomematicIP Cloud binary sensor from a config entry.""" hap = config_entry.runtime_data - entities: list[HomematicipGenericEntity] = [HomematicipCloudConnectionSensor(hap)] + entities: list[HomematicipGenericEntity] = [ + HomematicipCloudConnectionSensor(hap), + HomematicipArmingBlockedSensor(hap), + ] for device in hap.home.devices: if isinstance(device, AccelerationSensor): entities.append(HomematicipAccelerationSensor(hap, device)) @@ -464,6 +469,51 @@ class HomematicipCloudConnectionSensor(HomematicipGenericEntity, BinarySensorEnt return True +class HomematicipArmingBlockedSensor(HomematicipGenericEntity, BinarySensorEntity): + """Representation of a refused arming attempt of the alarm control panel.""" + + entity_description = BinarySensorEntityDescription( + key="arming_blocked", + translation_key="arming_blocked", + device_class=BinarySensorDeviceClass.PROBLEM, + entity_category=EntityCategory.DIAGNOSTIC, + ) + + def __init__(self, hap: HomematicipHAP) -> None: + """Initialize the arming blocked sensor.""" + super().__init__( + hap, + hap.home, + feature_id=self.entity_description.key, + use_description_name=True, + ) + + @property + @override + def device_info(self) -> DeviceInfo: + """Return device specific attributes.""" + # the access point is registered before the platforms are set up + return DeviceInfo(identifiers={(DOMAIN, self._home.id)}) + + @property + @override + def is_on(self) -> bool: + """Return true if the last arming attempt was refused.""" + return self._hap.arming_blocked + + @override + async def async_added_to_hass(self) -> None: + """Register callbacks.""" + await super().async_added_to_hass() + self.async_on_remove( + async_dispatcher_connect( + self.hass, + SIGNAL_ARMING_BLOCKED.format(self._hap.config_entry.entry_id), + self.async_write_ha_state, + ) + ) + + class HomematicipBaseActionSensor(HomematicipGenericEntity, BinarySensorEntity): """Representation of the HomematicIP base action sensor.""" diff --git a/homeassistant/components/homematicip_cloud/const.py b/homeassistant/components/homematicip_cloud/const.py index a8a47240915f..d64769d2b5e6 100644 --- a/homeassistant/components/homematicip_cloud/const.py +++ b/homeassistant/components/homematicip_cloud/const.py @@ -23,6 +23,8 @@ PLATFORMS = [ Platform.WEATHER, ] +SIGNAL_ARMING_BLOCKED = f"{DOMAIN}_arming_blocked_{{}}" + CONF_ACCESSPOINT = "accesspoint" CONF_AUTHTOKEN = "authtoken" diff --git a/homeassistant/components/homematicip_cloud/hap.py b/homeassistant/components/homematicip_cloud/hap.py index 041fdf69ae2a..158cb671f048 100644 --- a/homeassistant/components/homematicip_cloud/hap.py +++ b/homeassistant/components/homematicip_cloud/hap.py @@ -20,6 +20,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.httpx_client import get_async_client from .const import ( @@ -29,6 +30,7 @@ from .const import ( HMIPC_NAME, HMIPC_PIN, PLATFORMS, + SIGNAL_ARMING_BLOCKED, ) from .errors import HmipcConnectionError @@ -128,6 +130,15 @@ class HomematicipHAP: self._get_state_task: asyncio.Task | None = None self.hmip_device_by_entity_id: dict[str, Any] = {} self.reset_connection_listener: Callable | None = None + self.arming_blocked = False + + @callback + def async_set_arming_blocked(self, blocked: bool) -> None: + """Publish whether the last arming attempt was refused.""" + self.arming_blocked = blocked + async_dispatcher_send( + self.hass, SIGNAL_ARMING_BLOCKED.format(self.config_entry.entry_id) + ) async def async_setup(self, tries: int = 0) -> bool: """Initialize connection.""" diff --git a/homeassistant/components/homematicip_cloud/strings.json b/homeassistant/components/homematicip_cloud/strings.json index affde32111c8..2e33882efc16 100644 --- a/homeassistant/components/homematicip_cloud/strings.json +++ b/homeassistant/components/homematicip_cloud/strings.json @@ -37,6 +37,9 @@ }, "entity": { "binary_sensor": { + "arming_blocked": { + "name": "Arming blocked" + }, "chamber_degraded": { "name": "Chamber degraded" }, diff --git a/tests/components/homematicip_cloud/snapshots/test_binary_sensor.ambr b/tests/components/homematicip_cloud/snapshots/test_binary_sensor.ambr index 67ddde86307b..e673d7f413ab 100644 --- a/tests/components/homematicip_cloud/snapshots/test_binary_sensor.ambr +++ b/tests/components/homematicip_cloud/snapshots/test_binary_sensor.ambr @@ -1,4 +1,107 @@ # serializer version: 1 +# name: test_hmip_arming_blocked_sensor[binary_sensor.home_arming_blocked-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.home_arming_blocked', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Arming blocked', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': 'mdi:alert', + 'original_name': 'Arming blocked', + 'platform': 'homematicip_cloud', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'arming_blocked', + 'unique_id': '00000000-0000-0000-0000-000000000001_arming_blocked', + 'unit_of_measurement': None, + }) +# --- +# name: test_hmip_arming_blocked_sensor[binary_sensor.home_arming_blocked-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'problem', + : 'Home Arming blocked', + : 'mdi:alert', + }), + 'context': , + 'entity_id': 'binary_sensor.home_arming_blocked', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_hmip_arming_blocked_sensor[binary_sensor.home_cloud_connection-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.home_cloud_connection', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Cloud connection', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:access-point-network', + 'original_name': 'Cloud connection', + 'platform': 'homematicip_cloud', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'cloud_connection', + 'unique_id': '00000000-0000-0000-0000-000000000001_cloud_connection', + 'unit_of_measurement': None, + }) +# --- +# name: test_hmip_arming_blocked_sensor[binary_sensor.home_cloud_connection-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'Home Cloud connection', + : 'mdi:access-point-network', + }), + 'context': , + 'entity_id': 'binary_sensor.home_cloud_connection', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- # name: test_hmip_heating_output_sensor[binary_sensor.bwth_1_heating-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -55,6 +158,58 @@ 'state': 'on', }) # --- +# name: test_hmip_heating_output_sensor[binary_sensor.home_arming_blocked-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.home_arming_blocked', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Arming blocked', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': 'mdi:alert', + 'original_name': 'Arming blocked', + 'platform': 'homematicip_cloud', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'arming_blocked', + 'unique_id': '00000000-0000-0000-0000-000000000001_arming_blocked', + 'unit_of_measurement': None, + }) +# --- +# name: test_hmip_heating_output_sensor[binary_sensor.home_arming_blocked-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'problem', + : 'Home Arming blocked', + : 'mdi:alert', + }), + 'context': , + 'entity_id': 'binary_sensor.home_arming_blocked', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- # name: test_hmip_heating_output_sensor[binary_sensor.home_cloud_connection-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -163,6 +318,58 @@ 'state': 'off', }) # --- +# name: test_hmip_motion_detector_switch_outdoor[binary_sensor.home_arming_blocked-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.home_arming_blocked', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Arming blocked', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': 'mdi:alert', + 'original_name': 'Arming blocked', + 'platform': 'homematicip_cloud', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'arming_blocked', + 'unique_id': '00000000-0000-0000-0000-000000000001_arming_blocked', + 'unit_of_measurement': None, + }) +# --- +# name: test_hmip_motion_detector_switch_outdoor[binary_sensor.home_arming_blocked-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'problem', + : 'Home Arming blocked', + : 'mdi:alert', + }), + 'context': , + 'entity_id': 'binary_sensor.home_arming_blocked', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- # name: test_hmip_motion_detector_switch_outdoor[binary_sensor.home_cloud_connection-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -214,6 +421,58 @@ 'state': 'on', }) # --- +# name: test_hmip_water_alarm_sensor[binary_sensor.home_arming_blocked-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.home_arming_blocked', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Arming blocked', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': 'mdi:alert', + 'original_name': 'Arming blocked', + 'platform': 'homematicip_cloud', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'arming_blocked', + 'unique_id': '00000000-0000-0000-0000-000000000001_arming_blocked', + 'unit_of_measurement': None, + }) +# --- +# name: test_hmip_water_alarm_sensor[binary_sensor.home_arming_blocked-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'problem', + : 'Home Arming blocked', + : 'mdi:alert', + }), + 'context': , + 'entity_id': 'binary_sensor.home_arming_blocked', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- # name: test_hmip_water_alarm_sensor[binary_sensor.home_cloud_connection-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ diff --git a/tests/components/homematicip_cloud/test_alarm_control_panel.py b/tests/components/homematicip_cloud/test_alarm_control_panel.py index ed4f6097453a..16109529d2f1 100644 --- a/tests/components/homematicip_cloud/test_alarm_control_panel.py +++ b/tests/components/homematicip_cloud/test_alarm_control_panel.py @@ -63,13 +63,12 @@ async def test_hmip_alarm_control_panel( home = mock_hap.home # the mocked connection answers every call with a tuple, not a RestResult - home.set_security_zones_activation_async.return_value = Mock(success=True) + home.set_security_zones_activation_async.return_value = Mock(success=True, json={}) await hass.services.async_call( "alarm_control_panel", "alarm_arm_away", {"entity_id": entity_id}, blocking=True ) - assert home.mock_calls[-1][0] == "set_security_zones_activation_async" - assert home.mock_calls[-1][1] == (True, True) + home.set_security_zones_activation_async.assert_awaited_with(True, True) await _async_manipulate_security_zones( hass, home, internal_active=True, external_active=True ) @@ -78,24 +77,21 @@ async def test_hmip_alarm_control_panel( await hass.services.async_call( "alarm_control_panel", "alarm_arm_home", {"entity_id": entity_id}, blocking=True ) - assert home.mock_calls[-1][0] == "set_security_zones_activation_async" - assert home.mock_calls[-1][1] == (False, True) + home.set_security_zones_activation_async.assert_awaited_with(False, True) await _async_manipulate_security_zones(hass, home, external_active=True) assert hass.states.get(entity_id).state == AlarmControlPanelState.ARMED_HOME await hass.services.async_call( "alarm_control_panel", "alarm_disarm", {"entity_id": entity_id}, blocking=True ) - assert home.mock_calls[-1][0] == "set_security_zones_activation_async" - assert home.mock_calls[-1][1] == (False, False) + home.set_security_zones_activation_async.assert_awaited_with(False, False) await _async_manipulate_security_zones(hass, home) assert hass.states.get(entity_id).state == AlarmControlPanelState.DISARMED await hass.services.async_call( "alarm_control_panel", "alarm_arm_away", {"entity_id": entity_id}, blocking=True ) - assert home.mock_calls[-1][0] == "set_security_zones_activation_async" - assert home.mock_calls[-1][1] == (True, True) + home.set_security_zones_activation_async.assert_awaited_with(True, True) await _async_manipulate_security_zones( hass, home, internal_active=True, external_active=True, alarm_triggered=True ) @@ -104,8 +100,7 @@ async def test_hmip_alarm_control_panel( await hass.services.async_call( "alarm_control_panel", "alarm_arm_home", {"entity_id": entity_id}, blocking=True ) - assert home.mock_calls[-1][0] == "set_security_zones_activation_async" - assert home.mock_calls[-1][1] == (False, True) + home.set_security_zones_activation_async.assert_awaited_with(False, True) await _async_manipulate_security_zones( hass, home, external_active=True, alarm_triggered=True ) diff --git a/tests/components/homematicip_cloud/test_binary_sensor.py b/tests/components/homematicip_cloud/test_binary_sensor.py index aee9a110d35b..77d22ef95a60 100644 --- a/tests/components/homematicip_cloud/test_binary_sensor.py +++ b/tests/components/homematicip_cloud/test_binary_sensor.py @@ -1,7 +1,7 @@ """Tests for HomematicIP Cloud binary sensor.""" from typing import Any -from unittest.mock import patch +from unittest.mock import Mock, patch from homematicip.base.enums import ( BinaryBehaviorType, @@ -46,6 +46,7 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er from .helper import HomeFactory, async_manipulate_test_data, get_and_check_entity_basics @@ -169,6 +170,73 @@ async def test_hmip_home_cloud_connection_sensor( assert ha_state.state == STATE_OFF +async def test_hmip_arming_blocked_sensor( + hass: HomeAssistant, + default_mock_hap_factory: HomeFactory, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test the arming blocked sensor of the access point.""" + with patch( + "homeassistant.components.homematicip_cloud.hap.PLATFORMS", + [Platform.BINARY_SENSOR], + ): + mock_hap = await default_mock_hap_factory.async_get_mock_hap( + test_devices=[], test_groups=[] + ) + + await snapshot_platform( + hass, entity_registry, snapshot, mock_hap.config_entry.entry_id + ) + + +async def test_hmip_arming_blocked_sensor_follows_arming( + hass: HomeAssistant, default_mock_hap_factory: HomeFactory +) -> None: + """Test the arming blocked sensor follows the replies of the alarm panel.""" + entity_id = "binary_sensor.home_arming_blocked" + alarm_entity_id = "alarm_control_panel.hmip_alarm_control_panel" + mock_hap = await default_mock_hap_factory.async_get_mock_hap( + test_groups=["EXTERNAL", "INTERNAL"] + ) + assert hass.states.get(entity_id).state == STATE_OFF + + home = mock_hap.home + home.set_security_zones_activation_async.return_value = Mock( + success=False, + json={ + "channelActivationProblems": {"3014F7110000000000000005:1": ["WINDOW_OPEN"]} + }, + ) + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + "alarm_control_panel", + "alarm_arm_away", + {"entity_id": alarm_entity_id}, + blocking=True, + ) + assert hass.states.get(entity_id).state == STATE_ON + + home.set_security_zones_activation_async.return_value = Mock(success=False, json={}) + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + "alarm_control_panel", + "alarm_arm_away", + {"entity_id": alarm_entity_id}, + blocking=True, + ) + assert hass.states.get(entity_id).state == STATE_ON + + home.set_security_zones_activation_async.return_value = Mock(success=True, json={}) + await hass.services.async_call( + "alarm_control_panel", + "alarm_disarm", + {"entity_id": alarm_entity_id}, + blocking=True, + ) + assert hass.states.get(entity_id).state == STATE_OFF + + async def test_hmip_acceleration_sensor( hass: HomeAssistant, default_mock_hap_factory: HomeFactory ) -> None: diff --git a/tests/components/homematicip_cloud/test_device.py b/tests/components/homematicip_cloud/test_device.py index 7732f0956988..854b4b6a4734 100644 --- a/tests/components/homematicip_cloud/test_device.py +++ b/tests/components/homematicip_cloud/test_device.py @@ -24,7 +24,7 @@ async def test_hmip_load_all_supported_devices( test_devices=None, test_groups=None ) - assert len(mock_hap.hmip_device_by_entity_id) == 415 + assert len(mock_hap.hmip_device_by_entity_id) == 416 async def test_hmip_remove_device(