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."""
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:
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()

View File

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

View File

@@ -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__)

View File

@@ -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": {