Added handling for connection errors in state update, added available property (#27794)

This commit is contained in:
Tiit Rätsep 2019-10-18 03:23:11 +03:00 committed by Paulus Schoutsen
parent 86a4be1636
commit 81178661ae

View File

@ -3,6 +3,7 @@ import logging
import voluptuous as vol import voluptuous as vol
from api.soma_api import SomaApi from api.soma_api import SomaApi
from requests import RequestException
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant import config_entries from homeassistant import config_entries
@ -75,6 +76,12 @@ class SomaEntity(Entity):
self.device = device self.device = device
self.api = api self.api = api
self.current_position = 50 self.current_position = 50
self.is_available = True
@property
def available(self):
"""Return true if the last API commands returned successfully."""
return self.is_available
@property @property
def unique_id(self): def unique_id(self):
@ -100,12 +107,19 @@ class SomaEntity(Entity):
async def async_update(self): async def async_update(self):
"""Update the device with the latest data.""" """Update the device with the latest data."""
response = await self.hass.async_add_executor_job( try:
self.api.get_shade_state, self.device["mac"] response = await self.hass.async_add_executor_job(
) self.api.get_shade_state, self.device["mac"]
)
except RequestException:
_LOGGER.error("Connection to SOMA Connect failed")
self.is_available = False
return
if response["result"] != "success": if response["result"] != "success":
_LOGGER.error( _LOGGER.error(
"Unable to reach device %s (%s)", self.device["name"], response["msg"] "Unable to reach device %s (%s)", self.device["name"], response["msg"]
) )
self.is_available = False
return return
self.current_position = 100 - response["position"] self.current_position = 100 - response["position"]
self.is_available = True