ubuntu – 我的Nest能否测试是否存在本地设备,以确定我是否在家?

前端之家收集整理的这篇文章主要介绍了ubuntu – 我的Nest能否测试是否存在本地设备,以确定我是否在家?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 Nest学习恒温器.我认为当我不使用运动传感器时,它会尝试“自动检测”.无论如何,它不是很准确,更重要的是,它的算法不透明.

相反,我认为有更好的方法来判断我是否在家里……

Nest在与我的手机相同的子网上绘制IP地址.

我想:

>告诉我手机的IP地址(以及我妻子的手机)
>请求Nest定期(可能每10或15分钟)ping这些IP地址以进行状态测试
>让Nest根据这些客观测试更新它自己的归属/远离会计机制

这可能通过Nest API吗?

这是否违反任何Nest API Prohibitions

像这样的客户已经存在吗?

实际上,我使用Nest API想出了如何准确地做到这一点.它完美地运作!

我已经创建了一对Python脚本,/usr/bin/nest-home和/usr/bin/nest-away,它们是我上传到Ubuntu的uhome软件包的一部分.还有/usr/bin/uhome脚本,它处理我的第一个问题 – 读取IP / MAC地址的输入列表,迭代它们以查看是否有任何可ping的,并根据结果,标记我们的家或者离开.

与Nest交谈的真正魔力在于3个后续的API调用

>登录并建立身份验证令牌
>检索结构ID
>更新结构的远离布尔值

完整下面的脚本,或者您可以在LaunchpadGithub找到它.

  1. #!/usr/bin/python
  2. #
  3. # nest-home,nest-away
  4. #
  5. # Copyright 2014 Dustin Kirkland <dustin.kirkland@gmail.com>
  6. #
  7. # Licensed under the Apache License,Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing,software
  14. # distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17.  
  18. import os
  19. import requests
  20. import time
  21. import urllib
  22. import urllib2
  23. import sys
  24. import json
  25. import yaml
  26.  
  27. # login
  28. with open(os.path.expanduser("~") + "/.uhome/nest.yaml","r") as stream:
  29. credentials = yaml.load(stream)
  30. #headers = {"user-agent": "Nest/1.1.0.10 CFNetwork/548.0.4","X-nl-protocol-version": "1"}
  31. headers = {"X-nl-protocol-version": "1"}
  32. response = requests.post("https://home.nest.com/user/login",credentials)
  33. response = json.loads(response.content)
  34. transport_url = response["urls"]["transport_url"]
  35. userid = response["userid"]
  36. headers["Authorization"] = "Basic " + response["access_token"]
  37. headers["X-nl-user-id"] = userid
  38. # get structure id
  39. request = urllib2.Request(transport_url + "/v3/mobile/user." + userid,headers=headers)
  40. response = urllib2.urlopen(request).read()
  41. response = json.loads(response)
  42. structure_id = response["structure"].keys()[0]
  43. # set away,if necessary
  44. currently_away = response["structure"][structure_id]["away"]
  45. if "away" in sys.argv[0] and not currently_away:
  46. data = '{"away_timestamp":' + str(time.time()) + ',"away":true,"away_setter":0}'
  47. new_away = "True"
  48. elif not "away" in sys.argv[0] and currently_away:
  49. data = '{"away_timestamp":' + str(time.time()) + ',"away":false,"away_setter":0}'
  50. new_away = "False"
  51. else:
  52. data = ""
  53. new_away = str(currently_away)
  54. if data:
  55. structure_url = transport_url + "/v2/put/structure" "." + structure_id
  56. request = urllib2.Request(structure_url,data,headers)
  57. try:
  58. urllib2.urlopen(request).read()
  59. except urllib2.URLError:
  60. print "Put operation Failed"
  61. print "Nest updated --> away=" + new_away
  62. else:
  63. print "No need to update Nest --> away=" + new_away

猜你在找的Ubuntu相关文章