Catch thethingsnetwork TypeError (#23667)

* Catch TypeError (fixes #23097)

* Re-add return values

* Update homeassistant/components/thethingsnetwork/sensor.py

Co-Authored-By: fabaff <mail@fabian-affolter.ch>
This commit is contained in:
Fabian Affolter 2019-05-06 09:47:46 +02:00 committed by GitHub
parent cf03e42773
commit b331b081f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,7 +45,7 @@ async def async_setup_platform(
success = await ttn_data_storage.async_update() success = await ttn_data_storage.async_update()
if not success: if not success:
return False return
devices = [] devices = []
for value, unit_of_measurement in values.items(): for value, unit_of_measurement in values.items():
@ -78,8 +78,9 @@ class TtnDataSensor(Entity):
if self._ttn_data_storage.data is not None: if self._ttn_data_storage.data is not None:
try: try:
return round(self._state[self._value], 1) return round(self._state[self._value], 1)
except KeyError: except (KeyError, TypeError):
pass return None
return None
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
@ -124,32 +125,32 @@ class TtnDataStorage:
try: try:
session = async_get_clientsession(self._hass) session = async_get_clientsession(self._hass)
with async_timeout.timeout(DEFAULT_TIMEOUT, loop=self._hass.loop): with async_timeout.timeout(DEFAULT_TIMEOUT, loop=self._hass.loop):
req = await session.get(self._url, headers=self._headers) response = await session.get(self._url, headers=self._headers)
except (asyncio.TimeoutError, aiohttp.ClientError): except (asyncio.TimeoutError, aiohttp.ClientError):
_LOGGER.error("Error while accessing: %s", self._url) _LOGGER.error("Error while accessing: %s", self._url)
return False return None
status = req.status status = response.status
if status == 204: if status == 204:
_LOGGER.error("The device is not available: %s", self._device_id) _LOGGER.error("The device is not available: %s", self._device_id)
return False return None
if status == 401: if status == 401:
_LOGGER.error( _LOGGER.error(
"Not authorized for Application ID: %s", self._app_id) "Not authorized for Application ID: %s", self._app_id)
return False return None
if status == 404: if status == 404:
_LOGGER.error("Application ID is not available: %s", self._app_id) _LOGGER.error("Application ID is not available: %s", self._app_id)
return False return None
data = await req.json() data = await response.json()
self.data = data[-1] self.data = data[-1]
for value in self._values.items(): for value in self._values.items():
if value[0] not in self.data.keys(): if value[0] not in self.data.keys():
_LOGGER.warning("Value not available: %s", value[0]) _LOGGER.warning("Value not available: %s", value[0])
return req return response