Skip to content

Instantly share code, notes, and snippets.

@henriqueutsch
Last active October 21, 2018 12:56
Show Gist options
  • Save henriqueutsch/acc1b4fc544fdcf88ebfb7fee57c210e to your computer and use it in GitHub Desktop.
Save henriqueutsch/acc1b4fc544fdcf88ebfb7fee57c210e to your computer and use it in GitHub Desktop.
Broadlink MP1 AutoPower
#!/usr/bin/python
#Referencias
# https://br.gearbest.com/power-strips/pp_360376.html
# sudo pip3 install broadlink
# http://domo-attitude.fr/broadlink-domoticz/
# pmset -g batt
# https://stackoverflow.com/questions/17651017/python-post-osx-notification
# system_profiler SPPowerDataType | grep "Cycle Count" | awk '{print $3}'
# http://fruitjuiceapp.com/
import broadlink
import os
import subprocess
import time
def power(status):
devices = broadlink.mp1(host=("IP",80), mac=bytearray.fromhex("MAC"))
devices.auth()
devices.set_power(1, status)
while True:
time.sleep(5)
battery = subprocess.check_output("pmset -g batt | grep -Eo '\d+%' | cut -d% -f1",stderr=subprocess.STDOUT,shell=True)
print(battery)
os.system("clear")
if int(battery) <= 15:
print("Bateria: %sStatus: Power ON" % battery)
power(True)
else:
print("Bateria: %sStatus: Power OFF" % battery)
if battery == 100:
power(False)
#!/usr/bin/python
#Referencias
# https://br.gearbest.com/power-strips/pp_360376.html
# sudo pip3 install broadlink
# http://domo-attitude.fr/broadlink-domoticz/
# https://stackoverflow.com/questions/17651017/python-post-osx-notification
# https://pypi.org/project/weather-api/
# https://home.openweathermap.org/api_keys
import broadlink
import os
import subprocess
import time
import pyowm
def power(status):
devices = broadlink.mp1(host=("IP",80), mac=bytearray.fromhex("MAC"), devtype="mp1")
devices.auth()
devices.set_power(2, status)
while True:
time.sleep(5)
owm = pyowm.OWM('API_KEY') # https://home.openweathermap.org/api_keys
observation = owm.weather_at_place('Belo Horizonte,BR')
w = observation.get_weather()
temp = w.get_temperature('celsius')
tempnow = temp["temp"]
os.system("clear")
settemp = 34
if int(tempnow) >= settemp:
print("Temperatura: %sStatus: Power ON" % tempnow)
power(True)
else:
print("Temperatura: %sStatus: Power OFF" % tempnow)
if int(tempnow) >= settemp:
power(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment