diff --git a/.coveragerc b/.coveragerc index 674516aaeba..dc76159c045 100644 --- a/.coveragerc +++ b/.coveragerc @@ -5,6 +5,8 @@ omit = homeassistant/__main__.py # omit pieces of code that rely on external devices being present + homeassistant/components/alarm_control_panel/alarmdotcom.py + homeassistant/components/arduino.py homeassistant/components/*/arduino.py diff --git a/homeassistant/components/alarm_control_panel/alarmdotcom.py b/homeassistant/components/alarm_control_panel/alarmdotcom.py new file mode 100644 index 00000000000..d10afc429e6 --- /dev/null +++ b/homeassistant/components/alarm_control_panel/alarmdotcom.py @@ -0,0 +1,102 @@ +""" +homeassistant.components.alarm_control_panel.alarmdotcom +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Interfaces with Verisure alarm control panel. +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/alarmdotcom/ +""" +import logging + +import homeassistant.components.alarm_control_panel as alarm +from homeassistant.const import CONF_USERNAME, CONF_PASSWORD + +from homeassistant.const import ( + STATE_UNKNOWN, + STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY) + +_LOGGER = logging.getLogger(__name__) + + +REQUIREMENTS = ['https://github.com/Xorso/pyalarmdotcom' + '/archive/0.0.7.zip' + '#pyalarmdotcom==0.0.7'] +DEFAULT_NAME = 'Alarm.com' + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Setup Alarm.com control panel """ + + username = config.get(CONF_USERNAME) + password = config.get(CONF_PASSWORD) + + if username is None or password is None: + _LOGGER.error('Must specify username and password!') + return False + + add_devices([AlarmDotCom(hass, + config.get('name', DEFAULT_NAME), + config.get('code'), + username, + password)]) + + +# pylint: disable=too-many-arguments, too-many-instance-attributes +# pylint: disable=abstract-method +class AlarmDotCom(alarm.AlarmControlPanel): + """ Represents a Alarm.com status within HA """ + + def __init__(self, hass, name, code, username, password): + from pyalarmdotcom.pyalarmdotcom import Alarmdotcom + self._alarm = Alarmdotcom(username, password, timeout=10) + self._hass = hass + self._name = name + self._code = str(code) if code else None + + @property + def should_poll(self): + return True + + @property + def name(self): + return self._name + + @property + def code_format(self): + """ One or more characters if code is defined """ + return None if self._code is None else '.+' + + @property + def state(self): + if self._alarm.state == 'Disarmed': + return STATE_ALARM_DISARMED + elif self._alarm.state == 'Armed Stay': + return STATE_ALARM_ARMED_HOME + elif self._alarm.state == 'Armed Away': + return STATE_ALARM_ARMED_AWAY + else: + return STATE_UNKNOWN + + def alarm_disarm(self, code=None): + if not self._validate_code(code, 'arming home'): + return + self._alarm.disarm() + self.update_ha_state() + + def alarm_arm_home(self, code=None): + if not self._validate_code(code, 'arming home'): + return + self._alarm.arm_stay() + self.update_ha_state() + + def alarm_arm_away(self, code=None): + if not self._validate_code(code, 'arming home'): + return + self._alarm.arm_away() + self.update_ha_state() + + def _validate_code(self, code, state): + """ Validate given code. """ + check = self._code is None or code == self._code + if not check: + _LOGGER.warning('Wrong code entered for %s', state) + return check diff --git a/requirements_all.txt b/requirements_all.txt index 7b43b7a3f1c..11f3873c4b1 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -6,6 +6,9 @@ pip>=7.0.0 vincenty==0.1.3 jinja2>=2.8 +# homeassistant.components.alarm_control_panel.alarmdotcom +https://github.com/Xorso/pyalarmdotcom/archive/0.0.7.zip#pyalarmdotcom==0.0.7 + # homeassistant.components.arduino PyMata==2.07a