Difference between revisions of "Home Assistant - Python API Client"
From ETCwiki
Jump to navigationJump to search (Created page with "This script allows us to access our Home Assistant server (HASS) through Python scripts. I posted this on GitHub and will keep it more updated there, so please check it out th...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
This script allows us to access our Home Assistant server (HASS) through Python scripts. I posted this on GitHub and will keep it more updated there, so please check it out there. | This script allows us to access our Home Assistant server (HASS) through Python scripts. I posted this on GitHub and will keep it more updated there, so please check it out there. | ||
− | https://github.com/ddxfish/home-assistant-python-api-client | + | |
+ | == Updated Python API Client == | ||
+ | Go here. It is more updated. | ||
+ | * https://github.com/ddxfish/home-assistant-python-api-client | ||
+ | |||
+ | |||
+ | == Use the GitHub Version instead of this == | ||
+ | Updates get put in GitHub. https://github.com/ddxfish/home-assistant-python-api-client/ | ||
Line 58: | Line 65: | ||
return result | return result | ||
+ | == Examples == | ||
+ | Examples moved to GitHub: https://github.com/ddxfish/home-assistant-python-api-client/blob/master/hass-api-client.py | ||
== External Links == | == External Links == | ||
* https://github.com/ddxfish/home-assistant-python-api-client/blob/master/hass-api-client.py - Python API Client - Github - more updated version of this | * https://github.com/ddxfish/home-assistant-python-api-client/blob/master/hass-api-client.py - Python API Client - Github - more updated version of this | ||
+ | |||
+ | |||
+ | [[Category:Home Assistant]] |
Latest revision as of 19:39, 24 October 2021
This script allows us to access our Home Assistant server (HASS) through Python scripts. I posted this on GitHub and will keep it more updated there, so please check it out there.
Contents
Updated Python API Client
Go here. It is more updated.
Use the GitHub Version instead of this
Updates get put in GitHub. https://github.com/ddxfish/home-assistant-python-api-client/
from requests import get, post import json class HassRestAPIClient: def __init__(self): #Change to your Home Assistant API URL (ex. http://192.168.1.100:8123/api ) self.hassurl = "http://192.168.1.100:8123/api" #Make sure you enter your own Long Time Access token (Bearer token) from Home Assistant self.headers = { "Authorization": "Bearer BEARERTOKENGOESHERE", "content-type": "application/json", } #Get information from home assistant states def getHassStates(self, entitysearch="", key="all", exactmatch=0): url = self.hassurl + "/states" response = get(url, headers=self.headers) entities = response.json() result = [] for entity in entities: if exactmatch == 0: if entitysearch.lower() in entity['entity_id'].lower() or entitysearch.lower() in entity['attributes']['friendly_name'].lower(): if key == "all": result.append(entity) if key == "basic": result.append([entity['entity_id'],entity['attributes']['friendly_name'],entity['state']]) else: try: result.append(entity[key]) except KeyError: try: result.append(entity['attributes'][key]) except KeyError: result.append("no key named " + key) if exactmatch == 1: if entitysearch.lower() == entity['entity_id'].lower() or entitysearch.lower() == entity['attributes']['friendly_name'].lower(): if key == "all": return entity if key == "basic": return [entity['entity_id'],entity['attributes']['friendly_name'],entity['state']] else: try: return entity[key] except KeyError: try: return entity['attributes'][key] except KeyError: return "no key named " + key return result #Custom service call def callHassService(self, entity_id, service, action): payload = json.dumps({"entity_id": entity_id}) entityurl = self.hassurl + "/services/" + service + "/" + action result = post(entityurl, headers=self.headers, data=payload) return result
Examples
Examples moved to GitHub: https://github.com/ddxfish/home-assistant-python-api-client/blob/master/hass-api-client.py
External Links
- https://github.com/ddxfish/home-assistant-python-api-client/blob/master/hass-api-client.py - Python API Client - Github - more updated version of this