alarm component

This commit is contained in:
Per Sandström 2015-09-13 07:42:38 +02:00
parent e0db473294
commit ab6cb43d5b
7 changed files with 231 additions and 32 deletions

View file

@ -0,0 +1,106 @@
"""
homeassistant.components.sensor
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Component to interface with various sensors that can be monitored.
"""
import logging
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.components import verisure
from homeassistant.const import (
STATE_UNKNOWN,
STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY,
ATTR_ENTITY_PICTURE,
SERVICE_ALARM_DISARM, SERVICE_ALARM_ARM_HOME, SERVICE_ALARM_ARM_AWAY)
DOMAIN = 'alarm'
DEPENDENCIES = []
SCAN_INTERVAL = 30
ENTITY_ID_FORMAT = DOMAIN + '.{}'
# Maps discovered services to their platforms
DISCOVERY_PLATFORMS = {
verisure.DISCOVER_SENSORS: 'verisure'
}
SERVICE_TO_METHOD = {
SERVICE_ALARM_DISARM: 'alarm_disarm',
SERVICE_ALARM_ARM_HOME: 'alarm_arm_home',
SERVICE_ALARM_ARM_AWAY: 'alarm_arm_away',
}
ATTR_CODE = 'code'
ATTR_TO_PROPERTY = [
ATTR_CODE,
]
def setup(hass, config):
""" Track states and offer events for sensors. """
component = EntityComponent(
logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL,
DISCOVERY_PLATFORMS)
component.setup(config)
def alarm_service_handler(service):
""" Maps services to methods on Alarm. """
target_alarms = component.extract_from_service(service)
if ATTR_CODE not in service.data:
return
code = service.data[ATTR_CODE]
method = SERVICE_TO_METHOD[service.service]
for alarm in target_alarms:
getattr(alarm, method)(code)
for service in SERVICE_TO_METHOD:
hass.services.register(DOMAIN, service, alarm_service_handler)
return True
def alarm_disarm(hass, code, entity_id=None):
""" Send the alarm the command for disarm. """
data = {ATTR_CODE: code}
if entity_id:
data[ATTR_ENTITY_ID] = entity_id
hass.services.call(DOMAIN, SERVICE_ALARM_DISARM, data)
def alarm_arm_home(hass, code, entity_id=None):
""" Send the alarm the command for arm home. """
data = {ATTR_CODE: code}
if entity_id:
data[ATTR_ENTITY_ID] = entity_id
hass.services.call(DOMAIN, SERVICE_ALARM_ARM_HOME, data)
def alarm_arm_away(hass, code, entity_id=None):
""" Send the alarm the command for arm away. """
data = {ATTR_CODE: code}
if entity_id:
data[ATTR_ENTITY_ID] = entity_id
hass.services.call(DOMAIN, SERVICE_ALARM_ARM_AWAY, data)
class AlarmControl(Entity):
def alarm_disarm(self, code):
""" Send disar command. """
raise NotImplementedError()
def alarm_arm_home(self, code):
""" Send pause command. """
raise NotImplementedError()
def alarm_arm_away(self, code):
""" Send pause command. """
raise NotImplementedError()

View file

@ -0,0 +1,110 @@
"""
homeassistant.components.alarm.verisure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Interfaces with Verisure alarm.
"""
import logging
import homeassistant.components.verisure as verisure
import homeassistant.components.alarm as alarm
from homeassistant.helpers.entity import Entity
<<<<<<< HEAD
from homeassistant.const import (STATE_UNKNOWN,
STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY)
=======
from homeassistant.const import STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY
>>>>>>> 614caa33ae4b9fd13bd26436dd4c1dd09ff01119
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the Verisure platform. """
if not verisure.MY_PAGES:
_LOGGER.error('A connection has not been made to Verisure mypages.')
return False
<<<<<<< HEAD
alarms = []
alarms.extend([
=======
sensors = []
sensors.extend([
>>>>>>> 614caa33ae4b9fd13bd26436dd4c1dd09ff01119
VerisureAlarm(value)
for value in verisure.get_alarm_status().values()
if verisure.SHOW_ALARM
])
<<<<<<< HEAD
add_devices(alarms)
class VerisureAlarm(alarm.AlarmControl):
=======
add_devices(sensors)
class VerisureAlarm(Entity):
>>>>>>> 614caa33ae4b9fd13bd26436dd4c1dd09ff01119
""" represents a Verisure alarm status within home assistant. """
def __init__(self, alarm_status):
self._id = alarm_status.id
self._device = verisure.MY_PAGES.DEVICE_ALARM
<<<<<<< HEAD
self._state = STATE_UNKNOWN
=======
>>>>>>> 614caa33ae4b9fd13bd26436dd4c1dd09ff01119
@property
def name(self):
""" Returns the name of the device. """
return 'Alarm {}'.format(self._id)
@property
def state(self):
""" Returns the state of the device. """
if verisure.STATUS[self._device][self._id].status == 'unarmed':
<<<<<<< HEAD
self._state = STATE_ALARM_DISARMED
elif verisure.STATUS[self._device][self._id].status == 'armedhome':
self._state = STATE_ALARM_ARMED_HOME
elif verisure.STATUS[self._device][self._id].status == 'armedaway':
self._state = STATE_ALARM_ARMED_AWAY
elif verisure.STATUS[self._device][self._id].status != 'pending':
_LOGGER.error('Unknown alarm state ' + verisure.STATUS[self._device][self._id].status)
return self._state
=======
return STATE_ALARM_DISARMED
if verisure.STATUS[self._device][self._id].status == 'armed_home':
return STATE_ALARM_ARMED_HOME
if verisure.STATUS[self._device][self._id].status == 'armed_away':
return STATE_ALARM_ARMED_AWAY
>>>>>>> 614caa33ae4b9fd13bd26436dd4c1dd09ff01119
def update(self):
''' update alarm status '''
verisure.update()
<<<<<<< HEAD
def alarm_disarm(self, code):
""" Send disarm command. """
verisure.MY_PAGES.set_alarm_status(code, verisure.MY_PAGES.ALARM_DISARMED)
_LOGGER.warning('disarming')
def alarm_arm_home(self, code):
""" Send arm home command. """
verisure.MY_PAGES.set_alarm_status(code, verisure.MY_PAGES.ALARM_ARMED_HOME)
_LOGGER.warning('arming home')
def alarm_arm_away(self, code):
""" Send arm away command. """
verisure.MY_PAGES.set_alarm_status(code, verisure.MY_PAGES.ALARM_ARMED_AWAY)
_LOGGER.warning('arming away')
=======
>>>>>>> 614caa33ae4b9fd13bd26436dd4c1dd09ff01119

@ -1 +1 @@
Subproject commit 1c82a536312e8321716ab7d80a5d17045d20d77f
Subproject commit b0b12e20e0f61df849c414c2dfbcf9923f784631

View file

@ -36,12 +36,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
hasattr(value, 'humidity') and value.humidity
])
sensors.extend([
VerisureAlarm(value)
for value in verisure.get_alarm_status().values()
if verisure.SHOW_ALARM
])
add_devices(sensors)
@ -103,25 +97,3 @@ class VerisureHygrometer(Entity):
def update(self):
''' update sensor '''
verisure.update()
class VerisureAlarm(Entity):
""" represents a Verisure alarm status within home assistant. """
def __init__(self, alarm_status):
self._id = alarm_status.id
self._device = verisure.MY_PAGES.DEVICE_ALARM
@property
def name(self):
""" Returns the name of the device. """
return 'Alarm {}'.format(self._id)
@property
def state(self):
""" Returns the state of the device. """
return verisure.STATUS[self._device][self._id].label
def update(self):
''' update sensor '''
verisure.update()

View file

@ -58,8 +58,9 @@ from homeassistant.const import (
DOMAIN = "verisure"
DISCOVER_SENSORS = 'verisure.sensors'
DISCOVER_SWITCHES = 'verisure.switches'
DISCOVER_ALARMS = 'verisure.alarms'
DEPENDENCIES = []
DEPENDENCIES = ['alarm']
REQUIREMENTS = [
'https://github.com/persandstrom/python-verisure/archive/master.zip'
]
@ -121,7 +122,8 @@ def setup(hass, config):
# Load components for the devices in the ISY controller that we support
for comp_name, discovery in ((('sensor', DISCOVER_SENSORS),
('switch', DISCOVER_SWITCHES))):
('switch', DISCOVER_SWITCHES),
('alarm', DISCOVER_ALARMS))):
component = get_component(comp_name)
_LOGGER.info(config[DOMAIN])
bootstrap.setup_component(hass, component.DOMAIN, config)
@ -164,7 +166,7 @@ def reconnect():
def update():
''' Updates the status of verisure components '''
if WRONG_PASSWORD_GIVEN:
# Is there any way to inform user?
_LOGGER.error('Wrong password')
return
try:

View file

@ -43,6 +43,9 @@ STATE_CLOSED = 'closed'
STATE_PLAYING = 'playing'
STATE_PAUSED = 'paused'
STATE_IDLE = 'idle'
STATE_ALARM_DISARMED = 'disarmed'
STATE_ALARM_ARMED_HOME = 'armed_home'
STATE_ALARM_ARMED_AWAY = 'armed_away'
# #### STATE AND EVENT ATTRIBUTES ####
# Contains current time for a TIME_CHANGED event
@ -110,6 +113,10 @@ SERVICE_MEDIA_NEXT_TRACK = "media_next_track"
SERVICE_MEDIA_PREVIOUS_TRACK = "media_previous_track"
SERVICE_MEDIA_SEEK = "media_seek"
SERVICE_ALARM_DISARM = "alarm_disarm"
SERVICE_ALARM_ARM_HOME = "alarm_arm_home"
SERVICE_ALARM_ARM_AWAY = "alarm_arm_away"
# #### API / REMOTE ####
SERVER_PORT = 8123

View file

@ -123,6 +123,8 @@ def get_component(comp_name):
# custom_components/switch/some_platform.py exists,
# the import custom_components.switch would succeeed.
if module.__spec__.origin == 'namespace':
print("!!!!!!!!!!!!!!!!!!!!! " + module.__spec__.origin)
continue
_LOGGER.info("Loaded %s from %s", comp_name, path)