Example #1
0
def discover_devices():

    devices = yield from Discover.discover()

    plugs = []

    for key, device in devices.items():

        try:
            if not has_resource(device.alias):
                if isinstance(device, SmartStrip):
                    for strip_child in device.children:
                        try:
                            plug = KasaPlug(strip_child.alias,
                                            device_name=strip_child.alias,
                                            device=strip_child,
                                            parent=device)
                            plugs.append(plug)
                        except ResourceNameAlreadyTaken:
                            pass

                else:
                    try:
                        plugs.append(
                            KasaPlug(device.alias,
                                     device_name=device.alias,
                                     device=device))
                    except ResourceNameAlreadyTaken:
                        pass
        except SmartDeviceException:
            print("SmartDeviceException with {}".format(
                device.host))
            continue

    return plugs
Example #2
0
def get_kasa_plug(alias):
    devices = Discover.discover()

    for key, device in devices.items():

        if device.alias == alias:
            return device
Example #3
0
 def discover_bulbs(self, bulb_one_name, bulb_two_name):
     bulbs = []
     found_devices = asyncio.run(Discover.discover())
     ip_addresses = [item for item in found_devices]
     for count, device in enumerate(found_devices.values()):
         if device.is_bulb:
             bulbs.append(ip_addresses[count])
     return bulbs
Example #4
0
def setup_device_info():
    devices = asyncio.run(Discover.discover())
    for addr, dev in devices.items():
        asyncio.run(dev.update())
        if dev.alias == device_name:
            print("Found device")
            f = open("device-ip.txt", "a")
            f.write(addr)
            f.close()
def discoverPlug():
    print("trying to discover plug")
    d = asyncio.run(Discover.discover())

    if len(d) != 0:
        for addr, dev in d.items():
            asyncio.run(dev.update())
            if dev.is_plug:
                return dev
    print("couldn't find the plug")
    return None
Example #6
0
def get_switch(switch_name: str):
    found_devices = asyncio.run(Discover.discover())
    device = [
        dev for addr, dev in found_devices.items() if dev.alias == switch_name
    ]
    if len(device) != 1:
        if switch_name not in [s['alias'] for s in SWITCHES]:
            raise ValueError("Invalid Name")
        s = [s for s in SWITCHES if s['alias'] == switch_name][0]
        switch = SmartPlug(s['ip'])
        asyncio.run(switch.update())
        return switch
    return device[0]
Example #7
0
def rediscover():
    devices = asyncio.run(Discover.discover())

    formattedDevices = {'devices': []}

    for addr, dev in devices.items():
        asyncio.run(dev.update())

        formattedDevices['devices'].append({
            'name': dev.alias,
            # 'host': dev.host,
            'address': addr,
            'state': dev.is_on,
            'sysinfo': dev.sys_info
        })
Example #8
0
def find_host_from_alias(alias,
                         target="255.255.255.255",
                         timeout=1,
                         attempts=3):
    """Discover a device identified by its alias."""
    host = None
    click.echo("Trying to discover %s using %s attempts of %s seconds" %
               (alias, attempts, timeout))
    for attempt in range(1, attempts):
        click.echo(f"Attempt {attempt} of {attempts}")
        found_devs = Discover.discover(target=target, timeout=timeout).items()
        for ip, dev in found_devs:
            if dev.alias.lower() == alias.lower():
                host = dev.host
                return host
    return None
Example #9
0
def discover(ctx, timeout, discover_only, dump_raw):
    """Discover devices in the network."""
    target = ctx.parent.params["target"]
    click.echo("Discovering devices for %s seconds" % timeout)
    found_devs = asyncio.run(
        Discover.discover(target=target, timeout=timeout, return_raw=dump_raw))
    if not discover_only:
        for ip, dev in found_devs.items():
            asyncio.run(dev.update())
            if dump_raw:
                click.echo(dev)
                continue
            ctx.obj = dev
            ctx.invoke(state)
            print()

    return found_devs
Example #10
0
def discover(detectedLights):
    logging.debug('Kasa discovery started')
    devices: dict = asyncio.run(Discover.discover(target="192.168.0.255"))

    for device in list(devices.keys()):
        x: KL430LightStrip = devices[device]
        asyncio.run(x.update())

        if x.model.startswith("KL430"):
            ip = device
            name = x.alias
            for x in range(1, 4):
                lightName = name
                protocol_cfg = {"ip": ip, "id": name, "light_nr": x, "model": "KL430", }
                protocol_cfg["points_capable"] = 7
                detectedLights.append({"protocol": "tpkasa", "name": lightName, "modelid": "LCX002",
                                       "protocol_cfg": protocol_cfg})
Example #11
0
    def __init__(self, channels, logger=None):
        """ Initializes connection to all TP Kasa smart plugs in the network.

        :channels: list of channels accessaable via this smart plug interface
        """

        # Instantiate log.
        self.log = LogHandler(logger=logger)

        self.channels = channels

        # Discover smart plugs.
        self.found_devices = asyncio.run(Discover.discover())

        # Store aliases of found devices.
        self.found_device_aliases = [
            dev.alias for dev in self.found_devices.values()
        ]

        self.log.info(
            f"Discovered {len(self.found_device_aliases)} smart plugs.")
Example #12
0
 def discover(self):
     LOGGER.info(
         f"start: {self.poly.network_interface['broadcast']} timout=10 discovery_packets=10"
     )
     self.devm = {}
     devices = asyncio.run(
         Discover.discover(timeout=10,
                           discovery_packets=10,
                           target=self.poly.network_interface['broadcast'],
                           on_discovered=self.discover_add_device))
     # make sure all we know about are added in case they didn't respond this time.
     for mac in self.polyConfig['customParams']:
         if not self.smac(mac) in self.devm:
             cfg = self.get_device_cfg(mac)
             if cfg is not None:
                 LOGGER.warning(
                     f"Adding previously known device that didn't respond to discover: {cfg}"
                 )
                 self.add_node(cfg=cfg)
     self.discover_done = True
     LOGGER.info("done")
Example #13
0
def create_device_from_ip_or_scan(ip, device_name):
    """Tries to create a kasa SmartDevice object either from a given IP address
    or by scanning the network."""
    device = None
    if ip:
        try:
            device = SmartPlug(ip)
            asyncio.run(device.update())
        except exceptions.SmartDeviceException:
            print(
                "Unable to connect to device at provided IP. Attempting scan.")

    # If unable to create device from ip, or ip is not given, scan the network.
    if not device:
        devices = asyncio.run(Discover.discover())
        for _, dev in devices.items():
            if dev.alias == device_name:
                asyncio.run(dev.update())
                device = dev

    return device
Example #14
0
def dump_discover(ctx, scrub):
    """Dump discovery information.

    Useful for dumping into a file to be added to the test suite.
    """
    target = ctx.parent.params["target"]
    keys_to_scrub = [
        "deviceId",
        "fwId",
        "hwId",
        "oemId",
        "mac",
        "latitude_i",
        "longitude_i",
        "latitude",
        "longitude",
    ]
    devs = asyncio.run(Discover.discover(target=target, return_raw=True))
    if scrub:
        click.echo("Scrubbing personal data before writing")
    for dev in devs.values():
        if scrub:
            for key in keys_to_scrub:
                if key in dev["system"]["get_sysinfo"]:
                    val = dev["system"]["get_sysinfo"][key]
                    if key in ["latitude_i", "longitude_i"]:
                        val = 0
                    else:
                        val = re.sub("\w", "0", val)
                    dev["system"]["get_sysinfo"][key] = val

        model = dev["system"]["get_sysinfo"]["model"]
        hw_version = dev["system"]["get_sysinfo"]["hw_ver"]
        save_to = f"{model}_{hw_version}.json"
        click.echo("Saving info to %s" % save_to)
        with open(save_to, "w") as f:
            json.dump(dev, f, sort_keys=True, indent=4)
            f.write("\n")
Example #15
0
 def discover(self):
     devices = asyncio.run(Discover.discover())
     for addr,dev in devices.items():
         asyncio.run(dev.update())
         devices.update({addr:dev})
     devices = self.sortdevices(devices)
     for addr, dev in devices.items():
         if dev.is_plug:
             asyncio.run(dev.update())
             notexisting = False
             if not self.plugs.get(addr,False):
                 notexisting = True
             plug = self.plugs.get(addr,SmartPlug(addr))
             is_on = False
             powerupdate = False
             if not notexisting:
                 is_on = plug.is_on
                 if plug.model.startswith("HS110"):
                     powerupdate = True
             asyncio.run(plug.update())
             if notexisting or powerupdate or is_on != plug.is_on:
                 self.updateview.append(addr)
             self.plugs.update({addr:plug})
Example #16
0
 def discover(self):
     self.bulbs = [
         dev for dev in asyncio.run(Discover.discover()).values()
         if dev.is_bulb
     ]
Example #17
0
#!/usr/bin/env python3

import logging, asyncio
from kasa import Discover


#logger = logging.getLogger(__name__)
#logging.basicConfig(
#    level=10,
#    format='%(levelname)s:\t%(name)s\t%(message)s'
#)
#logger.setLevel(logging.WARNING)
#logging.getLogger('kasa.discover').setLevel(logging.DEBUG)
async def print_device(dev):
    await dev.update()
    print("print_device:")
    print(
        "print_device: Got Device\n\tAlias:{}\n\tModel:{}\n\tMac:{}\n\tHost:{}"
        .format(dev.alias, dev.model, dev.mac, dev.host))
    if dev.is_bulb:
        print("print_device: it's a bulb")
    elif dev.is_strip:
        print("print_device: it's a smart strip")
    elif dev.is_plug:
        print("print_device: it's a plug")
    else:
        print("What is this: {}".format(dev))


devices = asyncio.run(Discover.discover(on_discovered=print_device))
Example #18
0
from flask import Flask, render_template, redirect, request
from kasa import SmartPlug, Discover
import asyncio

app = Flask(__name__)

devices = asyncio.run(Discover.discover())


def rediscover():
    devices = asyncio.run(Discover.discover())

    formattedDevices = {'devices': []}

    for addr, dev in devices.items():
        asyncio.run(dev.update())

        formattedDevices['devices'].append({
            'name': dev.alias,
            # 'host': dev.host,
            'address': addr,
            'state': dev.is_on,
            'sysinfo': dev.sys_info
        })


def get_device_by_name(deviceName):
    for addr, dev in devices.items():
        if dev.alias.lower() == deviceName.lower():
            asyncio.run(dev.update())
            return dev
Example #19
0
def get_all_switches():
    found_devices = asyncio.run(Discover.discover())
    devices = [dev for addr, dev in found_devices.items()]
    return devices
Example #20
0
 def discover_new(self):
     LOGGER.info('start')
     devices = asyncio.run(
         Discover.discover(target=self.poly.network_interface['broadcast'],
                           on_discovered=self.discover_new_add_device))
     LOGGER.info("done")
Example #21
0
def setup_device_info():
    devices = asyncio.run(Discover.discover())
    for addr, dev in devices.items():
        if dev.alias == device_name:
            return addr
Example #22
0
	def discoverDevices(self, requestId, channel):
		"""
			Discover all devices on the network and send a Response event containing all of the devices.
		"""
		self.getLogger().debug(f"Discovering devices")
		self.queue.put(Job(
			lambda: self._sendResponseEvent(channel, "kasa.Response", {
				"requestId":requestId,
				"data":Any("sequence<kasa.Device>", [self._createDeviceEvent(addr, dev) for addr, dev in asyncio.run(Discover.discover()).items()]),
			})
		))