iancmcc / ouimeaux

Python API to Belkin WeMo devices
http://pypi.python.org/pypi/ouimeaux
BSD 3-Clause "New" or "Revised" License
575 stars 150 forks source link

Turn on, then off in REST API #31

Closed qrush closed 10 years ago

qrush commented 10 years ago

We've been using ouimeaux at CoworkBuffalo and it's been great. Right now, however, I have to make two API requests in this fashion:

  1. POST to turn switch on
  2. sleep N seconds (usually 3-5)
  3. POST to turn switch off

The main problem here is that if the second API call fails, that's not good. We need some kind of way to say "Turn on, then off after a delay".

Could something like this be built into ouimeaux? Sadly I'm not too familiar with Python or its threading model.

iancmcc commented 10 years ago

Sure. Actually, I've been thinking about adding a slightly more generic rules engine. I'll probably get that in the 0.8 release.

fingon commented 10 years ago

I'm planning rather complex rules engine for my home automation project as well; basic idea is to control WeMo switch or two, Phillips Hue lights, and potentially some other things too based on input from couple of sources. At the moment I'm trying to determine the rules I need, and based on that, determine how to encode them most easily ;) Any ETA on 0.8?

iancmcc commented 10 years ago

Not yet; day job is pretty involved at the moment.

Let me try to get up a prototype rules engine as soon as I can, then maybe it'll elicit ideas from others. It's pretty straightforward when it comes down to it, since performance isn't going to be an issue at this scale. The problems, such as they are, mostly come down to technology choices and UI.

fingon commented 10 years ago

I'm not going to try to even create UI for my rule set, it's just matter of seeing results of it (and overriding it's choices) that I care about currently.

My current model:

Inputs:

Conditions:

Actions:

And given those, I'm for every action target (Hue/WeMo switch), evaluating a priority list of rules with a set of conditions, and executing the first action with matching conditions. Rule re-evaluation is triggered by WeMo events, potential value for timed conditions changing, or time of day (sunrise/sunset state change notably).

I think I have mostly pieces in place (going to probably use ouimeaux for WeMo control, phue + my own module on top of that for Hue, and have sunrise/sunset calculator in Python), so next part is just to do the glue for it all ;)

Who knows, this might actually end up with ~Python being the 'rules', with just convenience grammar for specifying this priority list and conditions/actions.

aktur commented 10 years ago

It all can be done already and even more. All you need to do is to employ openremote.org as a middleware. It can read sensors, execute actions and have pretty powerful Drools rules engine. It is written in Java thus it runs on all hardware that can run java 6 and beyond. I'm personally running it on my Synology NAS and eBox (dedicated small linux machine). Moreover, you can easily make a custom user interface which runs on iOS, Android and web console. The best part is that openremote is freeware and open source. I'm using it with WeMo through REST api of ouimeaux. Runs great except that it needs reboot once a few days -- something wrong with discovery. Anyway, you can read more about openremote at http://openremote.org and about my applications at http://mqlservice.net/openremote/blog/

fingon commented 10 years ago

I was planning to run this on OpenWrt router ultimately, which doesn't have the memory (or probably CPU either) for Java. Perhaps I'll reconsider it and use Raspberry Pi or a Mac Mini instead, I'm not that keen to roll my own solutions unless they're absolutely needed so I suppose I'll take a look. Thanks for the hint ;)

aktur commented 10 years ago

Dedicated hardware for home automation server is way to go. You will hate to reset your internet connection when you need just to reset the controller.

balloob commented 10 years ago

Markus, I've created exactly the "glue" you are talking about in Python 3: Home Assistant on GitHub.

For running home automation I would also not run it on your router as high CPU can impact your network performance and stability!

iancmcc commented 10 years ago

Punting on the rules engine until 0.8; added a simple blink method to switches.

iancmcc commented 10 years ago

This should work by passing "blink" as the state and an optional "delay" argument. e.g.:

POST /api/device/bedroom?state=blink&delay=5

qrush commented 10 years ago

Perfect. I'll give this a shot this week.

qrush commented 10 years ago

This is fantastic and works wonderfully. THANK YOU!

awwyeah

mbaturin commented 9 years ago

Is there a way to browse directly to a button within the link of the Web App for one of my switches. My goal is to save it as a pin on my xbox one called Lights so i can say Xbox Lights and have my living room lights (on a Wemo Switch) turn off and on through voice command. Currently though I see the buttons are embedded in javascript.

iancmcc commented 9 years ago

Yes, see the rest Api docs. You can POST directly to a switch to toggle it. 

http://ouimeaux.readthedocs.org/en/latest/server.html#rest-api

On Tue, Feb 17, 2015 at 9:54 PM, mbaturin notifications@github.com wrote:

Is there a way to browse directly to the link of the Web App for one of my switches. My goal is to save it as a pin on my xbox one called Lights so i can say Xbox Lights and have my living room lights (on a Wemo Switch) turn off and on through voice command. Currently though I see the buttons are embedded in javascript. Chandelier Light

Reply to this email directly or view it on GitHub: https://github.com/iancmcc/ouimeaux/issues/31#issuecomment-74807361

mbaturin commented 9 years ago

Thanks for the super fast response - this is awesome by the way. I am an IT guy but programming isn't my strong suit. Is there any chance you would be able to give me an example of what the URL might look like to accomplish this? On the Xbox all I have is Internet explorer so I need to it basically be a permanent URL link that will toggle the switch.

iancmcc commented 9 years ago

It's in the docs. /api/device/MYDEVICE I believe, but you have to POST to it so a browser won't work unless you build a form.

On Tue, Feb 17, 2015 at 9:58 PM, mbaturin notifications@github.com wrote:

Thanks for the super fast response - this is awesome by the way. I am an IT guy but programming isn't my strong suit. Is there any chance you would be able to give me an example of what the URL might look like to accomplish this? On the Xbox all I have is Internet explorer so I need to it basically be a permanent URL link that will toggle the switch.

Reply to this email directly or view it on GitHub: https://github.com/iancmcc/ouimeaux/issues/31#issuecomment-74807717

mbaturin commented 9 years ago

OK Thanks I will give it a shot - eh just realized with a form a submit button would have to be pressed - that won't accomplish what I am trying to do with pure voice command. The hope was for a simple "Xbox Lights" voice command, quick opening of an IE window on the Xbox one, and the lights would go on/off.

A POST within a form would still require a button press if I remember correctly.

mbaturin commented 9 years ago

Tried this in my browser: http://10.0.0.5:5000/api/device/Chandelier?state=toggle

All I am getting back is this:

{ "host": "10.0.0.xxx", "model": "Belkin Plugin Socket 1.0", "name": "Chandelier Light", "serialnumber": "221410K13xxxxx", "state": 1, "type": "LightSwitch" }

I would really appreciate if you could help me as to the code I would need to put into an HTML file if that is what is needed to browse to and toggle the switch. I would be willing to donate a small amount to you for that and for all this other great work you've done. If not, I understand. I feel like I am so close but not quite able to figure it out. I tried this futile attempt at an HTML page:

curl -X POST http://10.0.0.5:5000/api/device/Chandelier?state=toggle

iancmcc commented 9 years ago

Did that curl command not work? It should have. You can technically omit ?state=toggle, since it’s the default.

— Ian

On Tue, Feb 17, 2015 at 11:00 PM, mbaturin notifications@github.com wrote:

Tried this in my browser: http://10.0.0.5:5000/api/device/Chandelier?state=toggle All I am getting back is this: { "host": "10.0.0.xxx", "model": "Belkin Plugin Socket 1.0", "name": "Chandelier Light", "serialnumber": "221410K13xxxxx", "state": 1, "type": "LightSwitch" } I would really appreciate if you could help me as to the code I would need to put into an HTML file if that is what is needed to browse to and toggle the switch. I would be willing to donate a small amount to you for that and for all this other great work you've done. If not, I understand. I feel like I am so close but not quite able to figure it out. I tried this futile attempt at an HTML page:

curl -X POST http://10.0.0.5:5000/api/device/Chandelier?state=toggle ##

Reply to this email directly or view it on GitHub: https://github.com/iancmcc/ouimeaux/issues/31#issuecomment-74812577

mbaturin commented 9 years ago

Yeah I think that is the right command, but I am trying to have it execute automatically when visiting a URL. Where is that command meant to be run? In a terminal window?

I think what I need help with is how to tie the POST command to a Form on an HTML page that autosubmits when the page is loaded.

iancmcc commented 9 years ago

Yes. You can probably get a page to auto-POST a form when you load it, although it's a huge security hole so maybe not.