Improve discovery error output (#134)

This commit is contained in:
Pierre Ståhl
2020-11-17 12:26:18 +01:00
committed by GitHub
parent 3e52811aac
commit c8a44019fa
4 changed files with 34 additions and 24 deletions

View File

@@ -1,4 +1,5 @@
"""Config flow for LocalTuya integration integration.""" """Config flow for LocalTuya integration integration."""
import errno
import logging import logging
from importlib import import_module from importlib import import_module
@@ -16,8 +17,8 @@ from homeassistant.const import (
from homeassistant.core import callback from homeassistant.core import callback
from . import pytuya from . import pytuya
from .const import ( # pylint: disable=unused-import from .const import CONF_DPS_STRINGS # pylint: disable=unused-import
CONF_DPS_STRINGS, from .const import (
CONF_LOCAL_KEY, CONF_LOCAL_KEY,
CONF_PROTOCOL_VERSION, CONF_PROTOCOL_VERSION,
DATA_DISCOVERY, DATA_DISCOVERY,
@@ -202,7 +203,7 @@ class LocaltuyaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self.entities = [] self.entities = []
async def async_step_user(self, user_input=None): async def async_step_user(self, user_input=None):
"""Handle initial step.""" """Handle the initial step."""
errors = {} errors = {}
if user_input is not None: if user_input is not None:
if user_input[DISCOVERED_DEVICE] != CUSTOM_DEVICE: 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() return await self.async_step_basic_info()
# Use cache if available or fallback to manual discovery # Use cache if available or fallback to manual discovery
if DOMAIN in self.hass.data: devices = {}
devices = self.hass.data[DOMAIN][DATA_DISCOVERY].devices data = self.hass.data.get(DOMAIN)
if data and DATA_DISCOVERY in data:
devices = data[DATA_DISCOVERY].devices
else: 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 = { self.devices = {
ip: dev ip: dev
for ip, dev in devices.items() for ip, dev in devices.items()

View File

@@ -55,7 +55,7 @@ class TuyaDiscovery(asyncio.DatagramProtocol):
def close(self): def close(self):
"""Stop discovery.""" """Stop discovery."""
self.callback = None self.callback = None
for transport, _ in self.listeners: for transport, _ in self._listeners:
transport.close() transport.close()
def datagram_received(self, data, addr): def datagram_received(self, data, addr):
@@ -85,8 +85,6 @@ async def discover():
try: try:
await discover.start() await discover.start()
await asyncio.sleep(DEFAULT_TIMEOUT) await asyncio.sleep(DEFAULT_TIMEOUT)
except Exception:
_LOGGER.exception("failed to discover devices")
finally: finally:
discover.close() discover.close()
return discover.devices return discover.devices

View File

@@ -3,28 +3,25 @@ import logging
from functools import partial from functools import partial
import voluptuous as vol import voluptuous as vol
from homeassistant.components.fan import ( from homeassistant.components.fan import (
FanEntity,
DOMAIN, DOMAIN,
SPEED_OFF, SPEED_HIGH,
SPEED_LOW, SPEED_LOW,
SPEED_MEDIUM, SPEED_MEDIUM,
SPEED_HIGH, SPEED_OFF,
SUPPORT_SET_SPEED,
SUPPORT_OSCILLATE, 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 .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__) _LOGGER = logging.getLogger(__name__)

View File

@@ -7,7 +7,9 @@
"cannot_connect": "Cannot connect to device. Verify that address is correct and try again.", "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.", "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.", "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": { "step": {
"user": { "user": {