示例#1
0
    def discover(
        protocol: TPLinkSmartHomeProtocol = None,
        target: str = "255.255.255.255",
        port: int = 9999,
        timeout: int = 3,
        discovery_packets=3,
        return_raw=False,
    ) -> Dict[str, SmartDevice]:
        """
        Sends discovery message to 255.255.255.255:9999 in order
        to detect available supported devices in the local network,
        and waits for given timeout for answers from devices.

        :param protocol: Protocol implementation to use
        :param target: The target broadcast address (e.g. 192.168.xxx.255).
        :param timeout: How long to wait for responses, defaults to 3
        :param port: port to send broadcast messages, defaults to 9999.
        :rtype: dict
        :return: Array of json objects {"ip", "port", "sys_info"}
        """
        if protocol is None:
            protocol = TPLinkSmartHomeProtocol()

        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.settimeout(timeout)

        req = json.dumps(Discover.DISCOVERY_QUERY)
        _LOGGER.debug("Sending discovery to %s:%s", target, port)

        encrypted_req = protocol.encrypt(req)
        for i in range(discovery_packets):
            sock.sendto(encrypted_req[4:], (target, port))

        devices = {}
        _LOGGER.debug("Waiting %s seconds for responses...", timeout)

        try:
            while True:
                data, addr = sock.recvfrom(4096)
                ip, port = addr
                info = json.loads(protocol.decrypt(data))
                device_class = Discover._get_device_class(info)
                if return_raw:
                    devices[ip] = info
                elif device_class is not None:
                    devices[ip] = device_class(ip)
        except socket.timeout:
            _LOGGER.debug("Got socket timeout, which is okay.")
        except Exception as ex:
            _LOGGER.error("Got exception %s", ex, exc_info=True)
        _LOGGER.debug("Found %s devices: %s", len(devices), devices)
        return devices
示例#2
0
    def discover(protocol: TPLinkSmartHomeProtocol = None,
                 port: int = 9999,
                 timeout: int = 3) -> Dict[str, SmartDevice]:
        """
        Sends discovery message to 255.255.255.255:9999 in order
        to detect available supported devices in the local network,
        and waits for given timeout for answers from devices.

        :param protocol: Protocol implementation to use
        :param timeout: How long to wait for responses, defaults to 3
        :param port: port to send broadcast messages, defaults to 9999.
        :rtype: dict
        :return: Array of json objects {"ip", "port", "sys_info"}
        """
        if protocol is None:
            protocol = TPLinkSmartHomeProtocol()

        target = "255.255.255.255"

        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.settimeout(timeout)

        req = json.dumps(Discover.DISCOVERY_QUERY)
        _LOGGER.debug("Sending discovery to %s:%s", target, port)

        encrypted_req = protocol.encrypt(req)
        sock.sendto(encrypted_req[4:], (target, port))

        devices = {}
        _LOGGER.debug("Waiting %s seconds for responses...", timeout)

        try:
            while True:
                data, addr = sock.recvfrom(4096)
                ip, port = addr
                info = json.loads(protocol.decrypt(data))
                device_class = Discover._get_device_class(info)
                if device_class is not None:
                    devices[ip] = device_class(ip)
        except socket.timeout:
            _LOGGER.debug("Got socket timeout, which is okay.")
        except Exception as ex:
            _LOGGER.error("Got exception %s", ex, exc_info=True)
        return devices
示例#3
0
    def discover_single(
            host: str,
            protocol: TPLinkSmartHomeProtocol = None) -> Optional[SmartDevice]:
        """Discover a single device by the given IP address.

        :param host: Hostname of device to query
        :param protocol: Protocol implementation to use
        :rtype: SmartDevice
        :return: Object for querying/controlling found device.
        """
        if protocol is None:
            protocol = TPLinkSmartHomeProtocol()

        info = protocol.query(host, Discover.DISCOVERY_QUERY)

        device_class = Discover._get_device_class(info)
        if device_class is not None:
            return device_class(host)

        return None
示例#4
0
文件: discover.py 项目: pf56/pyHS100
    def discover_single(
            host: str,
            protocol: TPLinkSmartHomeProtocol = None) -> SmartDevice:
        """
        Similar to discover(), except only return device object for a single
        host.

        :param host: Hostname of device to query
        :param protocol: Protocol implementation to use
        :rtype: SmartDevice
        :return: Object for querying/controlling found device.
        """
        if protocol is None:
            protocol = TPLinkSmartHomeProtocol()

        info = protocol.query(host, Discover.DISCOVERY_QUERY)

        device_class = Discover._get_device_class(info)
        if device_class is not None:
            return device_class(host)
        else:
            return None
示例#5
0
    def discover_single(host: str,
                        protocol: TPLinkSmartHomeProtocol = None
                        ) -> SmartDevice:
        """
        Similar to discover(), except only return device object for a single
        host.

        :param host: Hostname of device to query
        :param protocol: Protocol implementation to use
        :rtype: SmartDevice
        :return: Object for querying/controlling found device.
        """
        if protocol is None:
            protocol = TPLinkSmartHomeProtocol()

        info = protocol.query(host, Discover.DISCOVERY_QUERY)

        device_class = Discover._get_device_class(info)
        if device_class is not None:
            return device_class(host)
        else:
            return None
 def loadDevices(self):
     deviceManager = DeviceManager(self.context)
     for dev in TPLinkSmartHomeProtocol.discover():
         plug = SmartPlug(dev['ip'])
         deviceManager.addDevice(TPLinkDevice(plug))
     deviceManager.finishedLoading('tplink')
示例#7
0
GPIO.setmode(GPIO.BCM)
for port in GPIO_PORTS:
    print("Setting up port: %d" % (port))
    GPIO.setup(port, GPIO.IN, pull_up_down=GPIO.PUD_UP)

##### wait for the network to be established

##### find Plug
devToFind = 'Smart Plug 1'
devIP = ""
foundDev = False

print("Now searching for device: %s" % (devToFind))
while not foundDev:
    try:
        devs = TPLinkSmartHomeProtocol.discover(timeout=timeout)
    except IOError as e:
        if e.errno == 101:  # Network Unreachable
            devs = {}
        else:
            raise
    for devId in devs:
        alias = devId['sys_info']['system']['get_sysinfo']['alias']
        if alias == devToFind:
            devIP = devId['ip']
            foundDev = True

if foundDev:
    print("Setting up control loop with %s: %s" % (devToFind, devIP))
    plug = SmartPlug(devIP)
示例#8
0
import logging
from pprint import pprint as pp

from pyHS100 import TPLinkSmartHomeProtocol
logging.basicConfig(level=logging.DEBUG)

for dev in TPLinkSmartHomeProtocol.discover():
    print("Found device!")
    pp(dev)
示例#9
0
def discover(timeout):
    """Discover devices in the network."""
    click.echo("Discovering devices for %s seconds" % timeout)
    for dev in TPLinkSmartHomeProtocol.discover(timeout=timeout):
        print("Found device: %s" % pformat(dev))
示例#10
0
    def discover(protocol: TPLinkSmartHomeProtocol = None,
                 port: int = 9999,
                 timeout: int = 3) -> Dict[str, SmartDevice]:
        """
        Sends discovery message to 255.255.255.255:9999 in order
        to detect available supported devices in the local network,
        and waits for given timeout for answers from devices.

        :param protocol: Protocol implementation to use
        :param timeout: How long to wait for responses, defaults to 5
        :param port: port to send broadcast messages, defaults to 9999.
        :rtype: dict
        :return: Array of json objects {"ip", "port", "sys_info"}
        """
        if protocol is None:
            protocol = TPLinkSmartHomeProtocol()

        discovery_query = {
            "system": {
                "get_sysinfo": None
            },
            "emeter": {
                "get_realtime": None
            }
        }
        target = "255.255.255.255"

        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.settimeout(timeout)

        req = json.dumps(discovery_query)
        _LOGGER.debug("Sending discovery to %s:%s", target, port)

        encrypted_req = protocol.encrypt(req)
        sock.sendto(encrypted_req[4:], (target, port))

        devices = {}
        _LOGGER.debug("Waiting %s seconds for responses...", timeout)

        try:
            while True:
                data, addr = sock.recvfrom(4096)
                ip, port = addr
                info = json.loads(protocol.decrypt(data))
                if "system" in info and "get_sysinfo" in info["system"]:
                    sysinfo = info["system"]["get_sysinfo"]
                    if "type" in sysinfo:
                        type = sysinfo["type"]
                    elif "mic_type" in sysinfo:
                        type = sysinfo["mic_type"]
                    else:
                        _LOGGER.error("Unable to find the device type field!")
                        type = "UNKNOWN"
                else:
                    _LOGGER.error("No 'system' nor 'get_sysinfo' in response")
                if "smartplug" in type.lower():
                    devices[ip] = SmartPlug(ip)
                elif "smartbulb" in type.lower():
                    devices[ip] = SmartBulb(ip)
        except socket.timeout:
            _LOGGER.debug("Got socket timeout, which is okay.")
        except Exception as ex:
            _LOGGER.error("Got exception %s", ex, exc_info=True)
        return devices