基于iphonegp的Wemo灯开关自动控制

2024-09-28 23:16:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个程序,根据我的iPhone的GPS坐标来切换我家的灯光。以下是我到目前为止的情况。然而,我觉得必须有更好的方法来做到这一点。有没有一种方法可以在不每隔五分钟打一次电话的情况下获取GPS数据

到目前为止,我已经尝试了以下方法,但毫无乐趣:

  • 使用快捷方式和脚本,我试图编写一些JavaScript,当我离家近的时候会触发。但是,我不知道如何使用scriptablify使用await require('wemo-client')。我一直收到一个错误,“ReferenceError:找不到变量:require”
  • IFTTT没有可变定时触发器,因此灯在15分钟后不会熄灭。此外,我还计划添加一个不受支持的运动传感器触发器
  • Python是10美元。是的,我很便宜
  • Apple HomeKit不支持我正在使用的型号,Wemo智能灯开关F7C030
  • 下面的代码有效,但我讨厌每五分钟就要ping一次我的手机。我宁愿每天根据需要触发一到两次此代码以节省电池寿命

如有任何建议,将不胜感激

代码:

import sys
import time
import datetime
import os
from pyicloud import PyiCloudService
import pywemo

APPLE_ID = os.getenv('APPLE_ID') # Apple ID username
APPLE_ID_PASSWORD = os.getenv('APPLE_ID_PASSWORD') # Apple ID password
API = PyiCloudService(APPLE_ID, APPLE_ID_PASSWORD)
IPHONE = API.devices[1]
LOCATION = IPHONE.location()
FIVE = 300 # 5 * 60 seconds
FIFTEEN = 900 # 15 * 60 seconds
ONEMILE = 0.01449275362318840579710144927536 # one mile is 1/69 degrees lat or long
HOMELAT = # my home's latitude
HOMELONG = # my home's longitude
WEMOS = pywemo.discover_devices()
LEN_WEMOS = range(len(WEMOS))

# Two factor authentication to retrieve iPhone data
if API.requires_2fa:
    import click
    print("Two-step authentication required. Your trusted devices are:")

    DEVICES = API.devices
    for i, device in enumerate(DEVICES):
        print("  %s: %s" % (i, device.get('deviceName', "SMS to %s" % device.get('phoneNumber'))))

    DEF_DEVICE = click.prompt('Which device would you like to use?', default=0)
    DEVICE = DEVICES[DEF_DEVICE]
    if not API.send_verification_code(DEVICE):
        print("Failed to send verification code")
        sys.exit(1)

    CODE = click.prompt('Please enter validation code')
    if not API.validate_verification_code(DEVICE, CODE):
        print("Failed to verify verification code")
        sys.exit(1)

# Turn off the lights when I leave
def leavehome():
    timenow = datetime.datetime.now()
    print("Left home on {}".format(timenow.strftime("%B %d, %Y at %H:%M:%S")))
    for wemo in LEN_WEMOS:
        WEMOS[wemo].off()

# Turn on the lights for 15 minutes when I get home
def arrivehome():
    timenow = datetime.datetime.now()
    print("Arrived home on {}".format(timenow.strftime("%B %d, %Y at %H:%M:%S")))

    # Loop through all Wemo devices
    for wemo in LEN_WEMOS:
        WEMOS[wemo].on()

    time.sleep(FIFTEEN)
    for wemo in LEN_WEMOS:
        WEMOS[wemo].off()

# Automatically turn off the lights after 15 minutes - save electricity
def timeoff():
    time.sleep(FIFTEEN)
    for wemo in LEN_WEMOS:
        WEMOS[wemo].off()

# Ping my phone for GPS data
def pingphone(prev):
    mylat = LOCATION["latitude"]
    mylong = LOCATION["longitude"]
    logic(prev, mylat, mylong)
    time.sleep(FIVE)

# Perform logic to determine if I'm home, out, arriving, or leaving
def logic(prev, lat, long):
    inrange = (HOMELAT+ONEMILE >= lat >= HOMELAT-ONEMILE and HOMELONG+ONEMILE >= long >= HOMELONG-ONEMILE)
    current = bool(inrange)
    previous = prev
    if current and not previous:
        arrivehome()
    elif previous and not current:
        leavehome()
    else:
        timeoff()

    pingphone(current)

# Run the script
pingphone(False)


Tags: toimportapiidapplehomefordatetime