From b8e9e3af2f40b15be52079e615ae72dca33f319f Mon Sep 17 00:00:00 2001 From: shred86 <32663154+shred86@users.noreply.github.com> Date: Fri, 3 Apr 2020 09:08:32 -0700 Subject: [PATCH] Add Abode entity available property (#32923) * Add available property and raise exception * Add entity available property * Refactoring and fixes * Refactoring and fix for multi sensor * Bump abodepy version --- homeassistant/components/abode/__init__.py | 92 +++++++++++++------- homeassistant/components/abode/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 4 files changed, 63 insertions(+), 35 deletions(-) diff --git a/homeassistant/components/abode/__init__.py b/homeassistant/components/abode/__init__.py index 687d0d31263..9ecd2ffd155 100644 --- a/homeassistant/components/abode/__init__.py +++ b/homeassistant/components/abode/__init__.py @@ -19,6 +19,7 @@ from homeassistant.const import ( CONF_USERNAME, EVENT_HOMEASSISTANT_STOP, ) +from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import config_validation as cv from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.helpers.entity import Entity @@ -119,7 +120,7 @@ async def async_setup_entry(hass, config_entry): except (AbodeException, ConnectTimeout, HTTPError) as ex: LOGGER.error("Unable to connect to Abode: %s", str(ex)) - return False + raise ConfigEntryNotReady for platform in ABODE_PLATFORMS: hass.async_create_task( @@ -271,36 +272,72 @@ def setup_abode_events(hass): ) -class AbodeDevice(Entity): - """Representation of an Abode device.""" +class AbodeEntity(Entity): + """Representation of an Abode entity.""" - def __init__(self, data, device): - """Initialize Abode device.""" + def __init__(self, data): + """Initialize Abode entity.""" self._data = data - self._device = device + self._available = True - async def async_added_to_hass(self): - """Subscribe to device events.""" - self.hass.async_add_job( - self._data.abode.events.add_device_callback, - self._device.device_id, - self._update_callback, - ) - self.hass.data[DOMAIN].entity_ids.add(self.entity_id) - - async def async_will_remove_from_hass(self): - """Unsubscribe from device events.""" - self.hass.async_add_job( - self._data.abode.events.remove_all_device_callbacks, self._device.device_id - ) + @property + def available(self): + """Return the available state.""" + return self._available @property def should_poll(self): """Return the polling state.""" return self._data.polling + async def async_added_to_hass(self): + """Subscribe to Abode connection status updates.""" + self.hass.async_add_job( + self._data.abode.events.add_connection_status_callback, + self.unique_id, + self._update_connection_status, + ) + + self.hass.data[DOMAIN].entity_ids.add(self.entity_id) + + async def async_will_remove_from_hass(self): + """Unsubscribe from Abode connection status updates.""" + self.hass.async_add_job( + self._data.abode.events.remove_connection_status_callback, self.unique_id, + ) + + def _update_connection_status(self): + """Update the entity available property.""" + self._available = self._data.abode.events.connected + self.schedule_update_ha_state() + + +class AbodeDevice(AbodeEntity): + """Representation of an Abode device.""" + + def __init__(self, data, device): + """Initialize Abode device.""" + super().__init__(data) + self._device = device + + async def async_added_to_hass(self): + """Subscribe to device events.""" + await super().async_added_to_hass() + self.hass.async_add_job( + self._data.abode.events.add_device_callback, + self._device.device_id, + self._update_callback, + ) + + async def async_will_remove_from_hass(self): + """Unsubscribe from device events.""" + await super().async_will_remove_from_hass() + self.hass.async_add_job( + self._data.abode.events.remove_all_device_callbacks, self._device.device_id + ) + def update(self): - """Update device and automation states.""" + """Update device state.""" self._device.refresh() @property @@ -339,23 +376,14 @@ class AbodeDevice(Entity): self.schedule_update_ha_state() -class AbodeAutomation(Entity): +class AbodeAutomation(AbodeEntity): """Representation of an Abode automation.""" def __init__(self, data, automation): """Initialize for Abode automation.""" - self._data = data + super().__init__(data) self._automation = automation - async def async_added_to_hass(self): - """Set up automation entity.""" - self.hass.data[DOMAIN].entity_ids.add(self.entity_id) - - @property - def should_poll(self): - """Return the polling state.""" - return self._data.polling - def update(self): """Update automation state.""" self._automation.refresh() diff --git a/homeassistant/components/abode/manifest.json b/homeassistant/components/abode/manifest.json index eabd4a7f74f..51503142692 100644 --- a/homeassistant/components/abode/manifest.json +++ b/homeassistant/components/abode/manifest.json @@ -3,7 +3,7 @@ "name": "Abode", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/abode", - "requirements": ["abodepy==0.18.1"], + "requirements": ["abodepy==0.19.0"], "dependencies": [], "codeowners": ["@shred86"] } diff --git a/requirements_all.txt b/requirements_all.txt index 358d8aa62e2..0805d384c99 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -107,7 +107,7 @@ WazeRouteCalculator==0.12 YesssSMS==0.4.1 # homeassistant.components.abode -abodepy==0.18.1 +abodepy==0.19.0 # homeassistant.components.mcp23017 adafruit-blinka==3.9.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 4a6308c8908..8f62b8427b7 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -26,7 +26,7 @@ RtmAPI==0.7.2 YesssSMS==0.4.1 # homeassistant.components.abode -abodepy==0.18.1 +abodepy==0.19.0 # homeassistant.components.androidtv adb-shell==0.1.1