Merge pull request #5022 from home-assistant/broadlink_bug_fix
Solve some bugs in the broadlink switchpull/5033/head
commit
39bdd5310b
|
@ -57,22 +57,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
config.get(CONF_MAC).encode().replace(b':', b''))
|
config.get(CONF_MAC).encode().replace(b':', b''))
|
||||||
sensor_type = config.get(CONF_TYPE)
|
sensor_type = config.get(CONF_TYPE)
|
||||||
|
|
||||||
if sensor_type == "rm":
|
|
||||||
broadlink_device = broadlink.rm((ip_addr, 80), mac_addr)
|
|
||||||
switch = BroadlinkRMSwitch
|
|
||||||
elif sensor_type == "sp1":
|
|
||||||
broadlink_device = broadlink.sp1((ip_addr, 80), mac_addr)
|
|
||||||
switch = BroadlinkSP1Switch
|
|
||||||
elif sensor_type == "sp2":
|
|
||||||
broadlink_device = broadlink.sp2((ip_addr, 80), mac_addr)
|
|
||||||
switch = BroadlinkSP2Switch
|
|
||||||
|
|
||||||
broadlink_device.timeout = config.get(CONF_TIMEOUT)
|
|
||||||
try:
|
|
||||||
broadlink_device.auth()
|
|
||||||
except socket.timeout:
|
|
||||||
_LOGGER.error("Failed to connect to device.")
|
|
||||||
|
|
||||||
persistent_notification = loader.get_component('persistent_notification')
|
persistent_notification = loader.get_component('persistent_notification')
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
|
@ -103,7 +87,24 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
persistent_notification.async_create(hass,
|
persistent_notification.async_create(hass,
|
||||||
"Did not received any signal",
|
"Did not received any signal",
|
||||||
title='Broadlink switch')
|
title='Broadlink switch')
|
||||||
hass.services.register(DOMAIN, SERVICE_LEARN, _learn_command)
|
|
||||||
|
if sensor_type == "rm":
|
||||||
|
broadlink_device = broadlink.rm((ip_addr, 80), mac_addr)
|
||||||
|
switch = BroadlinkRMSwitch
|
||||||
|
hass.services.register(DOMAIN, SERVICE_LEARN + '_' + ip_addr,
|
||||||
|
_learn_command)
|
||||||
|
elif sensor_type == "sp1":
|
||||||
|
broadlink_device = broadlink.sp1((ip_addr, 80), mac_addr)
|
||||||
|
switch = BroadlinkSP1Switch
|
||||||
|
elif sensor_type == "sp2":
|
||||||
|
broadlink_device = broadlink.sp2((ip_addr, 80), mac_addr)
|
||||||
|
switch = BroadlinkSP2Switch
|
||||||
|
|
||||||
|
broadlink_device.timeout = config.get(CONF_TIMEOUT)
|
||||||
|
try:
|
||||||
|
broadlink_device.auth()
|
||||||
|
except socket.timeout:
|
||||||
|
_LOGGER.error("Failed to connect to device.")
|
||||||
|
|
||||||
for object_id, device_config in devices.items():
|
for object_id, device_config in devices.items():
|
||||||
switches.append(
|
switches.append(
|
||||||
|
@ -153,11 +154,13 @@ class BroadlinkRMSwitch(SwitchDevice):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
if self._sendpacket(self._command_on):
|
if self._sendpacket(self._command_on):
|
||||||
self._state = True
|
self._state = True
|
||||||
|
self.update_ha_state()
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
if self._sendpacket(self._command_off):
|
if self._sendpacket(self._command_off):
|
||||||
self._state = False
|
self._state = False
|
||||||
|
self.update_ha_state()
|
||||||
|
|
||||||
def _sendpacket(self, packet, retry=2):
|
def _sendpacket(self, packet, retry=2):
|
||||||
"""Send packet to device."""
|
"""Send packet to device."""
|
||||||
|
@ -166,7 +169,7 @@ class BroadlinkRMSwitch(SwitchDevice):
|
||||||
return True
|
return True
|
||||||
try:
|
try:
|
||||||
self._device.send_data(packet)
|
self._device.send_data(packet)
|
||||||
except socket.timeout as error:
|
except (socket.timeout, ValueError) as error:
|
||||||
if retry < 1:
|
if retry < 1:
|
||||||
_LOGGER.error(error)
|
_LOGGER.error(error)
|
||||||
return False
|
return False
|
||||||
|
@ -191,7 +194,7 @@ class BroadlinkSP1Switch(BroadlinkRMSwitch):
|
||||||
"""Send packet to device."""
|
"""Send packet to device."""
|
||||||
try:
|
try:
|
||||||
self._device.set_power(packet)
|
self._device.set_power(packet)
|
||||||
except socket.timeout as error:
|
except (socket.timeout, ValueError) as error:
|
||||||
if retry < 1:
|
if retry < 1:
|
||||||
_LOGGER.error(error)
|
_LOGGER.error(error)
|
||||||
return False
|
return False
|
||||||
|
@ -227,7 +230,7 @@ class BroadlinkSP2Switch(BroadlinkSP1Switch):
|
||||||
def _update(self, retry=2):
|
def _update(self, retry=2):
|
||||||
try:
|
try:
|
||||||
state = self._device.check_power()
|
state = self._device.check_power()
|
||||||
except socket.timeout as error:
|
except (socket.timeout, ValueError) as error:
|
||||||
if retry < 1:
|
if retry < 1:
|
||||||
_LOGGER.error(error)
|
_LOGGER.error(error)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue