Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the active sensors to the sensor attributes #1045

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions custom_components/alarmo/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def __init__(self, hass: HomeAssistant, name: str, entity_id: str) -> None:
self._changed_by = None
self._open_sensors = {}
self._bypassed_sensors = []
self.active_sensors = []
self._delay = None
self.expiration = None
self.area_id = None
Expand Down Expand Up @@ -257,6 +258,7 @@ def extra_state_attributes(self):
"next_state": self.next_state,
"open_sensors": self.open_sensors,
"bypassed_sensors": self.bypassed_sensors,
"active_sensors": self.active_sensors,
"delay": self.delay,
}

Expand Down Expand Up @@ -352,6 +354,7 @@ def alarm_disarm(self, code, **kwargs):
else:
self.open_sensors = None
self.bypassed_sensors = None
self.active_sensors = None
self.async_update_state(STATE_ALARM_DISARMED)
if self.changed_by:
_LOGGER.info("Alarm '{}' is disarmed by {}.".format(self.name, self.changed_by))
Expand Down Expand Up @@ -438,6 +441,7 @@ def async_handle_arm_request(self, arm_mode, **kwargs):
self._revert_state = STATE_ALARM_DISARMED
self.open_sensors = None
self.bypassed_sensors = None
self.active_sensors = None

self.async_arm(
arm_mode,
Expand Down Expand Up @@ -504,6 +508,8 @@ async def async_added_to_hass(self):
self.open_sensors = state.attributes["open_sensors"]
if "bypassed_sensors" in state.attributes:
self._bypassed_sensors = state.attributes["bypassed_sensors"]
if "actve_sensors" in state.attributes:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo that'll come back to bite you ;) (just passing by)

self.active_sensors = state.attributes["active_sensors"]

async def async_will_remove_from_hass(self):
await super().async_will_remove_from_hass()
Expand Down Expand Up @@ -624,6 +630,7 @@ def async_update_state(self, state: str = None):
def async_arm_failure(self, open_sensors: dict, context_id=None):
"""handle arm failure."""
self._open_sensors = open_sensors
self.active_sensors = None
command = self._arm_mode.replace("armed", "arm")

if self._state != self._revert_state and self._revert_state:
Expand Down Expand Up @@ -662,11 +669,14 @@ def async_arm(self, arm_mode, **kwargs):
if skip_delay or not exit_delay:
# immediate arm event

(open_sensors, bypassed_sensors) = self.hass.data[const.DOMAIN]["sensor_handler"].validate_arming_event(
(open_sensors, bypassed_sensors, active_sensors) = self.hass.data[const.DOMAIN]["sensor_handler"].validate_arming_event(
area_id=self.area_id,
target_state=arm_mode,
bypass_open_sensors=bypass_open_sensors
)

if active_sensors:
self.active_sensors = active_sensors

if open_sensors:
# there where errors -> abort the arm
Expand Down Expand Up @@ -701,7 +711,7 @@ def async_arm(self, arm_mode, **kwargs):

else: # normal arm event (from disarmed via arming)

(open_sensors, _bypassed_sensors) = self.hass.data[const.DOMAIN]["sensor_handler"].validate_arming_event(
(open_sensors, _bypassed_sensors, _active_sensors) = self.hass.data[const.DOMAIN]["sensor_handler"].validate_arming_event(
area_id=self.area_id,
target_state=arm_mode,
use_delay=True,
Expand Down Expand Up @@ -790,6 +800,7 @@ def async_trigger_timer_finished(now):
self.async_clear_timer()
if self._config[const.ATTR_DISARM_AFTER_TRIGGER] or not self.arm_mode:
self.bypassed_sensors = None
self.active_sensors = None
self.async_update_state(STATE_ALARM_DISARMED)
else:
self.open_sensors = None
Expand Down
13 changes: 6 additions & 7 deletions custom_components/alarmo/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
STATE_CLOSED,
STATE_ON,
STATE_OFF,
STATE_LOCKED,
STATE_UNLOCKED,
STATE_ALARM_PENDING,
STATE_ALARM_ARMING,
STATE_ALARM_TRIGGERED,
Expand All @@ -33,9 +35,6 @@
ATTR_NAME,
)


from homeassistant.components.lock import LockState

from . import const

ATTR_USE_EXIT_DELAY = "use_exit_delay"
Expand All @@ -53,8 +52,8 @@
ATTR_ENTITIES = "entities"
ATTR_NEW_ENTITY_ID = "new_entity_id"

SENSOR_STATES_OPEN = [STATE_ON, STATE_OPEN, LockState.UNLOCKED]
SENSOR_STATES_CLOSED = [STATE_OFF, STATE_CLOSED, LockState.LOCKED]
SENSOR_STATES_OPEN = [STATE_ON, STATE_OPEN, STATE_UNLOCKED]
SENSOR_STATES_CLOSED = [STATE_OFF, STATE_CLOSED, STATE_LOCKED]


SENSOR_TYPE_DOOR = "door"
Expand Down Expand Up @@ -264,7 +263,7 @@ def validate_arming_event(self, area_id: str, target_state: str = None, **kwargs
else:
open_sensors[entity] = sensor_state

return (open_sensors, bypassed_sensors)
return (open_sensors, bypassed_sensors, sensors_list)

@callback
def async_sensor_state_changed(self, event):
Expand Down Expand Up @@ -426,7 +425,7 @@ def update_ready_to_arm_status(self, area_id):
arm_modes.remove(alarm_entity.arm_mode)

def arm_mode_is_ready(mode):
(blocking_sensors, _bypassed_sensors) = self.validate_arming_event(area_id, mode)
(blocking_sensors, _bypassed_sensors, _active_sensors) = self.validate_arming_event(area_id, mode)
result = not(len(blocking_sensors))
return result

Expand Down