programmazione:python:shelly1pm_mini
Uno script python che spegne o accende uno shelly pm1mini gen3
Autore: Fabio Di Matteo
Ultima revisione: 12/05/2025 13:41
#!/usr/bin/env python import urllib3,json,sys ip="192.168.0.8" # curl -X POST -d '{"id":0, "on":true}' "http://<ip>/rpc/Switch.Set" def get_status(ip): http = urllib3.PoolManager() output=None url = f"http://{ip}/rpc/Switch.GetStatus?id=0" try: response = http.request('GET', url, headers={'Content-Type': 'application/json'}) output=json.loads(response.data.decode('utf-8'))["output"] except urllib3.exceptions.MaxRetryError as e: print(f"Problemi nel contattare lo shelly: {e.reason}!") sys.exit(-1) return output def turn_on(ip): http = urllib3.PoolManager() was_on=None url = f"http://{ip}/rpc/Switch.Set" data = { 'id': '0', 'on': True } encoded_data = json.dumps(data).encode('utf-8') try: response = http.request('POST', url, body=encoded_data, headers={'Content-Type': 'application/json'}) was_on=json.loads(response.data.decode('utf-8')) except urllib3.exceptions.MaxRetryError as e: print(f"Problemi nel contattare lo shelly: {e.reason}!") sys.exit(-1) return was_on["was_on"] def turn_off(ip): http = urllib3.PoolManager() was_on=None url = f"http://{ip}/rpc/Switch.Set" data = { 'id': '0', 'on': False } encoded_data = json.dumps(data).encode('utf-8') try: response = http.request('POST', url, body=encoded_data, headers={'Content-Type': 'application/json'}) was_on=json.loads(response.data.decode('utf-8')) except urllib3.exceptions.MaxRetryError as e: print(f"Problemi nel contattare lo shelly: {e.reason}!") sys.exit(-1) return was_on["was_on"] #if (turn_off(ip)==False): # print("era già spento.") #if (turn_on(ip)==True): # print("era già acceso.") print("Status: %s" % get_status(ip))
Modalità toggle
Se è spento lo accende e viceversa.
curl http://<ip>/rpc/Switch.Toggle?id=0
programmazione/python/shelly1pm_mini.txt · Ultima modifica: 14/05/2025 08:51 da Fabio Di Matteo