From a42288d056c6c4418856534c0dbacf2489a0a6c8 Mon Sep 17 00:00:00 2001 From: William Scanlon Date: Thu, 19 Jul 2018 13:13:46 -0400 Subject: [PATCH] Upgrade to simplisafe-python v2 to use new SimpliSafe API (#15542) * Upgrade to simplisafe-python v2 to use new SimpliSafe API --- .../alarm_control_panel/simplisafe.py | 77 +++++++++---------- requirements_all.txt | 2 +- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/homeassistant/components/alarm_control_panel/simplisafe.py b/homeassistant/components/alarm_control_panel/simplisafe.py index b4906acba3c..f818df4bd71 100644 --- a/homeassistant/components/alarm_control_panel/simplisafe.py +++ b/homeassistant/components/alarm_control_panel/simplisafe.py @@ -9,23 +9,22 @@ 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.components.alarm_control_panel import ( + PLATFORM_SCHEMA, AlarmControlPanel) from homeassistant.const import ( CONF_CODE, CONF_NAME, CONF_PASSWORD, CONF_USERNAME, - EVENT_HOMEASSISTANT_STOP, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, + STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED, STATE_UNKNOWN) import homeassistant.helpers.config_validation as cv -REQUIREMENTS = ['simplisafe-python==1.0.5'] +REQUIREMENTS = ['simplisafe-python==2.0.2'] _LOGGER = logging.getLogger(__name__) DEFAULT_NAME = 'SimpliSafe' -DOMAIN = 'simplisafe' -NOTIFICATION_ID = 'simplisafe_notification' -NOTIFICATION_TITLE = 'SimpliSafe Setup' +ATTR_ALARM_ACTIVE = "alarm_active" +ATTR_TEMPERATURE = "temperature" PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_PASSWORD): cv.string, @@ -37,36 +36,27 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the SimpliSafe platform.""" - from simplipy.api import SimpliSafeApiInterface, get_systems + from simplipy.api import SimpliSafeApiInterface, SimpliSafeAPIException name = config.get(CONF_NAME) code = config.get(CONF_CODE) username = config.get(CONF_USERNAME) password = config.get(CONF_PASSWORD) - simplisafe = SimpliSafeApiInterface() - status = simplisafe.set_credentials(username, password) - if status: - hass.data[DOMAIN] = simplisafe - locations = get_systems(simplisafe) - for location in locations: - add_devices([SimpliSafeAlarm(location, name, code)]) - else: - message = 'Failed to log into SimpliSafe. Check credentials.' - _LOGGER.error(message) - hass.components.persistent_notification.create( - message, - title=NOTIFICATION_TITLE, - notification_id=NOTIFICATION_ID) - return False + try: + simplisafe = SimpliSafeApiInterface(username, password) + except SimpliSafeAPIException: + _LOGGER.error("Failed to setup SimpliSafe") + return - def logout(event): - """Logout of the SimpliSafe API.""" - hass.data[DOMAIN].logout() + systems = [] - hass.bus.listen(EVENT_HOMEASSISTANT_STOP, logout) + for system in simplisafe.get_systems(): + systems.append(SimpliSafeAlarm(system, name, code)) + + add_devices(systems) -class SimpliSafeAlarm(alarm.AlarmControlPanel): +class SimpliSafeAlarm(AlarmControlPanel): """Representation of a SimpliSafe alarm.""" def __init__(self, simplisafe, name, code): @@ -75,12 +65,17 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel): self._name = name self._code = str(code) if code else None + @property + def unique_id(self): + """Return the unique ID.""" + return self.simplisafe.location_id + @property def name(self): """Return the name of the device.""" if self._name is not None: return self._name - return 'Alarm {}'.format(self.simplisafe.location_id()) + return 'Alarm {}'.format(self.simplisafe.location_id) @property def code_format(self): @@ -94,12 +89,13 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel): @property def state(self): """Return the state of the device.""" - status = self.simplisafe.state() - if status == 'off': + status = self.simplisafe.state + if status.lower() == 'off': state = STATE_ALARM_DISARMED - elif status == 'home': + elif status.lower() == 'home' or status.lower() == 'home_count': state = STATE_ALARM_ARMED_HOME - elif status == 'away': + elif (status.lower() == 'away' or status.lower() == 'exitDelay' or + status.lower() == 'away_count'): state = STATE_ALARM_ARMED_AWAY else: state = STATE_UNKNOWN @@ -108,14 +104,13 @@ class SimpliSafeAlarm(alarm.AlarmControlPanel): @property def device_state_attributes(self): """Return the state attributes.""" - return { - 'alarm': self.simplisafe.alarm(), - 'co': self.simplisafe.carbon_monoxide(), - 'fire': self.simplisafe.fire(), - 'flood': self.simplisafe.flood(), - 'last_event': self.simplisafe.last_event(), - 'temperature': self.simplisafe.temperature(), - } + attributes = {} + + attributes[ATTR_ALARM_ACTIVE] = self.simplisafe.alarm_active + if self.simplisafe.temperature is not None: + attributes[ATTR_TEMPERATURE] = self.simplisafe.temperature + + return attributes def update(self): """Update alarm status.""" diff --git a/requirements_all.txt b/requirements_all.txt index 36ad3cdc81d..309a8c7c1b6 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1241,7 +1241,7 @@ shodan==1.8.1 simplepush==1.1.4 # homeassistant.components.alarm_control_panel.simplisafe -simplisafe-python==1.0.5 +simplisafe-python==2.0.2 # homeassistant.components.skybell skybellpy==0.1.2