Migrate esphome alarm_control_panel platform to use _on_static_info_update (#94961)

This commit is contained in:
J. Nick Koston 2023-06-26 15:58:17 -05:00 committed by GitHub
parent 0f08e6699c
commit 7737271a30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@ from aioesphomeapi import (
AlarmControlPanelInfo, AlarmControlPanelInfo,
AlarmControlPanelState, AlarmControlPanelState,
APIIntEnum, APIIntEnum,
EntityInfo,
) )
from homeassistant.components.alarm_control_panel import ( from homeassistant.components.alarm_control_panel import (
@ -27,7 +28,7 @@ from homeassistant.const import (
STATE_ALARM_PENDING, STATE_ALARM_PENDING,
STATE_ALARM_TRIGGERED, STATE_ALARM_TRIGGERED,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .entity import ( from .entity import (
@ -85,14 +86,11 @@ class EsphomeAlarmControlPanel(
): ):
"""An Alarm Control Panel implementation for ESPHome.""" """An Alarm Control Panel implementation for ESPHome."""
@property @callback
def state(self) -> str | None: def _on_static_info_update(self, static_info: EntityInfo) -> None:
"""Return the state of the device.""" """Set attrs from static info."""
return _ESPHOME_ACP_STATE_TO_HASS_STATE.from_esphome(self._state.state) super()._on_static_info_update(static_info)
static_info = self._static_info
@property
def supported_features(self) -> AlarmControlPanelEntityFeature:
"""Return the list of supported features."""
feature = 0 feature = 0
if self._static_info.supported_features & EspHomeACPFeatures.ARM_HOME: if self._static_info.supported_features & EspHomeACPFeatures.ARM_HOME:
feature |= AlarmControlPanelEntityFeature.ARM_HOME feature |= AlarmControlPanelEntityFeature.ARM_HOME
@ -106,58 +104,55 @@ class EsphomeAlarmControlPanel(
feature |= AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS feature |= AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS
if self._static_info.supported_features & EspHomeACPFeatures.ARM_VACATION: if self._static_info.supported_features & EspHomeACPFeatures.ARM_VACATION:
feature |= AlarmControlPanelEntityFeature.ARM_VACATION feature |= AlarmControlPanelEntityFeature.ARM_VACATION
return AlarmControlPanelEntityFeature(feature) self._attr_supported_features = AlarmControlPanelEntityFeature(feature)
self._attr_code_format = (
CodeFormat.NUMBER if static_info.requires_code else None
)
self._attr_code_arm_required = bool(static_info.requires_code_to_arm)
@property @property
def code_format(self) -> CodeFormat | None: def state(self) -> str | None:
"""Return code format for disarm.""" """Return the state of the device."""
if self._static_info.requires_code: return _ESPHOME_ACP_STATE_TO_HASS_STATE.from_esphome(self._state.state)
return CodeFormat.NUMBER
return None
@property
def code_arm_required(self) -> bool:
"""Whether the code is required for arm actions."""
return bool(self._static_info.requires_code_to_arm)
async def async_alarm_disarm(self, code: str | None = None) -> None: async def async_alarm_disarm(self, code: str | None = None) -> None:
"""Send disarm command.""" """Send disarm command."""
await self._client.alarm_control_panel_command( await self._client.alarm_control_panel_command(
self._static_info.key, AlarmControlPanelCommand.DISARM, code self._key, AlarmControlPanelCommand.DISARM, code
) )
async def async_alarm_arm_home(self, code: str | None = None) -> None: async def async_alarm_arm_home(self, code: str | None = None) -> None:
"""Send arm home command.""" """Send arm home command."""
await self._client.alarm_control_panel_command( await self._client.alarm_control_panel_command(
self._static_info.key, AlarmControlPanelCommand.ARM_HOME, code self._key, AlarmControlPanelCommand.ARM_HOME, code
) )
async def async_alarm_arm_away(self, code: str | None = None) -> None: async def async_alarm_arm_away(self, code: str | None = None) -> None:
"""Send arm away command.""" """Send arm away command."""
await self._client.alarm_control_panel_command( await self._client.alarm_control_panel_command(
self._static_info.key, AlarmControlPanelCommand.ARM_AWAY, code self._key, AlarmControlPanelCommand.ARM_AWAY, code
) )
async def async_alarm_arm_night(self, code: str | None = None) -> None: async def async_alarm_arm_night(self, code: str | None = None) -> None:
"""Send arm away command.""" """Send arm away command."""
await self._client.alarm_control_panel_command( await self._client.alarm_control_panel_command(
self._static_info.key, AlarmControlPanelCommand.ARM_NIGHT, code self._key, AlarmControlPanelCommand.ARM_NIGHT, code
) )
async def async_alarm_arm_custom_bypass(self, code: str | None = None) -> None: async def async_alarm_arm_custom_bypass(self, code: str | None = None) -> None:
"""Send arm away command.""" """Send arm away command."""
await self._client.alarm_control_panel_command( await self._client.alarm_control_panel_command(
self._static_info.key, AlarmControlPanelCommand.ARM_CUSTOM_BYPASS, code self._key, AlarmControlPanelCommand.ARM_CUSTOM_BYPASS, code
) )
async def async_alarm_arm_vacation(self, code: str | None = None) -> None: async def async_alarm_arm_vacation(self, code: str | None = None) -> None:
"""Send arm away command.""" """Send arm away command."""
await self._client.alarm_control_panel_command( await self._client.alarm_control_panel_command(
self._static_info.key, AlarmControlPanelCommand.ARM_VACATION, code self._key, AlarmControlPanelCommand.ARM_VACATION, code
) )
async def async_alarm_trigger(self, code: str | None = None) -> None: async def async_alarm_trigger(self, code: str | None = None) -> None:
"""Send alarm trigger command.""" """Send alarm trigger command."""
await self._client.alarm_control_panel_command( await self._client.alarm_control_panel_command(
self._static_info.key, AlarmControlPanelCommand.TRIGGER, code self._key, AlarmControlPanelCommand.TRIGGER, code
) )