Skip to content

Commit

Permalink
Improved discovery logging
Browse files Browse the repository at this point in the history
  • Loading branch information
beveradb committed Jan 31, 2019
1 parent e48a808 commit 85dc5a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
37 changes: 29 additions & 8 deletions pysonofflan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def cli(ctx, host, device_id, inching):
return

if host is None:
logger.info(
"No host name given - try discovery mode to find your device!")
logger.error("No host name given, see usage below:")
click.echo(ctx.get_help())
sys.exit(1)

ctx.obj = {"host": host, "inching": inching}
Expand All @@ -93,8 +93,7 @@ def discover():
found_devices = asyncio.get_event_loop().run_until_complete(
Discover.discover()).items()
for ip, found_device_id in found_devices:
logger.info("Found Sonoff LAN Mode device at IP %s with ID: %s" % (
ip, found_device_id))
logger.info("Found Sonoff LAN Mode device at IP %s" % ip)

return found_devices

Expand All @@ -106,11 +105,33 @@ def find_host_from_device_id(device_id):
"on local network, please wait..." % device_id)
found_devices = asyncio.get_event_loop().run_until_complete(
Discover.discover()).items()
for ip, found_device_id in found_devices:
logger.info("Found Sonoff LAN Mode device at IP %s with ID: %s"
% (ip, found_device_id))
if found_device_id.lower() == device_id.lower():
for ip, _ in found_devices:
logger.info("Found Sonoff LAN Mode device at IP %s, attempting to "
"read state to get device ID" % ip)

shared_state = {'device_id_at_current_ip': None}

async def device_id_callback(device: SonoffSwitch):
if device.basic_info is not None:
device.shared_state['device_id_at_current_ip'] = \
device.device_id
device.keep_running = False

SonoffSwitch(
host=ip,
callback_after_update=device_id_callback,
shared_state=shared_state,
logger=logger
)

current_device_id = shared_state['device_id_at_current_ip']

if device_id.lower() == current_device_id.lower():
return ip
else:
logger.info(
"Found device ID %s which did not match" % current_device_id
)
return None


Expand Down
16 changes: 8 additions & 8 deletions pysonofflan/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
from itertools import chain
from typing import Dict

_LOGGER = logging.getLogger(__name__)


class Discover:
@staticmethod
async def discover() -> Dict[str, str]:
async def discover(logger=None) -> Dict[str, str]:
"""
Attempts websocket connection on port 8081 to all IP addresses on
common home IP subnets: 192.168.0.X and 192.168.1.X, in the hope of
Expand All @@ -18,9 +16,11 @@ async def discover() -> Dict[str, str]:
:rtype: dict
:return: Array of devices {"ip": "device_id"}
"""
if logger is None:
logger = logging.getLogger(__name__)

_LOGGER.debug("Attempting connection to all IPs on local network. "
"This will take approximately 1 minute, please wait...")
logger.debug("Attempting connection to all IPs on local network. "
"This will take approximately 1 minute, please wait...")
devices = {}

try:
Expand All @@ -31,14 +31,14 @@ async def discover() -> Dict[str, str]:
)

for ip in local_ip_ranges:
_LOGGER.debug("Attempting connection to IP: %s" % ip)
logger.debug("Attempting connection to IP: %s" % ip)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(0.1)
result = sock.connect_ex((str(ip), 8081))
if result == 0:
_LOGGER.info("Found open 8081 port at local IP: %s" % ip)
logger.debug("Found open 8081 port at local IP: %s" % ip)
devices[ip] = ip
except Exception as ex:
_LOGGER.error("Caught Exception: %s" % ex, exc_info=False)
logger.error("Caught Exception: %s" % ex, exc_info=False)

return devices

0 comments on commit 85dc5a0

Please sign in to comment.