core/homeassistant/components/alarm_control_panel/alarmdotcom.py

118 lines
3.9 KiB
Python
Raw Normal View History

2016-01-14 06:09:39 +00:00
"""
2016-03-07 19:21:43 +00:00
Interfaces with Alarm.com alarm control panels.
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
2016-09-06 21:48:32 +00:00
import voluptuous as vol
2016-01-14 06:09:39 +00:00
import homeassistant.components.alarm_control_panel as alarm
2016-09-06 21:48:32 +00:00
from homeassistant.components.alarm_control_panel import PLATFORM_SCHEMA
2016-01-14 06:09:39 +00:00
from homeassistant.const import (
2016-02-19 05:27:50 +00:00
CONF_PASSWORD, CONF_USERNAME, STATE_ALARM_ARMED_AWAY,
2016-09-06 21:48:32 +00:00
STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED, STATE_UNKNOWN, CONF_CODE,
CONF_NAME)
import homeassistant.helpers.config_validation as cv
2016-01-14 06:09:39 +00:00
REQUIREMENTS = ['https://github.com/Xorso/pyalarmdotcom'
'/archive/0.1.1.zip'
'#pyalarmdotcom==0.1.1']
2016-09-06 21:48:32 +00:00
_LOGGER = logging.getLogger(__name__)
2016-01-14 06:09:39 +00:00
DEFAULT_NAME = 'Alarm.com'
2016-09-06 21:48:32 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Optional(CONF_CODE): cv.positive_int,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
2016-01-14 06:09:39 +00:00
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-09-06 21:48:32 +00:00
name = config.get(CONF_NAME)
code = config.get(CONF_CODE)
2016-01-14 06:09:39 +00:00
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
add_devices([AlarmDotCom(hass, name, code, username, password)], True)
2016-01-14 06:09:39 +00:00
class AlarmDotCom(alarm.AlarmControlPanel):
2016-03-07 19:21:43 +00:00
"""Represent an Alarm.com status."""
2016-01-14 06:09:39 +00:00
def __init__(self, hass, name, code, username, password):
2016-03-07 19:21:43 +00:00
"""Initialize the Alarm.com status."""
2016-01-14 06:09:39 +00:00
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
self._state = STATE_UNKNOWN
2016-01-14 06:09:39 +00:00
def update(self):
"""Fetch the latest state."""
self._state = self._alarm.state
2016-01-14 06:09:39 +00:00
@property
def name(self):
2016-03-07 19:21:43 +00:00
"""Return the name of the alarm."""
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 19:21:43 +00:00
"""Return the state of the device."""
if self._state == 'Disarmed':
2016-01-14 06:09:39 +00:00
return STATE_ALARM_DISARMED
elif self._state == 'Armed Stay':
2016-01-14 06:09:39 +00:00
return STATE_ALARM_ARMED_HOME
elif self._state == 'Armed Away':
2016-01-14 06:09:39 +00:00
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-08-17 03:53:59 +00:00
if not self._validate_code(code, 'disarming home'):
2016-01-14 06:09:39 +00:00
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