From 9f58e5d6eaed731be728596a5b06e981f604fe30 Mon Sep 17 00:00:00 2001 From: Bruno Furtado Date: Sun, 9 Feb 2020 00:17:41 +0000 Subject: [PATCH] Only auth on enter_learning in response to errors for broadlink (#27341) * Only auth on enter_learning in response to errors. * Remove extra newline. * Add missing break on successful attempt. * Avoid logging success message when auth is unsuccessful. --- .../components/broadlink/__init__.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/broadlink/__init__.py b/homeassistant/components/broadlink/__init__.py index dd7c02b82ad..be6aa266491 100644 --- a/homeassistant/components/broadlink/__init__.py +++ b/homeassistant/components/broadlink/__init__.py @@ -75,18 +75,20 @@ def async_setup_service(hass, host, device): async def _learn_command(call): """Learn a packet from remote.""" + device = hass.data[DOMAIN][call.data[CONF_HOST]] - try: - auth = await hass.async_add_executor_job(device.auth) - except socket.timeout: - _LOGGER.error("Failed to connect to device, timeout") - return - if not auth: - _LOGGER.error("Failed to connect to device") - return - - await hass.async_add_executor_job(device.enter_learning) + for retry in range(DEFAULT_RETRY): + try: + await hass.async_add_executor_job(device.enter_learning) + break + except (socket.timeout, ValueError): + try: + await hass.async_add_executor_job(device.auth) + except socket.timeout: + if retry == DEFAULT_RETRY - 1: + _LOGGER.error("Failed to enter learning mode") + return _LOGGER.info("Press the key you want Home Assistant to learn") start_time = utcnow()