示例#1
0
 def _process_device(self, device, cache=None):
     if isinstance(device, Switch):
         callback = self._switch_callback
         registry = self._switches
     elif isinstance(device, Motion):
         callback = self._motion_callback
         registry = self._motions
     else:
         return
     self.devices[device.name] = device
     registry[device.name] = device
     if self._with_subscribers:
         self.registry.register(device)
         self.registry.on(device, 'BinaryState',
                          device._update_state)
     with get_cache() as c:
         try:
             device.ping()
         except DeviceUnreachable:
             return
         else:
             if cache if cache is not None else self._with_cache:
                 c.add_device(device)
     devicefound.send(device)
     callback(device)
示例#2
0
 def _process_device(self, device, cache=None):
     if isinstance(device, Switch):
         callback = self._switch_callback
         registry = self._switches
     elif isinstance(device, Motion):
         callback = self._motion_callback
         registry = self._motions
     elif isinstance(device, Maker):
         callback = self._maker_callback
         registry = self._makers
     else:
         return
     self.devices[device.name] = device
     registry[device.name] = device
     if self._with_subscribers:
         self.registry.register(device)
         self.registry.on(device, 'BinaryState', device._update_state)
     try:
         device.ping()
     except DeviceUnreachable:
         return
     else:
         if cache if cache is not None else self._with_cache:
             with get_cache() as c:
                 c.add_device(device)
     devicefound.send(device)
     callback(device)
示例#3
0
 def _process_device(self, device, cache=None):
     if isinstance(device, Switch):
         callback = self._switch_callback
         registry = self._switches
     elif isinstance(device, Motion):
         callback = self._motion_callback
         registry = self._motions
     else:
         return
     registry[device.name] = device
     if self._with_subscribers:
         self.registry.register(device)
         self.registry.on(device, 'BinaryState', device._update_state)
     if cache if cache is not None else self._with_cache:
         with get_cache() as c:
             c.add_device(device)
     callback(device)
示例#4
0
 def _process_device(self, device, cache=None):
     if isinstance(device, Switch):
         callback = self._switch_callback
         registry = self._switches
     elif isinstance(device, Motion):
         callback = self._motion_callback
         registry = self._motions
     else:
         return
     registry[device.name] = device
     if self._with_subscribers:
         self.registry.register(device)
         self.registry.on(device, "BinaryState", device._update_state)
     if cache if cache is not None else self._with_cache:
         with get_cache() as c:
             c.add_device(device)
     callback(device)
示例#5
0
    def start(self):
        """
        Start the server(s) necessary to receive information from devices.
        """
        if self._with_cache:
            with get_cache() as c:
                for dev in c.devices:
                    self._process_device(dev, cache=False)

        if self._with_discovery:
            # Start the server to listen to new devices
            self.upnp.server.set_spawn(2)
            self.upnp.server.start()

        if self._with_subscribers:
            # Start the server to listen to events
            self.registry.server.set_spawn(2)
            self.registry.server.start()
示例#6
0
    def start(self):
        """
        Start the server(s) necessary to receive information from devices.
        """
        if self._with_cache:
            with get_cache() as c:
                for dev in c.devices:
                    self._process_device(dev, cache=False)

        if self._with_discovery:
            # Start the server to listen to new devices
            self.upnp.server.set_spawn(2)
            self.upnp.server.start()

        if self._with_subscribers:
            # Start the server to listen to events
            self.registry.server.set_spawn(2)
            self.registry.server.start()
示例#7
0
 def _process_device(self, device, cache=None):
     if isinstance(device, Switch):
         callback = self._switch_callback
         registry = self._switches
     elif isinstance(device, Motion):
         callback = self._motion_callback
         registry = self._motions
     elif isinstance(device, Bridge):
         callback = self._bridge_callback
         registry = self._bridges
         for light in device.Lights:
             log.info("Found light \"%s\" connected to \"%s\"" %
                      (light, device.name))
         for group in device.Groups:
             log.info("Found group \"%s\" connected to \"%s\"" %
                      (group, device.name))
     elif isinstance(device, Maker):
         callback = self._maker_callback
         registry = self._makers
     else:
         return
     self.devices[device.name] = device
     registry[device.name] = device
     if self._with_subscribers:
         self.registry.register(device)
         self.registry.on(device, 'BinaryState', device._update_state)
     try:
         if isinstance(device, Bridge):
             pass
         else:
             device.ping()
     except DeviceUnreachable:
         return
     else:
         if cache if cache is not None else self._with_cache:
             with get_cache() as c:
                 c.add_device(device)
     devicefound.send(device)
     callback(device)
示例#8
0
 def _process_device(self, device, cache=None):
     if isinstance(device, Switch):
         callback = self._switch_callback
         registry = self._switches
     elif isinstance(device, Motion):
         callback = self._motion_callback
         registry = self._motions
     elif isinstance(device, Bridge):
         callback = self._bridge_callback
         registry = self._bridges
         for light in device.Lights:
             log.info("Found light \"%s\" connected to \"%s\"" % (light, device.name))
     elif isinstance(device, Maker):
         callback = self._maker_callback
         registry = self._makers
     else:
         return
     self.devices[device.name] = device
     registry[device.name] = device
     if self._with_subscribers:
         self.registry.register(device)
         self.registry.on(device, 'BinaryState',
                          device._update_state)
     try:
         if isinstance(device, Bridge):
             pass
         else:
             device.ping()
     except DeviceUnreachable:
         return
     else:
         if cache if cache is not None else self._with_cache:
             with get_cache() as c:
                 c.add_device(device)
     devicefound.send(device)
     callback(device)
示例#9
0
文件: cli.py 项目: GuoJing/homehue
def wemo():
    parser = ArgumentParser()

    parser.add_argument("--timeout", type=int, default=5,
                        help="Time in seconds to allow for discovery")
    parser.add_argument("--bind", default=None,
                        help="ip:port to which to bind the response server."
                             " Default is localhost:54321")
    parser.add_argument("--no-cache", dest="use_cache", default=None,
                        action="store_false",
                        help="Disable the device cache")
    subparsers = parser.add_subparsers()

    stateparser = subparsers.add_parser("switch",
                                        help="Turn a WeMo Switch on or off")
    stateparser.add_argument("device", help="Name or alias of the device")
    stateparser.add_argument("state", help="'on' or 'off")

    subparsers.add_parser("list",
                          help="List all devices found in the environment")

    args = parser.parse_args()

    ls = state = None

    if getattr(args, 'device', None):
        if args.state.lower() in ("on", "1", "true"):
            state = "on"
        elif args.state.lower() in ("off", "0", "false"):
            state = "off"
        elif args.state.lower() == "toggle":
            state = "toggle"
    else:
        ls = True

    def on_switch(switch):
        if state:
            if switch.name == args.device:
                if state == "toggle":
                    found_state = switch.get_state(force_update=True)
                    switch.set_state(not found_state)
                else:
                    getattr(switch, state)()
                sys.exit(0)
        elif ls:
            print "Switch: ", switch.name

    def on_motion(motion):
        if ls:
            print "Motion: ", motion.name

    try:
        env = Environment(on_switch, on_motion, with_subscribers=False,
                          bind=args.bind, with_cache=args.use_cache)
        if getattr(args, 'device', None):
            args.device = env._config.aliases.get(args.device, args.device)
        env.start()
        with get_cache() as c:
            if c.empty:
                args.use_cache = False
        if (args.use_cache is not None and not args.use_cache) or (
                    env._config.cache is not None and not env._config.cache):
            env.discover(args.timeout)
    except KeyboardInterrupt:
        sys.exit(0)

    if not ls:
        print "No device found with that name."
        sys.exit(1)
示例#10
0
def wemo():
    parser = ArgumentParser()

    parser.add_argument("--timeout",
                        type=int,
                        default=5,
                        help="Time in seconds to allow for discovery")
    parser.add_argument("--bind",
                        default=None,
                        help="ip:port to which to bind the response server."
                        " Default is localhost:54321")
    parser.add_argument("--no-cache",
                        dest="use_cache",
                        default=None,
                        action="store_false",
                        help="Disable the device cache")
    subparsers = parser.add_subparsers()

    stateparser = subparsers.add_parser("switch",
                                        help="Turn a WeMo Switch on or off")
    stateparser.add_argument("device", help="Name or alias of the device")
    stateparser.add_argument("state", help="'on' or 'off")

    subparsers.add_parser("list",
                          help="List all devices found in the environment")

    args = parser.parse_args()

    ls = state = None

    if getattr(args, 'device', None):
        if args.state.lower() in ("on", "1", "true"):
            state = "on"
        elif args.state.lower() in ("off", "0", "false"):
            state = "off"
        elif args.state.lower() == "toggle":
            state = "toggle"
    else:
        ls = True

    def on_switch(switch):
        if state:
            if switch.name == args.device:
                if state == "toggle":
                    found_state = switch.get_state(force_update=True)
                    switch.set_state(not found_state)
                else:
                    getattr(switch, state)()
                sys.exit(0)
        elif ls:
            print "Switch: ", switch.name

    def on_motion(motion):
        if ls:
            print "Motion: ", motion.name

    try:
        env = Environment(on_switch,
                          on_motion,
                          with_subscribers=False,
                          bind=args.bind,
                          with_cache=args.use_cache)
        if getattr(args, 'device', None):
            args.device = env._config.aliases.get(args.device, args.device)
        env.start()
        with get_cache() as c:
            if c.empty:
                args.use_cache = False
        if (args.use_cache is not None
                and not args.use_cache) or (env._config.cache is not None
                                            and not env._config.cache):
            env.discover(args.timeout)
    except KeyboardInterrupt:
        sys.exit(0)

    if not ls:
        print "No device found with that name."
        sys.exit(1)
示例#11
0
文件: cli.py 项目: jcmb/ouimeaux
def wemo():
    parser = argparse.ArgumentParser()

    parser.add_argument("--timeout", type=int, default=5,
                        help="Time in seconds to allow for discovery")
    parser.add_argument("--bind", default=None,
                        help="ip:port to which to bind the response server."
                             " Default is localhost:54321")
    parser.add_argument("--no-cache", dest="use_cache", default=None,
                        action="store_false",
                        help="Disable the device cache")
    parser.add_argument("--debug", action="store_true", default=False,
                        help="Enable debug logging")
    subparsers = parser.add_subparsers()

    clearparser = subparsers.add_parser(
        "clear", help="Clear the device cache")
    # Add a hidden argument so we can detect when we're in this command
    clearparser.add_argument('--clearcache', default="1", 
                             help=argparse.SUPPRESS)

    stateparser = subparsers.add_parser("switch",
                                        help="Turn a WeMo Switch on or off")
    stateparser.add_argument("device", help="Name or alias of the device")
    stateparser.add_argument("state", help="'on', 'off', 'toggle' or 'status'")

    subparsers.add_parser("list",
                          help="List all devices found in the environment")

    args = parser.parse_args()

    ls = state = None

    if getattr(args, 'debug', False):
        logging.basicConfig(level=logging.DEBUG)

    if getattr(args, 'clearcache', None):
        for fname in 'cache', 'cache.db':
            filename = in_home('.wemo', fname)
            try:
                os.remove(filename)
            except OSError:
                # File didn't exist; cache must be clear
                pass
        print "Device cache cleared."
        return

    if getattr(args, 'device', None):
        if args.state.lower() in ("on", "1", "true"):
            state = "on"
        elif args.state.lower() in ("off", "0", "false"):
            state = "off"
        elif args.state.lower() == "toggle":
            state = "toggle"
        elif args.state.lower() == "status":
            state = "status"
    else:
        ls = True

    def on_switch(switch):
        if state:
            if switch.name == args.device:
                if state == "toggle":
                    found_state = switch.get_state(force_update=True)
                    switch.set_state(not found_state)
                elif state == "status":
                    found_state = switch.get_state(force_update=True)
                    print found_state
                else:
                    getattr(switch, state)()
                sys.exit(0)
        elif ls:
            print "Switch: ", switch.name

    def on_motion(motion):
        if ls:
            print "Motion: ", motion.name

    try:
        env = Environment(on_switch, on_motion, with_subscribers=False,
                          bind=args.bind, with_cache=args.use_cache)
        if getattr(args, 'device', None):
            args.device = env._config.aliases.get(args.device, args.device)
        env.start()
        with get_cache() as c:
            if c.empty:
                args.use_cache = False
        if (args.use_cache is not None and not args.use_cache) or (
                    env._config.cache is not None and not env._config.cache):
            env.discover(args.timeout)
    except KeyboardInterrupt:
        sys.exit(0)
    except UPnPLoopbackException:
        print """
Loopback interface is being used! You will probably not receive any responses 
from devices.  Use ifconfig to find your IP address, then either pass the 
--bind argument or edit ~/.wemo/config.yml to specify the IP to which devices 
should call back during discovery.""".strip()
        sys.exit(1)

    if not ls:
        print "No device found with that name."
        sys.exit(1)