mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add Homee alarm-control-panel platform (#140041)
* Add alarm control panel * Add alarm control panel tests * add disarm function * reuse state setting code * change sleeping to night * review change 1 * fix review comments * fix review comments
This commit is contained in:
parent
aa4c41abe8
commit
3ff095cc51
@ -15,6 +15,7 @@ from .const import DOMAIN
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORMS = [
|
PLATFORMS = [
|
||||||
|
Platform.ALARM_CONTROL_PANEL,
|
||||||
Platform.BINARY_SENSOR,
|
Platform.BINARY_SENSOR,
|
||||||
Platform.BUTTON,
|
Platform.BUTTON,
|
||||||
Platform.CLIMATE,
|
Platform.CLIMATE,
|
||||||
|
138
homeassistant/components/homee/alarm_control_panel.py
Normal file
138
homeassistant/components/homee/alarm_control_panel.py
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
"""The Homee alarm control panel platform."""
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from pyHomee.const import AttributeChangedBy, AttributeType
|
||||||
|
from pyHomee.model import HomeeAttribute
|
||||||
|
|
||||||
|
from homeassistant.components.alarm_control_panel import (
|
||||||
|
AlarmControlPanelEntity,
|
||||||
|
AlarmControlPanelEntityDescription,
|
||||||
|
AlarmControlPanelEntityFeature,
|
||||||
|
AlarmControlPanelState,
|
||||||
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
|
from . import DOMAIN, HomeeConfigEntry
|
||||||
|
from .entity import HomeeEntity
|
||||||
|
from .helpers import get_name_for_enum
|
||||||
|
|
||||||
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, kw_only=True)
|
||||||
|
class HomeeAlarmControlPanelEntityDescription(AlarmControlPanelEntityDescription):
|
||||||
|
"""A class that describes Homee alarm control panel entities."""
|
||||||
|
|
||||||
|
code_arm_required: bool = False
|
||||||
|
state_list: list[AlarmControlPanelState]
|
||||||
|
|
||||||
|
|
||||||
|
ALARM_DESCRIPTIONS = {
|
||||||
|
AttributeType.HOMEE_MODE: HomeeAlarmControlPanelEntityDescription(
|
||||||
|
key="homee_mode",
|
||||||
|
code_arm_required=False,
|
||||||
|
state_list=[
|
||||||
|
AlarmControlPanelState.ARMED_HOME,
|
||||||
|
AlarmControlPanelState.ARMED_NIGHT,
|
||||||
|
AlarmControlPanelState.ARMED_AWAY,
|
||||||
|
AlarmControlPanelState.ARMED_VACATION,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_supported_features(
|
||||||
|
state_list: list[AlarmControlPanelState],
|
||||||
|
) -> AlarmControlPanelEntityFeature:
|
||||||
|
"""Return supported features based on the state list."""
|
||||||
|
supported_features = AlarmControlPanelEntityFeature(0)
|
||||||
|
if AlarmControlPanelState.ARMED_HOME in state_list:
|
||||||
|
supported_features |= AlarmControlPanelEntityFeature.ARM_HOME
|
||||||
|
if AlarmControlPanelState.ARMED_AWAY in state_list:
|
||||||
|
supported_features |= AlarmControlPanelEntityFeature.ARM_AWAY
|
||||||
|
if AlarmControlPanelState.ARMED_NIGHT in state_list:
|
||||||
|
supported_features |= AlarmControlPanelEntityFeature.ARM_NIGHT
|
||||||
|
if AlarmControlPanelState.ARMED_VACATION in state_list:
|
||||||
|
supported_features |= AlarmControlPanelEntityFeature.ARM_VACATION
|
||||||
|
return supported_features
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: HomeeConfigEntry,
|
||||||
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Add the Homee platform for the alarm control panel component."""
|
||||||
|
|
||||||
|
async_add_entities(
|
||||||
|
HomeeAlarmPanel(attribute, config_entry, ALARM_DESCRIPTIONS[attribute.type])
|
||||||
|
for node in config_entry.runtime_data.nodes
|
||||||
|
for attribute in node.attributes
|
||||||
|
if attribute.type in ALARM_DESCRIPTIONS and attribute.editable
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class HomeeAlarmPanel(HomeeEntity, AlarmControlPanelEntity):
|
||||||
|
"""Representation of a Homee alarm control panel."""
|
||||||
|
|
||||||
|
entity_description: HomeeAlarmControlPanelEntityDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
attribute: HomeeAttribute,
|
||||||
|
entry: HomeeConfigEntry,
|
||||||
|
description: HomeeAlarmControlPanelEntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize a Homee alarm control panel entity."""
|
||||||
|
super().__init__(attribute, entry)
|
||||||
|
self.entity_description = description
|
||||||
|
self._attr_code_arm_required = description.code_arm_required
|
||||||
|
self._attr_supported_features = get_supported_features(description.state_list)
|
||||||
|
self._attr_translation_key = description.key
|
||||||
|
|
||||||
|
@property
|
||||||
|
def alarm_state(self) -> AlarmControlPanelState:
|
||||||
|
"""Return current state."""
|
||||||
|
return self.entity_description.state_list[int(self._attribute.current_value)]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def changed_by(self) -> str:
|
||||||
|
"""Return by whom or what the entity was last changed."""
|
||||||
|
changed_by_name = get_name_for_enum(
|
||||||
|
AttributeChangedBy, self._attribute.changed_by
|
||||||
|
)
|
||||||
|
return f"{changed_by_name} - {self._attribute.changed_by_id}"
|
||||||
|
|
||||||
|
async def _async_set_alarm_state(self, state: AlarmControlPanelState) -> None:
|
||||||
|
"""Set the alarm state."""
|
||||||
|
if state in self.entity_description.state_list:
|
||||||
|
await self.async_set_homee_value(
|
||||||
|
self.entity_description.state_list.index(state)
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_alarm_disarm(self, code: str | None = None) -> None:
|
||||||
|
"""Send disarm command."""
|
||||||
|
# Since disarm is always present in the UI, we raise an error.
|
||||||
|
raise ServiceValidationError(
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="disarm_not_supported",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_alarm_arm_home(self, code: str | None = None) -> None:
|
||||||
|
"""Send arm home command."""
|
||||||
|
await self._async_set_alarm_state(AlarmControlPanelState.ARMED_HOME)
|
||||||
|
|
||||||
|
async def async_alarm_arm_night(self, code: str | None = None) -> None:
|
||||||
|
"""Send arm night command."""
|
||||||
|
await self._async_set_alarm_state(AlarmControlPanelState.ARMED_NIGHT)
|
||||||
|
|
||||||
|
async def async_alarm_arm_away(self, code: str | None = None) -> None:
|
||||||
|
"""Send arm away command."""
|
||||||
|
await self._async_set_alarm_state(AlarmControlPanelState.ARMED_AWAY)
|
||||||
|
|
||||||
|
async def async_alarm_arm_vacation(self, code: str | None = None) -> None:
|
||||||
|
"""Send arm vacation command."""
|
||||||
|
await self._async_set_alarm_state(AlarmControlPanelState.ARMED_VACATION)
|
@ -27,14 +27,20 @@ class HomeeEntity(Entity):
|
|||||||
)
|
)
|
||||||
self._entry = entry
|
self._entry = entry
|
||||||
node = entry.runtime_data.get_node_by_id(attribute.node_id)
|
node = entry.runtime_data.get_node_by_id(attribute.node_id)
|
||||||
self._attr_device_info = DeviceInfo(
|
# Homee hub itself has node-id -1
|
||||||
identifiers={
|
if node.id == -1:
|
||||||
(DOMAIN, f"{entry.runtime_data.settings.uid}-{attribute.node_id}")
|
self._attr_device_info = DeviceInfo(
|
||||||
},
|
identifiers={(DOMAIN, entry.runtime_data.settings.uid)},
|
||||||
name=node.name,
|
)
|
||||||
model=get_name_for_enum(NodeProfile, node.profile),
|
else:
|
||||||
via_device=(DOMAIN, entry.runtime_data.settings.uid),
|
self._attr_device_info = DeviceInfo(
|
||||||
)
|
identifiers={
|
||||||
|
(DOMAIN, f"{entry.runtime_data.settings.uid}-{attribute.node_id}")
|
||||||
|
},
|
||||||
|
name=node.name,
|
||||||
|
model=get_name_for_enum(NodeProfile, node.profile),
|
||||||
|
via_device=(DOMAIN, entry.runtime_data.settings.uid),
|
||||||
|
)
|
||||||
|
|
||||||
self._host_connected = entry.runtime_data.connected
|
self._host_connected = entry.runtime_data.connected
|
||||||
|
|
||||||
|
@ -26,6 +26,11 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"entity": {
|
"entity": {
|
||||||
|
"alarm_control_panel": {
|
||||||
|
"homee_mode": {
|
||||||
|
"name": "Status"
|
||||||
|
}
|
||||||
|
},
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
"blackout_alarm": {
|
"blackout_alarm": {
|
||||||
"name": "Blackout"
|
"name": "Blackout"
|
||||||
@ -370,6 +375,9 @@
|
|||||||
"connection_closed": {
|
"connection_closed": {
|
||||||
"message": "Could not connect to homee while setting attribute."
|
"message": "Could not connect to homee while setting attribute."
|
||||||
},
|
},
|
||||||
|
"disarm_not_supported": {
|
||||||
|
"message": "Disarm is not supported by homee."
|
||||||
|
},
|
||||||
"invalid_preset_mode": {
|
"invalid_preset_mode": {
|
||||||
"message": "Invalid preset mode: {preset_mode}. Turning on is only supported with preset mode 'Manual'."
|
"message": "Invalid preset mode: {preset_mode}. Turning on is only supported with preset mode 'Manual'."
|
||||||
}
|
}
|
||||||
|
135
tests/components/homee/fixtures/homee.json
Normal file
135
tests/components/homee/fixtures/homee.json
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
{
|
||||||
|
"id": -1,
|
||||||
|
"name": "homee",
|
||||||
|
"profile": 1,
|
||||||
|
"image": "default",
|
||||||
|
"favorite": 0,
|
||||||
|
"order": 0,
|
||||||
|
"protocol": 0,
|
||||||
|
"routing": 0,
|
||||||
|
"state": 1,
|
||||||
|
"state_changed": 16,
|
||||||
|
"added": 16,
|
||||||
|
"history": 1,
|
||||||
|
"cube_type": 0,
|
||||||
|
"note": "",
|
||||||
|
"services": 0,
|
||||||
|
"phonetic_name": "",
|
||||||
|
"owner": 0,
|
||||||
|
"security": 0,
|
||||||
|
"attributes": [
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"node_id": -1,
|
||||||
|
"instance": 0,
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 200,
|
||||||
|
"current_value": 0.0,
|
||||||
|
"target_value": 0.0,
|
||||||
|
"last_value": 2.0,
|
||||||
|
"unit": "n/a",
|
||||||
|
"step_value": 1.0,
|
||||||
|
"editable": 1,
|
||||||
|
"type": 205,
|
||||||
|
"state": 1,
|
||||||
|
"last_changed": 1735815716,
|
||||||
|
"changed_by": 2,
|
||||||
|
"changed_by_id": 4,
|
||||||
|
"based_on": 1,
|
||||||
|
"data": "",
|
||||||
|
"name": "",
|
||||||
|
"options": {
|
||||||
|
"history": {
|
||||||
|
"day": 182,
|
||||||
|
"week": 26,
|
||||||
|
"month": 6,
|
||||||
|
"stepped": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 18,
|
||||||
|
"node_id": -1,
|
||||||
|
"instance": 0,
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 100,
|
||||||
|
"current_value": 15.0,
|
||||||
|
"target_value": 15.0,
|
||||||
|
"last_value": 15.0,
|
||||||
|
"unit": "%",
|
||||||
|
"step_value": 0.1,
|
||||||
|
"editable": 0,
|
||||||
|
"type": 311,
|
||||||
|
"state": 1,
|
||||||
|
"last_changed": 1739390161,
|
||||||
|
"changed_by": 1,
|
||||||
|
"changed_by_id": 0,
|
||||||
|
"based_on": 1,
|
||||||
|
"data": "",
|
||||||
|
"name": "",
|
||||||
|
"options": {
|
||||||
|
"history": {
|
||||||
|
"day": 1,
|
||||||
|
"week": 26,
|
||||||
|
"month": 6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 19,
|
||||||
|
"node_id": -1,
|
||||||
|
"instance": 0,
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 100,
|
||||||
|
"current_value": 5.0,
|
||||||
|
"target_value": 5.0,
|
||||||
|
"last_value": 10.0,
|
||||||
|
"unit": "%",
|
||||||
|
"step_value": 0.1,
|
||||||
|
"editable": 0,
|
||||||
|
"type": 312,
|
||||||
|
"state": 1,
|
||||||
|
"last_changed": 1739390161,
|
||||||
|
"changed_by": 1,
|
||||||
|
"changed_by_id": 0,
|
||||||
|
"based_on": 1,
|
||||||
|
"data": "",
|
||||||
|
"name": "",
|
||||||
|
"options": {
|
||||||
|
"history": {
|
||||||
|
"day": 1,
|
||||||
|
"week": 26,
|
||||||
|
"month": 6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 20,
|
||||||
|
"node_id": -1,
|
||||||
|
"instance": 0,
|
||||||
|
"minimum": 0,
|
||||||
|
"maximum": 100,
|
||||||
|
"current_value": 10.0,
|
||||||
|
"target_value": 10.0,
|
||||||
|
"last_value": 10.0,
|
||||||
|
"unit": "%",
|
||||||
|
"step_value": 0.1,
|
||||||
|
"editable": 0,
|
||||||
|
"type": 313,
|
||||||
|
"state": 1,
|
||||||
|
"last_changed": 1739390161,
|
||||||
|
"changed_by": 1,
|
||||||
|
"changed_by_id": 0,
|
||||||
|
"based_on": 1,
|
||||||
|
"data": "",
|
||||||
|
"name": "",
|
||||||
|
"options": {
|
||||||
|
"history": {
|
||||||
|
"day": 1,
|
||||||
|
"week": 26,
|
||||||
|
"month": 6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
# serializer version: 1
|
||||||
|
# name: test_alarm_control_panel_snapshot[alarm_control_panel.testhomee_status-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'alarm_control_panel',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'alarm_control_panel.testhomee_status',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Status',
|
||||||
|
'platform': 'homee',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': <AlarmControlPanelEntityFeature: 39>,
|
||||||
|
'translation_key': 'homee_mode',
|
||||||
|
'unique_id': '00055511EECC--1-1',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_alarm_control_panel_snapshot[alarm_control_panel.testhomee_status-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'changed_by': 'user - 4',
|
||||||
|
'code_arm_required': False,
|
||||||
|
'code_format': None,
|
||||||
|
'friendly_name': 'TestHomee Status',
|
||||||
|
'supported_features': <AlarmControlPanelEntityFeature: 39>,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'alarm_control_panel.testhomee_status',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'armed_home',
|
||||||
|
})
|
||||||
|
# ---
|
96
tests/components/homee/test_alarm_control_panel.py
Normal file
96
tests/components/homee/test_alarm_control_panel.py
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
"""Test Homee alarm control panels."""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
|
from homeassistant.components.alarm_control_panel import (
|
||||||
|
DOMAIN as ALARM_CONTROL_PANEL_DOMAIN,
|
||||||
|
SERVICE_ALARM_ARM_AWAY,
|
||||||
|
SERVICE_ALARM_ARM_HOME,
|
||||||
|
SERVICE_ALARM_ARM_NIGHT,
|
||||||
|
SERVICE_ALARM_ARM_VACATION,
|
||||||
|
SERVICE_ALARM_DISARM,
|
||||||
|
)
|
||||||
|
from homeassistant.components.homee.const import DOMAIN
|
||||||
|
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
|
from . import build_mock_node, setup_integration
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry, snapshot_platform
|
||||||
|
|
||||||
|
|
||||||
|
async def setup_alarm_control_panel(
|
||||||
|
hass: HomeAssistant, mock_homee: MagicMock, mock_config_entry: MockConfigEntry
|
||||||
|
) -> None:
|
||||||
|
"""Setups the integration for select tests."""
|
||||||
|
mock_homee.nodes = [build_mock_node("homee.json")]
|
||||||
|
mock_homee.get_node_by_id.return_value = mock_homee.nodes[0]
|
||||||
|
await setup_integration(hass, mock_config_entry)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("service", "state"),
|
||||||
|
[
|
||||||
|
(SERVICE_ALARM_ARM_HOME, 0),
|
||||||
|
(SERVICE_ALARM_ARM_NIGHT, 1),
|
||||||
|
(SERVICE_ALARM_ARM_AWAY, 2),
|
||||||
|
(SERVICE_ALARM_ARM_VACATION, 3),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_alarm_control_panel_services(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_homee: MagicMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
service: str,
|
||||||
|
state: int,
|
||||||
|
) -> None:
|
||||||
|
"""Test alarm control panel services."""
|
||||||
|
await setup_alarm_control_panel(hass, mock_homee, mock_config_entry)
|
||||||
|
|
||||||
|
await hass.services.async_call(
|
||||||
|
ALARM_CONTROL_PANEL_DOMAIN,
|
||||||
|
service,
|
||||||
|
{ATTR_ENTITY_ID: "alarm_control_panel.testhomee_status"},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
mock_homee.set_value.assert_called_once_with(-1, 1, state)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_alarm_control_panel_service_disarm_error(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_homee: MagicMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test that disarm service calls no action."""
|
||||||
|
await setup_alarm_control_panel(hass, mock_homee, mock_config_entry)
|
||||||
|
|
||||||
|
with pytest.raises(ServiceValidationError) as exc_info:
|
||||||
|
await hass.services.async_call(
|
||||||
|
ALARM_CONTROL_PANEL_DOMAIN,
|
||||||
|
SERVICE_ALARM_DISARM,
|
||||||
|
{ATTR_ENTITY_ID: "alarm_control_panel.testhomee_status"},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
assert exc_info.value.translation_domain == DOMAIN
|
||||||
|
assert exc_info.value.translation_key == "disarm_not_supported"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_alarm_control_panel_snapshot(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_homee: MagicMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
) -> None:
|
||||||
|
"""Test the alarm-control_panel snapshots."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.homee.PLATFORMS", [Platform.ALARM_CONTROL_PANEL]
|
||||||
|
):
|
||||||
|
await setup_alarm_control_panel(hass, mock_homee, mock_config_entry)
|
||||||
|
|
||||||
|
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
Loading…
x
Reference in New Issue
Block a user