From c8a44019fa5f6ffd685dbf9657abbb68e9369c1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20St=C3=A5hl?= Date: Tue, 17 Nov 2020 12:26:18 +0100 Subject: [PATCH] Improve discovery error output (#134) --- custom_components/localtuya/config_flow.py | 25 ++++++++++++++----- custom_components/localtuya/discovery.py | 4 +-- custom_components/localtuya/fan.py | 25 ++++++++----------- .../localtuya/translations/en.json | 4 ++- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index d41e80b..fed54d7 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -1,4 +1,5 @@ """Config flow for LocalTuya integration integration.""" +import errno import logging from importlib import import_module @@ -16,8 +17,8 @@ from homeassistant.const import ( from homeassistant.core import callback from . import pytuya -from .const import ( # pylint: disable=unused-import - CONF_DPS_STRINGS, +from .const import CONF_DPS_STRINGS # pylint: disable=unused-import +from .const import ( CONF_LOCAL_KEY, CONF_PROTOCOL_VERSION, DATA_DISCOVERY, @@ -202,7 +203,7 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self.entities = [] async def async_step_user(self, user_input=None): - """Handle initial step.""" + """Handle the initial step.""" errors = {} if user_input is not None: if user_input[DISCOVERED_DEVICE] != CUSTOM_DEVICE: @@ -210,10 +211,22 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): return await self.async_step_basic_info() # Use cache if available or fallback to manual discovery - if DOMAIN in self.hass.data: - devices = self.hass.data[DOMAIN][DATA_DISCOVERY].devices + devices = {} + data = self.hass.data.get(DOMAIN) + if data and DATA_DISCOVERY in data: + devices = data[DATA_DISCOVERY].devices else: - devices = await discover() + try: + devices = await discover() + except OSError as ex: + if ex.errno == errno.EADDRINUSE: + errors["base"] = "address_in_use" + else: + errors["base"] = "discovery_failed" + except Exception: + _LOGGER.exception("discovery failed") + errors["base"] = "discovery_failed" + self.devices = { ip: dev for ip, dev in devices.items() diff --git a/custom_components/localtuya/discovery.py b/custom_components/localtuya/discovery.py index 1480d75..93a41bd 100644 --- a/custom_components/localtuya/discovery.py +++ b/custom_components/localtuya/discovery.py @@ -55,7 +55,7 @@ class TuyaDiscovery(asyncio.DatagramProtocol): def close(self): """Stop discovery.""" self.callback = None - for transport, _ in self.listeners: + for transport, _ in self._listeners: transport.close() def datagram_received(self, data, addr): @@ -85,8 +85,6 @@ async def discover(): try: await discover.start() await asyncio.sleep(DEFAULT_TIMEOUT) - except Exception: - _LOGGER.exception("failed to discover devices") finally: discover.close() return discover.devices diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index bb3b796..beb4001 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -3,28 +3,25 @@ import logging from functools import partial import voluptuous as vol - from homeassistant.components.fan import ( - FanEntity, DOMAIN, - SPEED_OFF, + SPEED_HIGH, SPEED_LOW, SPEED_MEDIUM, - SPEED_HIGH, - SUPPORT_SET_SPEED, + SPEED_OFF, SUPPORT_OSCILLATE, + SUPPORT_SET_SPEED, + FanEntity, ) -from .const import ( - CONF_FAN_SPEED_CONTROL, - CONF_FAN_OSCILLATING_CONTROL, - CONF_FAN_SPEED_LOW, - CONF_FAN_SPEED_MEDIUM, - CONF_FAN_SPEED_HIGH, -) - - from .common import LocalTuyaEntity, async_setup_entry +from .const import ( + CONF_FAN_OSCILLATING_CONTROL, + CONF_FAN_SPEED_CONTROL, + CONF_FAN_SPEED_HIGH, + CONF_FAN_SPEED_LOW, + CONF_FAN_SPEED_MEDIUM, +) _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index 654906f..f171840 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -7,7 +7,9 @@ "cannot_connect": "Cannot connect to device. Verify that address is correct and try again.", "invalid_auth": "Failed to authenticate with device. Verify that device id and local key are correct.", "unknown": "An unknown error occurred. See log for details.", - "entity_already_configured": "Entity with this ID has already been configured." + "entity_already_configured": "Entity with this ID has already been configured.", + "address_in_use": "Address used for discovery is already in use. Make sure no other application is using it (TCP port 6668).", + "discovery_failed": "Something failed when discovering devices. See log for details." }, "step": { "user": {