mirror of
https://github.com/AquaMorph/dotfiles.git
synced 2025-04-30 01:35:34 +00:00
Home Assistant post requests
This commit is contained in:
parent
89ecb534e0
commit
bc4e37a398
@ -6,12 +6,16 @@ from requests import get, post
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
class HomeAssistant(object):
|
class HomeAssistant(object):
|
||||||
|
|
||||||
|
# Initalizes Home Assistant API wrapper.
|
||||||
def __init__(self, ip, token):
|
def __init__(self, ip, token):
|
||||||
self.url = 'http://{}:8123'.format(ip)
|
self.url = 'http://{}:8123'.format(ip)
|
||||||
self.headers = {
|
self.headers = {
|
||||||
'Authorization': 'Bearer {}'.format(token),
|
'Authorization': 'Bearer {}'.format(token),
|
||||||
'content-type': 'application/json',
|
'content-type': 'application/json',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Sends post requests.
|
||||||
def postService(self, domain, service, data):
|
def postService(self, domain, service, data):
|
||||||
response = post("{}/api/services/{}/{}".format(self.url,
|
response = post("{}/api/services/{}/{}".format(self.url,
|
||||||
domain,
|
domain,
|
||||||
@ -19,10 +23,12 @@ class HomeAssistant(object):
|
|||||||
headers=self.headers, data=json.dumps(data))
|
headers=self.headers, data=json.dumps(data))
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response
|
return response
|
||||||
def getRequest(self, domain, data=''):
|
|
||||||
|
# Sends get requests and turns requested data.
|
||||||
|
def getRequest(self, domain):
|
||||||
response = get("{}/api/{}".format(self.url, domain),
|
response = get("{}/api/{}".format(self.url, domain),
|
||||||
headers=self.headers, data=json.dumps(data))
|
headers=self.headers)
|
||||||
return response.text
|
return json.loads(response.text)
|
||||||
|
|
||||||
# Returns a message if the API is up and running.
|
# Returns a message if the API is up and running.
|
||||||
def getAPI(self):
|
def getAPI(self):
|
||||||
@ -96,6 +102,34 @@ class HomeAssistant(object):
|
|||||||
def getCameraProxy(self, entityId):
|
def getCameraProxy(self, entityId):
|
||||||
return self.getRequest('camera_proxy/' + entityId)
|
return self.getRequest('camera_proxy/' + entityId)
|
||||||
|
|
||||||
|
# Runs a Home Assistant scene.
|
||||||
def runScene(self, entityId):
|
def runScene(self, entityId):
|
||||||
data = {'entity_id': entityId}
|
data = {'entity_id': entityId}
|
||||||
self.postService('scene', 'turn_on', data)
|
self.postService('scene', 'turn_on', data)
|
||||||
|
|
||||||
|
# Runs a Home Assistant script.
|
||||||
|
def runScript(self, entityId):
|
||||||
|
data = {'entity_id': entityId}
|
||||||
|
self.postService('script', 'turn_on', data)
|
||||||
|
|
||||||
|
# Sets the brightness level of a device.
|
||||||
|
def setLevel(self, entityId, level):
|
||||||
|
data = {'entity_id': entityId, 'brightness_pct': level}
|
||||||
|
self.postService('homeassistant', 'turn_on', data)
|
||||||
|
|
||||||
|
# Turns a device off.
|
||||||
|
def turnOn(self, entityId):
|
||||||
|
data = {'entity_id': entityId}
|
||||||
|
self.postService('homeassistant', 'turn_on', data)
|
||||||
|
|
||||||
|
# Turns a device on.
|
||||||
|
def turnOff(self, entityId):
|
||||||
|
data = {'entity_id': entityId}
|
||||||
|
self.postService('homeassistant', 'turn_off', data)
|
||||||
|
|
||||||
|
# Turns a device of the given power state.
|
||||||
|
def setOnOff(self, entityId, power):
|
||||||
|
if power:
|
||||||
|
self.turnOn(entityId)
|
||||||
|
else:
|
||||||
|
self.turnOff(entityId)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user