diff --git a/homeassistant/components/switch/broadlink.py b/homeassistant/components/switch/broadlink.py index 7c561a3eb1f..52a5c1e4e80 100644 --- a/homeassistant/components/switch/broadlink.py +++ b/homeassistant/components/switch/broadlink.py @@ -28,7 +28,9 @@ _LOGGER = logging.getLogger(__name__) DOMAIN = "broadlink" DEFAULT_NAME = 'Broadlink switch' DEFAULT_TIMEOUT = 10 +DEFAULT_RETRY = 3 SERVICE_LEARN = "learn_command" +SERVICE_SEND = "send_packet" RM_TYPES = ["rm", "rm2", "rm_mini", "rm_pro_phicomm", "rm2_home_plus", "rm2_home_plus_gdt", "rm2_pro_plus", "rm2_pro_plus2", @@ -101,10 +103,30 @@ def setup_platform(hass, config, add_devices, discovery_info=None): "Did not received any signal", title='Broadlink switch') + @asyncio.coroutine + def _send_packet(call): + packets = call.data.get('packet', []) + for packet in packets: + for retry in range(DEFAULT_RETRY): + try: + payload = b64decode(packet) + yield from hass.loop.run_in_executor( + None, broadlink_device.send_data, payload) + break + except (socket.timeout, ValueError): + try: + yield from hass.loop.run_in_executor( + None, broadlink_device.auth) + except socket.timeout: + if retry == DEFAULT_RETRY-1: + _LOGGER.error("Failed to send packet to device.") + if switch_type in RM_TYPES: broadlink_device = broadlink.rm((ip_addr, 80), mac_addr) - hass.services.register(DOMAIN, SERVICE_LEARN + '_' + ip_addr, - _learn_command) + hass.services.register(DOMAIN, SERVICE_LEARN + '_' + + ip_addr.replace('.', '_'), _learn_command) + hass.services.register(DOMAIN, SERVICE_SEND + '_' + + ip_addr.replace('.', '_'), _send_packet) switches = [] for object_id, device_config in devices.items(): switches.append(