Browse Source

Implement get and list hass entities

master
Tjiho 8 months ago
parent
commit
7f532d50fb
  1. 1
      .gitignore
  2. 2
      main.py
  3. 1
      requirements.txt
  4. 28
      src/hass.py

1
.gitignore

@ -2,3 +2,4 @@ __pycache__
env
venv
*.swp
config.py

2
main.py

@ -14,6 +14,8 @@ logging.basicConfig(
walle = Hass(config.hass_url,config.hass_token)
print(walle.get('device_tracker.winnie'))
Yoda(config.mqtt_ip)
Yoda(config.mqtt_ip)

1
requirements.txt

@ -1 +1,2 @@
paho-mqtt<1.6
requests<2.26

28
src/hass.py

@ -1,18 +1,38 @@
import logging
import requests
class Hass():
def __init__(self,url,token):
self.url = url
self.token = token
logging.info("Hass ok")
def build_request(self,endpoint,content,method = 'get'):
api_status = self.build_request('api/')
if api_status.status_code != 200:
logging.warning('Hass not available')
def build_request(self,endpoint,method = 'get', content = {}):
url = self.url+endpoint
headers = {
"Authorization": "Bearer "+self.token,
"content-type": "application/json",
}
if method == 'get':
response = get(url, headers=headers)
print(response.text)
response = requests.get(url, headers=headers)
return response
def get(self, id):
response = self.build_request('api/states/'+id)
if response.status_code != 200:
logging.warning('Id not found')
return {}
else:
return response.json()
def list_entities(self, id):
response = self.build_request('api/states')
if response.status_code != 200:
logging.warning('Not found')
return {}
else:
return [ entity['entity_id'] for entity in response.json() ]
Loading…
Cancel
Save