Skip to content

Commit

Permalink
Add code support for iAlarm (#19124)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuzakiKK authored and fabaff committed Dec 9, 2018
1 parent 4b4f51f commit 4d4967d
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions homeassistant/components/alarm_control_panel/ialarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
https://home-assistant.io/components/alarm_control_panel.ialarm/
"""
import logging
import re

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 (
CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME, STATE_ALARM_ARMED_AWAY,
STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED, STATE_ALARM_TRIGGERED)
CONF_CODE, CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME,
STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED,
STATE_ALARM_TRIGGERED)
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['pyialarm==0.3']
Expand All @@ -36,30 +38,33 @@ def no_application_protocol(value):
vol.Required(CONF_HOST): vol.All(cv.string, no_application_protocol),
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,
})


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up an iAlarm control panel."""
name = config.get(CONF_NAME)
code = config.get(CONF_CODE)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
host = config.get(CONF_HOST)

url = 'http://{}'.format(host)
ialarm = IAlarmPanel(name, username, password, url)
ialarm = IAlarmPanel(name, code, username, password, url)
add_entities([ialarm], True)


class IAlarmPanel(alarm.AlarmControlPanel):
"""Representation of an iAlarm status."""

def __init__(self, name, username, password, url):
def __init__(self, name, code, username, password, url):
"""Initialize the iAlarm status."""
from pyialarm import IAlarm

self._name = name
self._code = str(code) if code else None
self._username = username
self._password = password
self._url = url
Expand All @@ -71,6 +76,15 @@ def name(self):
"""Return the name of the device."""
return self._name

@property
def code_format(self):
"""Return one or more digits/characters."""
if self._code is None:
return None
if isinstance(self._code, str) and re.search('^\\d+$', self._code):
return 'Number'
return 'Any'

@property
def state(self):
"""Return the state of the device."""
Expand Down Expand Up @@ -98,12 +112,22 @@ def update(self):

def alarm_disarm(self, code=None):
"""Send disarm command."""
self._client.disarm()
if self._validate_code(code):
self._client.disarm()

def alarm_arm_away(self, code=None):
"""Send arm away command."""
self._client.arm_away()
if self._validate_code(code):
self._client.arm_away()

def alarm_arm_home(self, code=None):
"""Send arm home command."""
self._client.arm_stay()
if self._validate_code(code):
self._client.arm_stay()

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")
return check

0 comments on commit 4d4967d

Please sign in to comment.