Add support for available property for broadlink (#23981)

* Add support for available property for broadlink

* Broadlink, except oserror

* Broadlink, except oserror
This commit is contained in:
Daniel Høyer Iversen 2019-05-23 09:52:30 +02:00 committed by Pascal Vizeli
parent d2eb5bb0f3
commit 7f7435f003
2 changed files with 26 additions and 7 deletions

View File

@ -1,7 +1,6 @@
"""Support for the Broadlink RM2 Pro (only temperature) and A1 devices.""" """Support for the Broadlink RM2 Pro (only temperature) and A1 devices."""
import binascii import binascii
import logging import logging
import socket
from datetime import timedelta from datetime import timedelta
import voluptuous as vol import voluptuous as vol
@ -60,6 +59,7 @@ class BroadlinkSensor(Entity):
"""Initialize the sensor.""" """Initialize the sensor."""
self._name = '{} {}'.format(name, SENSOR_TYPES[sensor_type][0]) self._name = '{} {}'.format(name, SENSOR_TYPES[sensor_type][0])
self._state = None self._state = None
self._is_available = False
self._type = sensor_type self._type = sensor_type
self._broadlink_data = broadlink_data self._broadlink_data = broadlink_data
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
@ -74,6 +74,11 @@ class BroadlinkSensor(Entity):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._state return self._state
@property
def available(self):
"""Return True if entity is available."""
return self._is_available
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return the unit this state is expressed in.""" """Return the unit this state is expressed in."""
@ -83,8 +88,11 @@ class BroadlinkSensor(Entity):
"""Get the latest data from the sensor.""" """Get the latest data from the sensor."""
self._broadlink_data.update() self._broadlink_data.update()
if self._broadlink_data.data is None: if self._broadlink_data.data is None:
self._state = None
self._is_available = False
return return
self._state = self._broadlink_data.data[self._type] self._state = self._broadlink_data.data[self._type]
self._is_available = True
class BroadlinkData: class BroadlinkData:
@ -119,8 +127,9 @@ class BroadlinkData:
if data is not None: if data is not None:
self.data = self._schema(data) self.data = self._schema(data)
return return
except socket.timeout as error: except OSError as error:
if retry < 1: if retry < 1:
self.data = None
_LOGGER.error(error) _LOGGER.error(error)
return return
except (vol.Invalid, vol.MultipleInvalid): except (vol.Invalid, vol.MultipleInvalid):
@ -131,7 +140,7 @@ class BroadlinkData:
def _auth(self, retry=3): def _auth(self, retry=3):
try: try:
auth = self._device.auth() auth = self._device.auth()
except socket.timeout: except OSError:
auth = False auth = False
if not auth and retry > 0: if not auth and retry > 0:
self._connect() self._connect()

View File

@ -110,7 +110,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
broadlink_device.timeout = config.get(CONF_TIMEOUT) broadlink_device.timeout = config.get(CONF_TIMEOUT)
try: try:
broadlink_device.auth() broadlink_device.auth()
except socket.timeout: except OSError:
_LOGGER.error("Failed to connect to device") _LOGGER.error("Failed to connect to device")
add_entities(switches) add_entities(switches)
@ -127,6 +127,7 @@ class BroadlinkRMSwitch(SwitchDevice, RestoreEntity):
self._command_on = command_on self._command_on = command_on
self._command_off = command_off self._command_off = command_off
self._device = device self._device = device
self._is_available = False
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Call when entity about to be added to hass.""" """Call when entity about to be added to hass."""
@ -145,6 +146,11 @@ class BroadlinkRMSwitch(SwitchDevice, RestoreEntity):
"""Return true if unable to access real state of entity.""" """Return true if unable to access real state of entity."""
return True return True
@property
def available(self):
"""Return True if entity is available."""
return not self.should_poll or self._is_available
@property @property
def should_poll(self): def should_poll(self):
"""Return the polling state.""" """Return the polling state."""
@ -174,7 +180,7 @@ class BroadlinkRMSwitch(SwitchDevice, RestoreEntity):
return True return True
try: try:
self._device.send_data(packet) self._device.send_data(packet)
except (socket.timeout, ValueError) as error: except (ValueError, OSError) as error:
if retry < 1: if retry < 1:
_LOGGER.error("Error during sending a packet: %s", error) _LOGGER.error("Error during sending a packet: %s", error)
return False return False
@ -186,7 +192,7 @@ class BroadlinkRMSwitch(SwitchDevice, RestoreEntity):
def _auth(self, retry=2): def _auth(self, retry=2):
try: try:
auth = self._device.auth() auth = self._device.auth()
except socket.timeout: except OSError:
auth = False auth = False
if retry < 1: if retry < 1:
_LOGGER.error("Timeout during authorization") _LOGGER.error("Timeout during authorization")
@ -252,6 +258,7 @@ class BroadlinkSP2Switch(BroadlinkSP1Switch):
except (socket.timeout, ValueError) as error: except (socket.timeout, ValueError) as error:
if retry < 1: if retry < 1:
_LOGGER.error("Error during updating the state: %s", error) _LOGGER.error("Error during updating the state: %s", error)
self._is_available = False
return return
if not self._auth(): if not self._auth():
return return
@ -260,6 +267,7 @@ class BroadlinkSP2Switch(BroadlinkSP1Switch):
return self._update(retry-1) return self._update(retry-1)
self._state = state self._state = state
self._load_power = load_power self._load_power = load_power
self._is_available = True
class BroadlinkMP1Slot(BroadlinkRMSwitch): class BroadlinkMP1Slot(BroadlinkRMSwitch):
@ -285,10 +293,12 @@ class BroadlinkMP1Slot(BroadlinkRMSwitch):
except (socket.timeout, ValueError) as error: except (socket.timeout, ValueError) as error:
if retry < 1: if retry < 1:
_LOGGER.error("Error during sending a packet: %s", error) _LOGGER.error("Error during sending a packet: %s", error)
self._is_available = False
return False return False
if not self._auth(): if not self._auth():
return False return False
return self._sendpacket(packet, max(0, retry-1)) return self._sendpacket(packet, max(0, retry-1))
self._is_available = True
return True return True
@property @property
@ -338,7 +348,7 @@ class BroadlinkMP1Switch:
"""Authenticate the device.""" """Authenticate the device."""
try: try:
auth = self._device.auth() auth = self._device.auth()
except socket.timeout: except OSError:
auth = False auth = False
if not auth and retry > 0: if not auth and retry > 0:
return self._auth(retry-1) return self._auth(retry-1)