From d1b519a41809edc9dfbb1d1706cbb3e2b583f724 Mon Sep 17 00:00:00 2001 From: Xorso Date: Wed, 29 Mar 2017 00:21:40 -0600 Subject: [PATCH] Updating Alarm.com Component for async and no Selenium (#6752) * Updating Alarm.com Component for async and no Selenium * Fixed gen_all_requirements --- .../alarm_control_panel/alarmdotcom.py | 83 ++++++++++--------- requirements_all.txt | 6 +- 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/homeassistant/components/alarm_control_panel/alarmdotcom.py b/homeassistant/components/alarm_control_panel/alarmdotcom.py index 07f90cf4476..22cbdefd403 100644 --- a/homeassistant/components/alarm_control_panel/alarmdotcom.py +++ b/homeassistant/components/alarm_control_panel/alarmdotcom.py @@ -1,13 +1,13 @@ """ + Interfaces with Alarm.com alarm control panels. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/alarm_control_panel.alarmdotcom/ """ import logging - +import asyncio import voluptuous as vol - import homeassistant.components.alarm_control_panel as alarm from homeassistant.components.alarm_control_panel import PLATFORM_SCHEMA from homeassistant.const import ( @@ -15,10 +15,9 @@ from homeassistant.const import ( STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED, STATE_UNKNOWN, CONF_CODE, CONF_NAME) import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.aiohttp_client import async_get_clientsession -REQUIREMENTS = ['https://github.com/Xorso/pyalarmdotcom' - '/archive/0.1.1.zip' - '#pyalarmdotcom==0.1.1'] +REQUIREMENTS = ['pyalarmdotcom==0.2.9'] _LOGGER = logging.getLogger(__name__) @@ -32,14 +31,17 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ }) -def setup_platform(hass, config, add_devices, discovery_info=None): - """Setup an Alarm.com control panel.""" +@asyncio.coroutine +def async_setup_platform(hass, config, async_add_devices, discovery_info=None): + """Setup a Alarm.com control panel.""" name = config.get(CONF_NAME) code = config.get(CONF_CODE) username = config.get(CONF_USERNAME) password = config.get(CONF_PASSWORD) - add_devices([AlarmDotCom(hass, name, code, username, password)], True) + alarmdotcom = AlarmDotCom(hass, name, code, username, password) + yield from alarmdotcom.async_login() + async_add_devices([alarmdotcom]) class AlarmDotCom(alarm.AlarmControlPanel): @@ -47,18 +49,30 @@ class AlarmDotCom(alarm.AlarmControlPanel): def __init__(self, hass, name, code, username, password): """Initialize the Alarm.com status.""" - from pyalarmdotcom.pyalarmdotcom import Alarmdotcom - self._alarm = Alarmdotcom(username, password, timeout=10) + from pyalarmdotcom import Alarmdotcom + _LOGGER.debug('Setting up Alarm.com...') self._hass = hass self._name = name self._code = str(code) if code else None self._username = username self._password = password + self._websession = async_get_clientsession(self._hass) self._state = STATE_UNKNOWN + self._alarm = Alarmdotcom(username, + password, + self._websession, + hass.loop) - def update(self): + @asyncio.coroutine + def async_login(self): + """Login to Alarm.com.""" + yield from self._alarm.async_login() + + @asyncio.coroutine + def async_update(self): """Fetch the latest state.""" - self._state = self._alarm.state + yield from self._alarm.async_update() + return self._alarm.state @property def name(self): @@ -73,45 +87,36 @@ class AlarmDotCom(alarm.AlarmControlPanel): @property def state(self): """Return the state of the device.""" - if self._state == 'Disarmed': + if self._alarm.state.lower() == 'disarmed': return STATE_ALARM_DISARMED - elif self._state == 'Armed Stay': + elif self._alarm.state.lower() == 'armed stay': return STATE_ALARM_ARMED_HOME - elif self._state == 'Armed Away': + elif self._alarm.state.lower() == 'armed away': return STATE_ALARM_ARMED_AWAY else: return STATE_UNKNOWN - def alarm_disarm(self, code=None): + @asyncio.coroutine + def async_alarm_disarm(self, code=None): """Send disarm command.""" - if not self._validate_code(code, 'disarming home'): - return - from pyalarmdotcom.pyalarmdotcom import Alarmdotcom - # Open another session to alarm.com to fire off the command - _alarm = Alarmdotcom(self._username, self._password, timeout=10) - _alarm.disarm() + if self._validate_code(code): + yield from self._alarm.async_alarm_disarm() - def alarm_arm_home(self, code=None): - """Send arm home command.""" - if not self._validate_code(code, 'arming home'): - return - from pyalarmdotcom.pyalarmdotcom import Alarmdotcom - # Open another session to alarm.com to fire off the command - _alarm = Alarmdotcom(self._username, self._password, timeout=10) - _alarm.arm_stay() + @asyncio.coroutine + def async_alarm_arm_home(self, code=None): + """Send arm hom command.""" + if self._validate_code(code): + yield from self._alarm.async_alarm_arm_home() - def alarm_arm_away(self, code=None): + @asyncio.coroutine + def async_alarm_arm_away(self, code=None): """Send arm away command.""" - if not self._validate_code(code, 'arming home'): - return - from pyalarmdotcom.pyalarmdotcom import Alarmdotcom - # Open another session to alarm.com to fire off the command - _alarm = Alarmdotcom(self._username, self._password, timeout=10) - _alarm.arm_away() + if self._validate_code(code): + yield from self._alarm.async_alarm_arm_away() - def _validate_code(self, code, state): + def _validate_code(self, code): """Validate given code.""" check = self._code is None or code == self._code if not check: - _LOGGER.warning('Wrong code entered for %s', state) + _LOGGER.warning('Wrong code entered.') return check diff --git a/requirements_all.txt b/requirements_all.txt index dac5add5c94..2c3301671a3 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -236,9 +236,6 @@ https://github.com/TheRealLink/pylgtv/archive/v0.1.4.zip#pylgtv==0.1.4 # homeassistant.components.switch.thinkingcleaner https://github.com/TheRealLink/pythinkingcleaner/archive/v0.0.2.zip#pythinkingcleaner==0.0.2 -# homeassistant.components.alarm_control_panel.alarmdotcom -https://github.com/Xorso/pyalarmdotcom/archive/0.1.1.zip#pyalarmdotcom==0.1.1 - # homeassistant.components.media_player.braviatv https://github.com/aparraga/braviarc/archive/0.3.6.zip#braviarc==0.3.6 @@ -464,6 +461,9 @@ pyHS100==0.2.4.1 # homeassistant.components.rfxtrx pyRFXtrx==0.17.0 +# homeassistant.components.alarm_control_panel.alarmdotcom +pyalarmdotcom==0.2.9 + # homeassistant.components.notify.xmpp pyasn1-modules==0.0.8