Skip to content

Commit

Permalink
add 'next_state' property
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsfaber committed Jul 3, 2024
1 parent ef3ac41 commit 79152fc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,14 @@ The Alarmo entity follows the state definitions as defined by HA:

The Alarmo entity defines the following attributes:

| Attribute | Default value | Example value when set | Description |
| ------------------ | ------------- | ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `arm_mode` | `null` | `armed_away` | The current active arm mode.<br> Defined in all states except `disarmed`. |
| `changed_by` | `null` | `Niels` | User who last armed or disarmed the alarm.<br> Detected from the entered code.<br> Cleared when alarm is armed or disarmed without a code. |
| `open_sensors` | `null` | `{binary_sensor.backdoor: on}` | Dictionary of sensors with their entity-ID + state, that caused the alarm to change state.<br>Set when arming attempt failed (due to one or more sensors).<br>Set when alarm is triggered (only first sensor that caused the triggering is stored). |
| `bypassed_sensors` | `null` | `[binary_sensor.backdoor]` | List of sensors that are temporarily excluded from the alarm, due to arming in force. |
| `delay` | `null` | 30 | Duration of the exit or entry delay. Only set in the `arming` and `pending` states, `null` otherwise. |
| Attribute | Default value | Example value when set | Description |
| ------------------ | ----------------------- | ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `arm_mode` | `null` | `armed_away` | The current active arm mode.<br> Defined in all states except `disarmed`. |
| `next_state` | (same as current state) | `armed_away` | When alarm is in state `arming`: `next_state` is equal to the `arm_mode`.<br>When alarm is in state `pending`: `next_state` is `triggered`. <br>When alarm is in state `triggered`: `next_state` is `triggered` (if no trigger time is configured), `disarmed` (if 'disarm after triggering' setting is enabled) or equal to the `arm_mode` (otherwise).<br>In other cases, the `next_state` will be equal to the current state. |
| `changed_by` | `null` | `Niels` | User who last armed or disarmed the alarm.<br> Detected from the entered code.<br> Cleared when alarm is armed or disarmed without a code. |
| `open_sensors` | `null` | `{binary_sensor.backdoor: on}` | Dictionary of sensors with their entity-ID + state, that caused the alarm to change state.<br>Set when arming attempt failed (due to one or more sensors).<br>Set when alarm is triggered (only first sensor that caused the triggering is stored). |
| `bypassed_sensors` | `null` | `[binary_sensor.backdoor]` | List of sensors that are temporarily excluded from the alarm, due to arming in force. |
| `delay` | `null` | 30 | Duration of the exit or entry delay. Only set in the `arming` and `pending` states, `null` otherwise. |

#### Commands
The Alarmo entities support the following commands:
Expand Down Expand Up @@ -289,12 +290,12 @@ When assigning sensors to Alarmo, the type of the sensor is automatically determ
Note that assigning a sensor type is not mandatory, and all configuration settings can also be set manually. It is also possible to deviate from the predefined configuration after setting a type.

The following table defines the sensor types and the predefined configuration:
| Type | Device classes | Arm modes | Enabled configuration options |
| ------------- | ----------------------------------------------------- | ------------------------------------------------------------------ | ----------------------------------------- |
| Door | `door`<br> `garage_door`<br>`lock`<br> `opening ` | `Armed Away`<br> `Armed Home`<br>`Armed Night`<br>`Armed Vacation` | `Arm after closing`<br> `Use entry delay` |
| Window | `window` | `Armed Away`<br>`Armed Home`<br>`Armed Night`<br>`Armed Vacation` | - |
| Motion | `motion`<br>`moving`<br>`occupancy`<br>`presence` | `Armed Away`<br>`Armed Vacation` | `Use exit delay`<br>`Use entry delay` |
| Tamper | `tamper`<br>`sound`<br>`vibration` | `Armed Away`<br>`Armed Home`<br>`Armed Night`<br>`Armed Vacation` | - |
| Type | Device classes | Arm modes | Enabled configuration options |
| ------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------ | ----------------------------------------- |
| Door | `door`<br> `garage_door`<br>`lock`<br> `opening ` | `Armed Away`<br> `Armed Home`<br>`Armed Night`<br>`Armed Vacation` | `Arm after closing`<br> `Use entry delay` |
| Window | `window` | `Armed Away`<br>`Armed Home`<br>`Armed Night`<br>`Armed Vacation` | - |
| Motion | `motion`<br>`moving`<br>`occupancy`<br>`presence` | `Armed Away`<br>`Armed Vacation` | `Use exit delay`<br>`Use entry delay` |
| Tamper | `tamper`<br>`sound`<br>`vibration` | `Armed Away`<br>`Armed Home`<br>`Armed Night`<br>`Armed Vacation` | - |
| Environmental | `carbon_monoxide`<br>`gas`<br> `heat`<br>`moisture`<br>`smoke`<br>`safety` | N/A | `Always on` |


Expand Down Expand Up @@ -499,12 +500,12 @@ Example:

The following actions are defined in Alarmo and can be used in actionable notifications:

| action | Description | Suitable events |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- |
| `ALARMO_RETRY_ARM` | Repeats the command that failed before.<br>Will only succeed in case the issue blocking the arming before has been restored. | Failed to arm |
| `ALARMO_FORCE_ARM` | Repeats the command that failed before.<br>The sensor/sensors that failed, shall be ignored (bypassed) while the alarm is armed. | Failed to arm |
| `ALARMO_DISARM` | Disarm the alarm. | Armed, Leave, Entry, Triggered |
| `ALARMO_ARM_AWAY`<br> `ALARMO_ARM_HOME`<br> `ALARMO_ARM_NIGHT`<br> `ALARMO_ARM_VACATION`<br> `ALARMO_ARM_CUSTOM_BYPASS` | Arm the alarm in the corresponding mode. | None (intended for automations outside of alarmo). |
| action | Description | Suitable events |
| ----------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------- |
| `ALARMO_RETRY_ARM` | Repeats the command that failed before.<br>Will only succeed in case the issue blocking the arming before has been restored. | Failed to arm |
| `ALARMO_FORCE_ARM` | Repeats the command that failed before.<br>The sensor/sensors that failed, shall be ignored (bypassed) while the alarm is armed. | Failed to arm |
| `ALARMO_DISARM` | Disarm the alarm. | Armed, Leave, Entry, Triggered |
| `ALARMO_ARM_AWAY`<br> `ALARMO_ARM_HOME`<br> `ALARMO_ARM_NIGHT`<br> `ALARMO_ARM_VACATION`<br> `ALARMO_ARM_CUSTOM_BYPASS` | Arm the alarm in the corresponding mode. | None (intended for automations outside of alarmo). |
**Set up notification with actions**

In the Alarmo notifications editor, create a notification and select an event.
Expand Down
38 changes: 38 additions & 0 deletions custom_components/alarmo/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def extra_state_attributes(self):

return {
"arm_mode": self.arm_mode,
"next_state": self.next_state,
"open_sensors": self.open_sensors,
"bypassed_sensors": self.bypassed_sensors,
"delay": self.delay,
Expand Down Expand Up @@ -535,6 +536,27 @@ def supported_features(self) -> int:

return supported_features

@property
def next_state(self):
"""Return the state after transition (countdown) state."""
next_state = self.state
if self._state == STATE_ALARM_ARMING:
next_state = self.arm_mode
elif self._state == STATE_ALARM_PENDING:
next_state = STATE_ALARM_TRIGGERED
elif self._state == STATE_ALARM_TRIGGERED:
if (
not self._config
or not self._arm_mode
or not self._config[const.ATTR_MODES][self._arm_mode]["trigger_time"]
):
next_state = STATE_ALARM_TRIGGERED
elif self._config[const.ATTR_DISARM_AFTER_TRIGGER] or not self.arm_mode:
next_state = STATE_ALARM_DISARMED
else:
next_state = self.arm_mode
return next_state

async def async_added_to_hass(self):
"""Connect to dispatcher listening for entity data notifications."""
await super().async_added_to_hass()
Expand Down Expand Up @@ -852,6 +874,22 @@ def supported_features(self) -> int:
]
return functools.reduce(operator.and_, supported_features)

@property
def next_state(self):
"""Return the state after transition (countdown) state."""
next_states = list(set([
item.next_state
for item in self.hass.data[const.DOMAIN]["areas"].values()
]))

next_state = self.state
if len(next_states)==1:
next_state = next_states[0]
elif STATE_ALARM_TRIGGERED in next_states:
next_state = STATE_ALARM_TRIGGERED

return next_state

async def async_added_to_hass(self):
"""Connect to dispatcher listening for entity data notifications."""
await super().async_added_to_hass()
Expand Down

0 comments on commit 79152fc

Please sign in to comment.