core/homeassistant/components/alarm_control_panel/alarmdotcom.py

112 lines
3.8 KiB
Python
Raw Normal View History

2016-01-14 06:09:39 +00:00
"""
Interfaces with Verisure alarm control panel.
2016-01-15 07:27:41 +00:00
2016-01-14 06:09:39 +00:00
For more details about this platform, please refer to the documentation at
2016-01-15 07:27:41 +00:00
https://home-assistant.io/components/alarm_control_panel.alarmdotcom/
2016-01-14 06:09:39 +00:00
"""
import logging
import homeassistant.components.alarm_control_panel as alarm
from homeassistant.const import (
2016-02-19 05:27:50 +00:00
CONF_PASSWORD, CONF_USERNAME, STATE_ALARM_ARMED_AWAY,
STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED, STATE_UNKNOWN)
2016-01-14 06:09:39 +00:00
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['https://github.com/Xorso/pyalarmdotcom'
'/archive/0.1.1.zip'
'#pyalarmdotcom==0.1.1']
2016-01-14 06:09:39 +00:00
DEFAULT_NAME = 'Alarm.com'
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-07 15:45:21 +00:00
"""Setup an Alarm.com control panel."""
2016-01-14 06:09:39 +00:00
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)])
2016-01-14 13:29:12 +00:00
# pylint: disable=too-many-arguments, too-many-instance-attributes
# pylint: disable=abstract-method
2016-01-14 06:09:39 +00:00
class AlarmDotCom(alarm.AlarmControlPanel):
2016-03-07 15:45:21 +00:00
"""Represents a Alarm.com status."""
2016-01-14 06:09:39 +00:00
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
self._username = username
self._password = password
2016-01-14 06:09:39 +00:00
@property
def should_poll(self):
2016-03-07 15:45:21 +00:00
"""No polling needed."""
2016-01-14 06:09:39 +00:00
return True
@property
def name(self):
2016-03-07 15:45:21 +00:00
"""Returns the name of the device."""
2016-01-14 06:09:39 +00:00
return self._name
@property
def code_format(self):
2016-03-07 15:45:21 +00:00
"""One or more characters if code is defined."""
2016-01-14 06:09:39 +00:00
return None if self._code is None else '.+'
@property
def state(self):
2016-03-07 15:45:21 +00:00
"""Returns the state of the device."""
2016-01-14 06:09:39 +00:00
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):
2016-03-07 15:45:21 +00:00
"""Send disarm command."""
2016-01-14 06:09:39 +00:00
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.disarm()
2016-01-14 06:09:39 +00:00
def alarm_arm_home(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send arm home command."""
2016-01-14 06:09:39 +00:00
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()
2016-01-14 06:09:39 +00:00
def alarm_arm_away(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send arm away command."""
2016-01-14 06:09:39 +00:00
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()
2016-01-14 06:09:39 +00:00
def _validate_code(self, code, state):
2016-03-07 15:45:21 +00:00
"""Validate given code."""
2016-01-14 06:09:39 +00:00
check = self._code is None or code == self._code
if not check:
_LOGGER.warning('Wrong code entered for %s', state)
return check