- added send packet service to send ir packets directly from scripts (no need to define a switch for each command) (#5768)

- changed ip suffix of SERVICE_LEARN to use _ instead of . as ip seperator to avoid errors if used in yaml
pull/5774/head
Yannic-HAW 2017-02-06 07:53:58 +01:00 committed by Daniel Høyer Iversen
parent 0d89f2bc69
commit 4a341ff55f
1 changed files with 24 additions and 2 deletions

View File

@ -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(