omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Here is a script to control a local WeMo switch.

    Pythonista
    4
    6
    14014
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • pruppert222
      pruppert222 last edited by

      This is a script that you can use to control a local WeMo switch. You can turn turn on, turn off, toggle, or get status. You need to know the local IP of the WeMo and enter that in the top of the script. Then, uncomment one of the triggers at the bottom of the script.

      https://gist.github.com/pruppert/af7d38cb7b7ca75584ef

      This is a fork of pdumoulin's blinky script on github (https://github.com/pdumoulin/blinky).

      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        Cool stuff!

        I added code to provide a Pythonista UI in the comments of the gist. Warning: I don't have a Wemo (but I now have one on order) so my code might require further debugging.

        1 Reply Last reply Reply Quote 0
        • ccc
          ccc last edited by

          I purchased a few WeMo Insight Switches and a NetCam HD+ (with movement detection and night vision) and was able to use @pruppert 's code above to control the WeMo Switches. The UI that I added in the comments of the gist even worked without any debugging required. I was able to hack the code to remove the dependencies on urllib2 and re (my brain just does not grok regex) and replaced them with the far more elegant Requests and BeautifulSoup.

          I am trying to see how much of the Belkin WeMo app I can replicate in Pythonista and I could use some help on device discovery. I want to find the IP addresses of all the WeMo's on the local network and that seems to require doing a UPnP lookup. There is an interesting Python module called ouimeaux that does WeMo device discovery but it's requirements include gevent and other modules that not part of Pythonista.

          Do any of you folks know how to do WeMo device discovery using only the modules that are built into Pythonista?

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            The following discovered my upnp router etc.

            
            import sys
            import socket
            
            SSDP_ADDR = "239.255.255.250";
            SSDP_PORT = 1900;
            SSDP_MX = 10;
            SSDP_ST = "ssdp:all";
            
            ssdpRequest = "M-SEARCH * HTTP/1.1\r\n" + \
                            "HOST: %s:%d\r\n" % (SSDP_ADDR, SSDP_PORT) + \
                            "MAN: \"ssdp:discover\"\r\n" + \
                            "MX: %d\r\n" % (SSDP_MX, ) + \
                            "ST: %s\r\n" % (SSDP_ST, ) + "\r\n";
                            
               
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
            sock.sendto(ssdpRequest, (SSDP_ADDR, SSDP_PORT))
            print sock.getsockname()
            
            sock.settimeout(5)# set 5 sec timeout
            while True:
                print sock.recv(1000)
            
            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by

              Thanks massively @JonB! That works perfectly. You get the gold star.

              One question: Why did you put ; at the end of several strings? Does that have an effect?

              upnp_discovery.py is a slight reformulation that also seems to work:

              import socket
              
              SSDP_DICT = { 'ip_address' : '239.255.255.250',
                            'port'       : 1900,
                            'mx'         : 10,
                            'st'         : 'ssdp:all' }
              
              ssdp_request = '''M-SEARCH * HTTP/1.1
              HOST: {ip_address}:{port}
              MAN: "ssdp:discover"
              MX: {mx}
              ST: {st}
              
              '''.replace('\n', '\r\n').format(**SSDP_DICT)
              
              sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
              sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
              sock.sendto(ssdp_request, (SSDP_DICT['ip_address'], SSDP_DICT['port']))
              print sock.getsockname()
              
              sock.settimeout(5)# set 5 sec timeout
              while True:
                  print sock.recv(1000)
              
              1 Reply Last reply Reply Quote 0
              • markph
                markph last edited by

                Change the ST value from:
                ssdp:all

                to:
                upnp:rootdevice

                Such that:
                SSDP_DICT = {'ip_address': '239.255.255.250',
                'port': 1900,
                'mx': 10,
                'st': 'upnp:rootdevice'}

                Old/previous value did not report any Wemo devices. Updated value now shows all UPNP devices.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors