42 lines
1.3 KiB
Python
42 lines
1.3 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]
|
|
response = self.build_request('api/services/light/turn_off',
|
|
'post', {"entity_id": entity_id})
|
|
if response.status_code != 200:
|
|
raise Exception(f"Error while turning off light {light_name}")
|
|
|
|
def light_on(self, light_name):
|
|
entity_id = config.hass_entities[light_name]
|
|
response = self.build_request(
|
|
'api/services/light/turn_on', 'post', {"entity_id": entity_id})
|
|
if response.status_code != 200:
|
|
raise Exception(f"Error while turning on light {light_name}")
|