Skip to content

Commit

Permalink
detector_trigger motion event validation
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-bar committed Sep 11, 2021
1 parent 9356760 commit 250ddb1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.10

- Added `detector_trigger` motion event validation to avoid corrupted data

## 1.1.9

- Missing Binary Motion Sensors due to wrong motion detection parameter [#15](https://github.com/elad-bar/ha-shinobi/issues/15)
Expand Down
74 changes: 46 additions & 28 deletions custom_components/shinobi/managers/event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,46 +57,64 @@ def message_received(self, topic, payload):
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno

_LOGGER.error(f"Failed to message_received, Topic {topic}, Payload: {payload}, Error: {ex}, Line: {line_number}")
_LOGGER.error(
f"Failed to handle received message, Topic {topic}, Payload: {payload}, "
f"Error: {ex}, Line: {line_number}"
)

def _handle_sensor_event(self, topic, payload):
trigger_details = payload.get(TRIGGER_DETAILS, {})
trigger_reason = trigger_details.get(TRIGGER_DETAILS_REASON)
try:
trigger_details = payload.get(TRIGGER_DETAILS, {})
trigger_reason = trigger_details.get(TRIGGER_DETAILS_REASON)

trigger_name = payload.get(TRIGGER_NAME)
trigger_plug = trigger_details.get(TRIGGER_DETAILS_PLUG)
trigger_matrices = trigger_details.get(TRIGGER_DETAILS_MATRICES, [])

if trigger_matrices is None:
_LOGGER.warning(f"Empty trigger matrices, payload: {payload}")
return

trigger_name = payload.get(TRIGGER_NAME)
trigger_plug = trigger_details.get(TRIGGER_DETAILS_PLUG)
trigger_matrices = trigger_details.get(TRIGGER_DETAILS_MATRICES, [])
if trigger_name is None:
trigger_name = trigger_details.get(TRIGGER_NAME)

if trigger_name is None:
trigger_name = trigger_details.get(TRIGGER_NAME)
trigger_tags = []

trigger_tags = []
for trigger_object in trigger_matrices:
if trigger_object is None:
_LOGGER.warning(f"Invalid trigger object, payload: {payload}")

for trigger_object in trigger_matrices:
trigger_tag = trigger_object.get(TRIGGER_DETAILS_MATRICES_TAG)
else:
trigger_tag = trigger_object.get(TRIGGER_DETAILS_MATRICES_TAG)

if trigger_tag not in trigger_tags:
trigger_tags.append(trigger_tag)
if trigger_tag not in trigger_tags:
trigger_tags.append(trigger_tag)

sensor_type = PLUG_SENSOR_TYPE.get(trigger_reason, None)
sensor_type = PLUG_SENSOR_TYPE.get(trigger_reason, None)

value = {
TRIGGER_NAME: trigger_name,
TRIGGER_PLUG: trigger_plug,
TRIGGER_DETAILS_REASON: trigger_reason,
TRIGGER_TAGS: trigger_tags,
TRIGGER_STATE: STATE_ON,
TRIGGER_TIMESTAMP: datetime.now().timestamp(),
TRIGGER_TOPIC: topic
}
value = {
TRIGGER_NAME: trigger_name,
TRIGGER_PLUG: trigger_plug,
TRIGGER_DETAILS_REASON: trigger_reason,
TRIGGER_TAGS: trigger_tags,
TRIGGER_STATE: STATE_ON,
TRIGGER_TIMESTAMP: datetime.now().timestamp(),
TRIGGER_TOPIC: topic
}

previous_data = self.get_state(topic, sensor_type)
previous_state = previous_data.get(TRIGGER_STATE, STATE_OFF)
previous_data = self.get_state(topic, sensor_type)
previous_state = previous_data.get(TRIGGER_STATE, STATE_OFF)

self.set_state(topic, sensor_type, value)
self.set_state(topic, sensor_type, value)

if previous_state == STATE_OFF:
self.callback(sensor_type, payload)

except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno

if previous_state == STATE_OFF:
self.callback(sensor_type, payload)
_LOGGER.error(f"Failed to handle sensor message, Error: {ex}, Line: {line_number}")

def _check_triggers(self, now):
self.hass.async_create_task(self._async_check_triggers(now))
Expand Down
2 changes: 1 addition & 1 deletion custom_components/shinobi/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"codeowners": ["@elad-bar"],
"requirements": [ ],
"config_flow": true,
"version": "1.1.8",
"version": "1.1.10",
"iot_class": "local_polling"
}

0 comments on commit 250ddb1

Please sign in to comment.