Skip to content

Commit

Permalink
Allow disabling the bellows UART thread (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Nov 9, 2022
1 parent 3050e0f commit e74aa85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
2 changes: 2 additions & 0 deletions bellows/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

CONF_DEVICE_BAUDRATE = "baudrate"
CONF_USE_THREAD = "use_thread"
CONF_EZSP_CONFIG = "ezsp_config"
CONF_EZSP_POLICIES = "ezsp_policies"
CONF_PARAM_MAX_WATCHDOG_FAILURES = "max_watchdog_failures"
Expand All @@ -42,6 +43,7 @@
vol.Optional(CONF_EZSP_POLICIES, default={}): vol.Schema(
{vol.Optional(str): int}
),
vol.Optional(CONF_USE_THREAD, default=True): cv_boolean,
}
)

Expand Down
32 changes: 16 additions & 16 deletions bellows/ezsp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@

import zigpy.config

from bellows.config import (
CONF_DEVICE,
CONF_DEVICE_BAUDRATE,
CONF_DEVICE_PATH,
SCHEMA_DEVICE,
)
import bellows.config as conf
from bellows.exception import EzspError
import bellows.types as t
import bellows.uart
Expand Down Expand Up @@ -55,15 +50,18 @@ def __init__(self, device_config: Dict):
@classmethod
async def probe(cls, device_config: Dict) -> bool | dict[str, int | str | bool]:
"""Probe port for the device presence."""
for config in ({**device_config, CONF_DEVICE_BAUDRATE: 115200}, device_config):
ezsp = cls(SCHEMA_DEVICE(config))
for config in (
{**device_config, conf.CONF_DEVICE_BAUDRATE: 115200},
device_config,
):
ezsp = cls(conf.SCHEMA_DEVICE(config))
try:
await asyncio.wait_for(ezsp._probe(), timeout=PROBE_TIMEOUT)
return config
except Exception as exc:
LOGGER.debug(
"Unsuccessful radio probe of '%s' port",
device_config[CONF_DEVICE_PATH],
device_config[conf.CONF_DEVICE_PATH],
exc_info=exc,
)
finally:
Expand All @@ -73,14 +71,14 @@ async def probe(cls, device_config: Dict) -> bool | dict[str, int | str | bool]:

async def _probe(self) -> None:
"""Open port and try sending a command"""
await self.connect()
await self.connect(use_thread=False)
await self._startup_reset()
await self.version()

async def _startup_reset(self):
"""Start EZSP and reset the stack."""
# `zigbeed` resets on startup
parsed_path = urllib.parse.urlparse(self._config[CONF_DEVICE_PATH])
parsed_path = urllib.parse.urlparse(self._config[conf.CONF_DEVICE_PATH])
if parsed_path.scheme == "socket":
try:
await asyncio.wait_for(
Expand All @@ -99,8 +97,8 @@ async def _startup_reset(self):
@classmethod
async def initialize(cls, zigpy_config: Dict) -> "EZSP":
"""Return initialized EZSP instance."""
ezsp = cls(zigpy_config[CONF_DEVICE])
await ezsp.connect()
ezsp = cls(zigpy_config[conf.CONF_DEVICE])
await ezsp.connect(use_thread=zigpy_config[conf.CONF_USE_THREAD])

try:
await ezsp._startup_reset()
Expand All @@ -115,9 +113,9 @@ async def initialize(cls, zigpy_config: Dict) -> "EZSP":

return ezsp

async def connect(self) -> None:
async def connect(self, *, use_thread: bool = True) -> None:
assert self._gw is None
self._gw = await bellows.uart.connect(self._config, self)
self._gw = await bellows.uart.connect(self._config, self, use_thread=use_thread)
self._protocol = v4.EZSPv4(self.handle_callback, self._gw)

async def reset(self):
Expand Down Expand Up @@ -241,7 +239,9 @@ def cb(frame_name: str, response: List) -> None:
def connection_lost(self, exc):
"""Lost serial connection."""
LOGGER.debug(
"%s connection lost unexpectedly: %s", self._config[CONF_DEVICE_PATH], exc
"%s connection lost unexpectedly: %s",
self._config[conf.CONF_DEVICE_PATH],
exc,
)
self.enter_failed_state(f"Serial connection loss: {exc!r}")

Expand Down

0 comments on commit e74aa85

Please sign in to comment.