2019-04-03 15:40:03 +00:00
|
|
|
"""Interfaces with Alarm.com alarm control panels."""
|
2018-01-21 06:35:38 +00:00
|
|
|
import logging
|
2018-05-29 05:50:27 +00:00
|
|
|
import re
|
2018-01-21 06:35:38 +00:00
|
|
|
|
2019-10-17 13:05:45 +00:00
|
|
|
from pyalarmdotcom import Alarmdotcom
|
2016-09-06 21:48:32 +00:00
|
|
|
import voluptuous as vol
|
2018-01-21 06:35:38 +00:00
|
|
|
|
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
|
2019-11-25 23:42:53 +00:00
|
|
|
from homeassistant.components.alarm_control_panel.const import (
|
|
|
|
SUPPORT_ALARM_ARM_AWAY,
|
|
|
|
SUPPORT_ALARM_ARM_HOME,
|
|
|
|
)
|
2016-01-14 06:09:39 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CODE,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
|
|
|
STATE_ALARM_ARMED_AWAY,
|
|
|
|
STATE_ALARM_ARMED_HOME,
|
|
|
|
STATE_ALARM_DISARMED,
|
|
|
|
)
|
2017-03-29 06:21:40 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2018-01-21 06:35:38 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-01-14 06:09:39 +00:00
|
|
|
|
2016-09-06 21:48:32 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Alarm.com"
|
2016-01-14 06:09:39 +00:00
|
|
|
|
2019-07-31 19:25:30 +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-09-06 21:48:32 +00:00
|
|
|
|
2016-01-14 06:09:39 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up a 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)
|
|
|
|
|
2017-03-29 06:21:40 +00:00
|
|
|
alarmdotcom = AlarmDotCom(hass, name, code, username, password)
|
2018-10-01 06:49:19 +00:00
|
|
|
await alarmdotcom.async_login()
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities([alarmdotcom])
|
2016-01-14 06:09:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AlarmDotCom(alarm.AlarmControlPanel):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Representation of an Alarm.com status."""
|
2016-03-07 19:21:43 +00:00
|
|
|
|
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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
_LOGGER.debug("Setting up Alarm.com...")
|
2016-01-14 06:09:39 +00:00
|
|
|
self._hass = hass
|
|
|
|
self._name = name
|
|
|
|
self._code = str(code) if code else None
|
2016-01-16 05:15:31 +00:00
|
|
|
self._username = username
|
|
|
|
self._password = password
|
2017-03-29 06:21:40 +00:00
|
|
|
self._websession = async_get_clientsession(self._hass)
|
2019-01-24 07:20:20 +00:00
|
|
|
self._state = None
|
2019-07-31 19:25:30 +00:00
|
|
|
self._alarm = Alarmdotcom(username, password, self._websession, hass.loop)
|
2017-03-29 06:21:40 +00:00
|
|
|
|
2018-10-01 06:49:19 +00:00
|
|
|
async def async_login(self):
|
2017-03-29 06:21:40 +00:00
|
|
|
"""Login to Alarm.com."""
|
2018-10-01 06:49:19 +00:00
|
|
|
await self._alarm.async_login()
|
2017-03-29 06:21:40 +00:00
|
|
|
|
2018-10-01 06:49:19 +00:00
|
|
|
async def async_update(self):
|
2016-11-09 03:57:46 +00:00
|
|
|
"""Fetch the latest state."""
|
2018-10-01 06:49:19 +00:00
|
|
|
await self._alarm.async_update()
|
2017-03-29 06:21:40 +00:00
|
|
|
return self._alarm.state
|
2016-11-09 03:57:46 +00:00
|
|
|
|
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):
|
2018-05-29 05:50:27 +00:00
|
|
|
"""Return one or more digits/characters."""
|
|
|
|
if self._code is None:
|
|
|
|
return None
|
2019-07-31 19:25:30 +00:00
|
|
|
if isinstance(self._code, str) and re.search("^\\d+$", self._code):
|
2019-01-13 10:59:12 +00:00
|
|
|
return alarm.FORMAT_NUMBER
|
2019-01-14 12:02:30 +00:00
|
|
|
return alarm.FORMAT_TEXT
|
2016-01-14 06:09:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-07 19:21:43 +00:00
|
|
|
"""Return the state of the device."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._alarm.state.lower() == "disarmed":
|
2016-01-14 06:09:39 +00:00
|
|
|
return STATE_ALARM_DISARMED
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._alarm.state.lower() == "armed stay":
|
2016-01-14 06:09:39 +00:00
|
|
|
return STATE_ALARM_ARMED_HOME
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._alarm.state.lower() == "armed away":
|
2016-01-14 06:09:39 +00:00
|
|
|
return STATE_ALARM_ARMED_AWAY
|
2019-01-24 07:20:20 +00:00
|
|
|
return None
|
2016-01-14 06:09:39 +00:00
|
|
|
|
2019-11-25 23:42:53 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Return the list of supported features."""
|
|
|
|
return SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY
|
|
|
|
|
2018-05-05 14:21:58 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"sensor_status": self._alarm.sensor_status}
|
2018-05-05 14:21:58 +00:00
|
|
|
|
2018-10-01 06:49:19 +00:00
|
|
|
async def async_alarm_disarm(self, code=None):
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send disarm command."""
|
2017-03-29 06:21:40 +00:00
|
|
|
if self._validate_code(code):
|
2018-10-01 06:49:19 +00:00
|
|
|
await self._alarm.async_alarm_disarm()
|
2017-03-29 06:21:40 +00:00
|
|
|
|
2018-10-01 06:49:19 +00:00
|
|
|
async def async_alarm_arm_home(self, code=None):
|
2017-03-29 06:21:40 +00:00
|
|
|
"""Send arm hom command."""
|
|
|
|
if self._validate_code(code):
|
2018-10-01 06:49:19 +00:00
|
|
|
await self._alarm.async_alarm_arm_home()
|
2017-03-29 06:21:40 +00:00
|
|
|
|
2018-10-01 06:49:19 +00:00
|
|
|
async def async_alarm_arm_away(self, code=None):
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send arm away command."""
|
2017-03-29 06:21:40 +00:00
|
|
|
if self._validate_code(code):
|
2018-10-01 06:49:19 +00:00
|
|
|
await self._alarm.async_alarm_arm_away()
|
2017-03-29 06:21:40 +00:00
|
|
|
|
|
|
|
def _validate_code(self, code):
|
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:
|
2018-01-21 06:35:38 +00:00
|
|
|
_LOGGER.warning("Wrong code entered")
|
2016-01-14 06:09:39 +00:00
|
|
|
return check
|