Support wifi scan & join for bulbs using a different interface (#49)

This commit is contained in:
Teemu R 2020-04-24 16:57:04 +02:00 committed by GitHub
parent 0c71957aa8
commit 28c1811aef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 3 deletions

View File

@ -106,7 +106,7 @@ async def scan(dev):
@pass_dev @pass_dev
async def join(dev: SmartDevice, ssid, password, keytype): async def join(dev: SmartDevice, ssid, password, keytype):
"""Join the given wifi network.""" """Join the given wifi network."""
click.echo("Asking the device to connect to {ssid}.." % (ssid)) click.echo(f"Asking the device to connect to {ssid}..")
res = await dev.wifi_join(ssid, password, keytype=keytype) res = await dev.wifi_join(ssid, password, keytype=keytype)
click.echo( click.echo(
f"Response: {res} - if the device is not able to join the network, it will revert back to its previous state." f"Response: {res} - if the device is not able to join the network, it will revert back to its previous state."

View File

@ -40,6 +40,11 @@ class WifiNetwork:
ssid: str ssid: str
key_type: int key_type: int
# These are available only on softaponboarding
cipher_type: Optional[int] = None
bssid: Optional[str] = None
channel: Optional[int] = None
rssi: Optional[int] = None
class SmartDeviceException(Exception): class SmartDeviceException(Exception):
@ -592,7 +597,18 @@ class SmartDevice:
async def wifi_scan(self) -> List[WifiNetwork]: async def wifi_scan(self) -> List[WifiNetwork]:
"""Scan for available wifi networks.""" """Scan for available wifi networks."""
info = await self._query_helper("netif", "get_scaninfo", {"refresh": 1})
async def _scan(target):
return await self._query_helper(target, "get_scaninfo", {"refresh": 1})
try:
info = await _scan("netif")
except SmartDeviceException as ex:
_LOGGER.debug(
"Unable to scan using 'netif', retrying with 'softaponboarding': %s", ex
)
info = await _scan("smartlife.iot.common.softaponboarding")
if "ap_list" not in info: if "ap_list" not in info:
raise SmartDeviceException("Invalid response for wifi scan: %s" % info) raise SmartDeviceException("Invalid response for wifi scan: %s" % info)
@ -603,8 +619,18 @@ class SmartDevice:
If joining the network fails, the device will return to AP mode after a while. If joining the network fails, the device will return to AP mode after a while.
""" """
async def _join(target, payload):
return await self._query_helper(target, "set_stainfo", payload)
payload = {"ssid": ssid, "password": password, "key_type": keytype} payload = {"ssid": ssid, "password": password, "key_type": keytype}
return await self._query_helper("netif", "set_stainfo", payload) try:
return await _join("netif", payload)
except SmartDeviceException as ex:
_LOGGER.debug(
"Unable to join using 'netif', retrying with 'softaponboarding': %s", ex
)
return await _join("smartlife.iot.common.softaponboarding", payload)
@property @property
def device_type(self) -> DeviceType: def device_type(self) -> DeviceType: