36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
import logging
|
|
import requests
|
|
import time
|
|
import config
|
|
|
|
from src.generic.http_api import Http_api
|
|
|
|
class Hass(Http_api):
|
|
|
|
def __init__(self,url,token):
|
|
super().__init__('Hass',url,token)
|
|
|
|
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):
|
|
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() ]
|
|
|
|
def light_off(self,light_name):
|
|
entity_id = config.hass_entities[light_name]
|
|
self.build_request('api/services/light/turn_off','post',{"entity_id": entity_id})
|
|
|
|
def light_on(self,light_name):
|
|
entity_id = config.hass_entities[light_name]
|
|
self.build_request('api/services/light/turn_on','post',{"entity_id": entity_id})
|