Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
77 changes: 31 additions & 46 deletions abodepy/automation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Representation of an automation configured in Abode."""
import json
import logging

from abodepy.exceptions import AbodeException

import abodepy.helpers.constants as CONST
import abodepy.helpers.errors as ERROR

_LOGGER = logging.getLogger(__name__)


class AbodeAutomation:
"""Class for viewing and controlling automations."""
Expand All @@ -15,49 +18,49 @@ def __init__(self, abode, automation):
self._abode = abode
self._automation = automation

def set_active(self, active):
"""Activate and deactivate an automation."""
url = CONST.AUTOMATION_EDIT_URL
url = url.replace(
'$AUTOMATIONID$', self.automation_id)
def enable(self, enable):
"""Enable an automation."""
url = str.replace(CONST.AUTOMATION_ID_URL, '$AUTOMATIONID$',
self.automation_id)

self._automation['is_active'] = str(int(active))
self._automation['enabled'] = enable

response = self._abode.send_request(
method="put", url=url, data=self._automation)
method="patch", url=url, data={'enabled': enable})

response_object = json.loads(response.text)

_LOGGER.info("Set automation %s enable to: %s", self.name,
str(response_object['enabled']))
_LOGGER.debug("Automation response: %s", response.text)

if isinstance(response_object, (tuple, list)):
response_object = response_object[0]

if (str(response_object['id']) != str(self._automation['id']) or
response_object['is_active'] != self._automation['is_active']):
response_object['enabled'] != self._automation['enabled']):
raise AbodeException((ERROR.INVALID_AUTOMATION_EDIT_RESPONSE))

self.update(response_object)

return True

def trigger(self, only_manual=True):
"""Trigger a quick-action automation."""
if not self.is_quick_action and only_manual:
raise AbodeException((ERROR.TRIGGER_NON_QUICKACTION))

url = CONST.AUTOMATION_APPLY_URL
url = url.replace(
'$AUTOMATIONID$', self.automation_id)
def trigger(self):
"""Trigger an automation."""
url = str.replace(CONST.AUTOMATION_APPLY_URL, '$AUTOMATIONID$',
self.automation_id)

self._abode.send_request(
method="put", url=url, data=self._automation)
method="post", url=url)

_LOGGER.info("Automation triggered: %s", self.name)

return True

def refresh(self):
"""Refresh the automation."""
url = CONST.AUTOMATION_ID_URL
url = url.replace(
'$AUTOMATIONID$', self.automation_id)
url = str.replace(CONST.AUTOMATION_ID_URL, '$AUTOMATIONID$',
self.automation_id)

response = self._abode.send_request(method="get", url=url)
response_object = json.loads(response.text)
Expand Down Expand Up @@ -85,41 +88,23 @@ def name(self):
"""Get the name of the automation."""
return self._automation['name']

@property
def is_enabled(self):
"""Return True if the automation is enabled."""
return self._automation['enabled']

@property
def generic_type(self):
"""Get the generic type of the automation."""
if self.is_quick_action:
return CONST.TYPE_QUICK_ACTION

return CONST.TYPE_AUTOMATION

@property
def type(self):
"""Get the type of the automation."""
return self._automation['type']

@property
def sub_type(self):
"""Get the sub type of the automation."""
return self._automation['sub_type']

@property
def is_active(self):
"""Return if the automation is active."""
return int(self._automation.get('is_active', '0')) == 1

@property
def is_quick_action(self):
"""Return if the automation is a quick action."""
return self.type == CONST.AUTOMATION_TYPE_MANUAL
return self._automation['subType']

@property
def desc(self):
"""Get a short description of the automation."""
# Auto Away (1) - Location - Enabled
active = 'inactive'
if self.is_active:
active = 'active'

return '{0} (ID: {1}) - {2} - {3}'.format(
self.name, self.automation_id, self.type, active)
return '{0} (ID: {1}, Enabled: {2})'.format(
self.name, self.automation_id, self.is_enabled)
11 changes: 1 addition & 10 deletions abodepy/helpers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ def get_panel_mode_url(area, mode):
SOUNDS_URL = BASE_URL + 'api/v1/sounds'
SIREN_URL = BASE_URL + 'api/v1/siren'

AUTOMATION_URL = BASE_URL + 'api/v1/automation/'
AUTOMATION_URL = BASE_URL + 'integrations/v1/automations/'
AUTOMATION_ID_URL = AUTOMATION_URL + '$AUTOMATIONID$/'
AUTOMATION_EDIT_URL = AUTOMATION_ID_URL + 'edit'
AUTOMATION_APPLY_URL = AUTOMATION_ID_URL + 'apply'

TIMELINE_IMAGES_ID_URL = BASE_URL + \
Expand Down Expand Up @@ -125,14 +124,6 @@ def get_panel_mode_url(area, mode):
COLOR_MODE_ON = 0
COLOR_MODE_OFF = 2

AUTOMATION_TYPE_MANUAL = 'manual'
AUTOMATION_TYPE_LOCATION = 'location'
AUTOMATION_TYPE_SCHEDULE = 'schedule'
AUTOMATION_TYPE_STATUS = 'status'

AUTOMATION_SUBTYPE_ENTERING_HOME = 'entering_home'
AUTOMATION_SUBTYPE_LEAVING_HOME = 'leaving_home'

# GENERIC ABODE DEVICE TYPES
TYPE_ALARM = "alarm"
TYPE_AUTOMATION = "automation"
Expand Down
4 changes: 2 additions & 2 deletions abodepy/helpers/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
INVALID_AUTOMATION_EDIT_RESPONSE = (
16, "Automation edit response did not match expected values.")

TRIGGER_NON_QUICKACTION = (
17, "Can not trigger an automation that is not a manual quick-action.")
# TRIGGER_NON_QUICKACTION = (
# 17, "Can not trigger an automation that is not a manual quick-action.")

UNABLE_TO_MAP_DEVICE = (
18, "Unable to map device json to device class - no type tag found.")
Expand Down
39 changes: 30 additions & 9 deletions tests/mock/automation.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
"""Mock Abode Automation."""


def get_response_ok(aid, name, is_active, the_type, sub_type=""):
def get_response_ok(name, enabled, aid):
"""Return automation json."""
return '''{
"id": ''' + str(int(aid)) + ''',
"name": "''' + name + '''",
"actions": "a=1&m=2;",
"condition": "HHMM_2300_2300",
"is_active": "''' + str(int(is_active)) + '''",
"sub_type": "''' + sub_type + '''",
"type": "''' + the_type + '''",
"notify_msg": null
}'''
"enabled": "''' + str(enabled) + '''",
"version": 2,
"id": "''' + aid + '''",
"subType": "",
"actions": [{
"directive": {
"trait": "panel.traits.panelMode",
"name": "panel.directives.arm",
"state": {
"panelMode": "AWAY"
}
}
}],
"conditions": {},
"triggers": {
"operator": "OR",
"expressions": [{
"mobileDevices": ["89381", "658"],
"property": {
"trait": "mobile.traits.location",
"name": "location",
"rule": {
"location": "31675",
"equalTo": "LAST_OUT"
}
}
}]
}
}'''
Loading