mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Convert template alarm_control_panel to use async_track_template_result (#39014)
* Convert template lock to use async_track_template_result * Convert template switch to use async_track_template_result * Convert template fan to use async_track_template_result * Convert template alarm_control_panel to use async_track_template_result
This commit is contained in:
parent
f3d077c1e1
commit
8813f669c2
@ -19,8 +19,6 @@ from homeassistant.const import (
|
|||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_UNIQUE_ID,
|
CONF_UNIQUE_ID,
|
||||||
CONF_VALUE_TEMPLATE,
|
CONF_VALUE_TEMPLATE,
|
||||||
EVENT_HOMEASSISTANT_START,
|
|
||||||
MATCH_ALL,
|
|
||||||
STATE_ALARM_ARMED_AWAY,
|
STATE_ALARM_ARMED_AWAY,
|
||||||
STATE_ALARM_ARMED_HOME,
|
STATE_ALARM_ARMED_HOME,
|
||||||
STATE_ALARM_ARMED_NIGHT,
|
STATE_ALARM_ARMED_NIGHT,
|
||||||
@ -33,9 +31,10 @@ from homeassistant.core import callback
|
|||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import async_generate_entity_id
|
from homeassistant.helpers.entity import async_generate_entity_id
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
|
||||||
from homeassistant.helpers.script import Script
|
from homeassistant.helpers.script import Script
|
||||||
|
|
||||||
|
from .template_entity import TemplateEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
_VALID_STATES = [
|
_VALID_STATES = [
|
||||||
STATE_ALARM_ARMED_AWAY,
|
STATE_ALARM_ARMED_AWAY,
|
||||||
@ -90,18 +89,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
code_arm_required = device_config[CONF_CODE_ARM_REQUIRED]
|
code_arm_required = device_config[CONF_CODE_ARM_REQUIRED]
|
||||||
unique_id = device_config.get(CONF_UNIQUE_ID)
|
unique_id = device_config.get(CONF_UNIQUE_ID)
|
||||||
|
|
||||||
template_entity_ids = set()
|
|
||||||
|
|
||||||
if state_template is not None:
|
|
||||||
temp_ids = state_template.extract_entities()
|
|
||||||
if str(temp_ids) != MATCH_ALL:
|
|
||||||
template_entity_ids |= set(temp_ids)
|
|
||||||
else:
|
|
||||||
_LOGGER.warning("No value template - will use optimistic state")
|
|
||||||
|
|
||||||
if not template_entity_ids:
|
|
||||||
template_entity_ids = MATCH_ALL
|
|
||||||
|
|
||||||
alarm_control_panels.append(
|
alarm_control_panels.append(
|
||||||
AlarmControlPanelTemplate(
|
AlarmControlPanelTemplate(
|
||||||
hass,
|
hass,
|
||||||
@ -113,7 +100,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
arm_home_action,
|
arm_home_action,
|
||||||
arm_night_action,
|
arm_night_action,
|
||||||
code_arm_required,
|
code_arm_required,
|
||||||
template_entity_ids,
|
|
||||||
unique_id,
|
unique_id,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -121,7 +107,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
async_add_entities(alarm_control_panels)
|
async_add_entities(alarm_control_panels)
|
||||||
|
|
||||||
|
|
||||||
class AlarmControlPanelTemplate(AlarmControlPanelEntity):
|
class AlarmControlPanelTemplate(TemplateEntity, AlarmControlPanelEntity):
|
||||||
"""Representation of a templated Alarm Control Panel."""
|
"""Representation of a templated Alarm Control Panel."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -135,11 +121,10 @@ class AlarmControlPanelTemplate(AlarmControlPanelEntity):
|
|||||||
arm_home_action,
|
arm_home_action,
|
||||||
arm_night_action,
|
arm_night_action,
|
||||||
code_arm_required,
|
code_arm_required,
|
||||||
template_entity_ids,
|
|
||||||
unique_id,
|
unique_id,
|
||||||
):
|
):
|
||||||
"""Initialize the panel."""
|
"""Initialize the panel."""
|
||||||
self.hass = hass
|
super().__init__()
|
||||||
self.entity_id = async_generate_entity_id(
|
self.entity_id = async_generate_entity_id(
|
||||||
ENTITY_ID_FORMAT, device_id, hass=hass
|
ENTITY_ID_FORMAT, device_id, hass=hass
|
||||||
)
|
)
|
||||||
@ -161,12 +146,8 @@ class AlarmControlPanelTemplate(AlarmControlPanelEntity):
|
|||||||
self._arm_night_script = Script(hass, arm_night_action, name, domain)
|
self._arm_night_script = Script(hass, arm_night_action, name, domain)
|
||||||
|
|
||||||
self._state = None
|
self._state = None
|
||||||
self._entities = template_entity_ids
|
|
||||||
self._unique_id = unique_id
|
self._unique_id = unique_id
|
||||||
|
|
||||||
if self._template is not None:
|
|
||||||
self._template.hass = self.hass
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the display name of this alarm control panel."""
|
"""Return the display name of this alarm control panel."""
|
||||||
@ -177,11 +158,6 @@ class AlarmControlPanelTemplate(AlarmControlPanelEntity):
|
|||||||
"""Return the unique id of this alarm control panel."""
|
"""Return the unique id of this alarm control panel."""
|
||||||
return self._unique_id
|
return self._unique_id
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""Return the polling state."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
@ -212,28 +188,32 @@ class AlarmControlPanelTemplate(AlarmControlPanelEntity):
|
|||||||
"""Whether the code is required for arm actions."""
|
"""Whether the code is required for arm actions."""
|
||||||
return self._code_arm_required
|
return self._code_arm_required
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _update_state(self, state):
|
||||||
|
if isinstance(state, TemplateError):
|
||||||
|
self._state = None
|
||||||
|
return
|
||||||
|
|
||||||
|
# Validate state
|
||||||
|
if state in _VALID_STATES:
|
||||||
|
self._state = state
|
||||||
|
_LOGGER.debug("Valid state - %s", state)
|
||||||
|
return
|
||||||
|
|
||||||
|
_LOGGER.error(
|
||||||
|
"Received invalid alarm panel state: %s. Expected: %s",
|
||||||
|
state,
|
||||||
|
", ".join(_VALID_STATES),
|
||||||
|
)
|
||||||
|
self._state = None
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
|
if self._template:
|
||||||
@callback
|
self.add_template_attribute(
|
||||||
def template_alarm_state_listener(event):
|
"_state", self._template, None, self._update_state
|
||||||
"""Handle target device state changes."""
|
)
|
||||||
self.async_schedule_update_ha_state(True)
|
await super().async_added_to_hass()
|
||||||
|
|
||||||
@callback
|
|
||||||
def template_alarm_control_panel_startup(event):
|
|
||||||
"""Update template on startup."""
|
|
||||||
if self._template is not None and self._entities != MATCH_ALL:
|
|
||||||
# Track state change only for valid templates
|
|
||||||
async_track_state_change_event(
|
|
||||||
self.hass, self._entities, template_alarm_state_listener
|
|
||||||
)
|
|
||||||
|
|
||||||
self.async_schedule_update_ha_state(True)
|
|
||||||
|
|
||||||
self.hass.bus.async_listen_once(
|
|
||||||
EVENT_HOMEASSISTANT_START, template_alarm_control_panel_startup
|
|
||||||
)
|
|
||||||
|
|
||||||
async def _async_alarm_arm(self, state, script=None, code=None):
|
async def _async_alarm_arm(self, state, script=None, code=None):
|
||||||
"""Arm the panel to specified state with supplied script."""
|
"""Arm the panel to specified state with supplied script."""
|
||||||
@ -274,25 +254,3 @@ class AlarmControlPanelTemplate(AlarmControlPanelEntity):
|
|||||||
await self._async_alarm_arm(
|
await self._async_alarm_arm(
|
||||||
STATE_ALARM_DISARMED, script=self._disarm_script, code=code
|
STATE_ALARM_DISARMED, script=self._disarm_script, code=code
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""Update the state from the template."""
|
|
||||||
if self._template is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
state = self._template.async_render().lower()
|
|
||||||
except TemplateError as ex:
|
|
||||||
_LOGGER.error(ex)
|
|
||||||
self._state = None
|
|
||||||
|
|
||||||
if state in _VALID_STATES:
|
|
||||||
self._state = state
|
|
||||||
_LOGGER.debug("Valid state - %s", state)
|
|
||||||
else:
|
|
||||||
_LOGGER.error(
|
|
||||||
"Received invalid alarm panel state: %s. Expected: %s",
|
|
||||||
state,
|
|
||||||
", ".join(_VALID_STATES),
|
|
||||||
)
|
|
||||||
self._state = None
|
|
||||||
|
@ -148,6 +148,7 @@ async def test_optimistic_states(hass):
|
|||||||
await common.async_alarm_arm_away(
|
await common.async_alarm_arm_away(
|
||||||
hass, entity_id="alarm_control_panel.test_template_panel"
|
hass, entity_id="alarm_control_panel.test_template_panel"
|
||||||
)
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get("alarm_control_panel.test_template_panel")
|
state = hass.states.get("alarm_control_panel.test_template_panel")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert state.state == STATE_ALARM_ARMED_AWAY
|
assert state.state == STATE_ALARM_ARMED_AWAY
|
||||||
@ -201,6 +202,7 @@ async def test_no_action_scripts(hass):
|
|||||||
await common.async_alarm_arm_away(
|
await common.async_alarm_arm_away(
|
||||||
hass, entity_id="alarm_control_panel.test_template_panel"
|
hass, entity_id="alarm_control_panel.test_template_panel"
|
||||||
)
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get("alarm_control_panel.test_template_panel")
|
state = hass.states.get("alarm_control_panel.test_template_panel")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert state.state == STATE_ALARM_ARMED_AWAY
|
assert state.state == STATE_ALARM_ARMED_AWAY
|
||||||
@ -208,6 +210,7 @@ async def test_no_action_scripts(hass):
|
|||||||
await common.async_alarm_arm_home(
|
await common.async_alarm_arm_home(
|
||||||
hass, entity_id="alarm_control_panel.test_template_panel"
|
hass, entity_id="alarm_control_panel.test_template_panel"
|
||||||
)
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get("alarm_control_panel.test_template_panel")
|
state = hass.states.get("alarm_control_panel.test_template_panel")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert state.state == STATE_ALARM_ARMED_AWAY
|
assert state.state == STATE_ALARM_ARMED_AWAY
|
||||||
@ -215,6 +218,7 @@ async def test_no_action_scripts(hass):
|
|||||||
await common.async_alarm_arm_night(
|
await common.async_alarm_arm_night(
|
||||||
hass, entity_id="alarm_control_panel.test_template_panel"
|
hass, entity_id="alarm_control_panel.test_template_panel"
|
||||||
)
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get("alarm_control_panel.test_template_panel")
|
state = hass.states.get("alarm_control_panel.test_template_panel")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert state.state == STATE_ALARM_ARMED_AWAY
|
assert state.state == STATE_ALARM_ARMED_AWAY
|
||||||
@ -222,6 +226,7 @@ async def test_no_action_scripts(hass):
|
|||||||
await common.async_alarm_disarm(
|
await common.async_alarm_disarm(
|
||||||
hass, entity_id="alarm_control_panel.test_template_panel"
|
hass, entity_id="alarm_control_panel.test_template_panel"
|
||||||
)
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get("alarm_control_panel.test_template_panel")
|
state = hass.states.get("alarm_control_panel.test_template_panel")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert state.state == STATE_ALARM_ARMED_AWAY
|
assert state.state == STATE_ALARM_ARMED_AWAY
|
||||||
|
Loading…
x
Reference in New Issue
Block a user