ha integration
This commit is contained in:
@@ -24,11 +24,8 @@ class API:
|
||||
@self.app.route('/api')
|
||||
@cross_origin()
|
||||
def serve_root():
|
||||
workdir, filename = os.path.split(os.path.abspath(__file__))
|
||||
update = check_for_update()
|
||||
files = os.listdir(f'{workdir}/data')
|
||||
for file in files:
|
||||
if not file.endswith('.json'): files.remove(file)
|
||||
|
||||
root_dict = {
|
||||
"version": {
|
||||
"version": os.getenv('VERSION'),
|
||||
@@ -40,7 +37,7 @@ class API:
|
||||
"name": os.getenv('NAME'),
|
||||
"info": {
|
||||
"files_size_sum": self.get_file_size(),
|
||||
"files": files
|
||||
"devices": self.get_files()
|
||||
}
|
||||
}
|
||||
return jsonify(root_dict)
|
||||
@@ -49,14 +46,15 @@ class API:
|
||||
@cross_origin()
|
||||
def serve_json(path):
|
||||
workdir, filename = os.path.split(os.path.abspath(__file__))
|
||||
path += '.json'
|
||||
return send_from_directory(f'{workdir}/data', path)
|
||||
|
||||
@self.app.route('/api/state/<path:path>')
|
||||
@cross_origin()
|
||||
def serve_entity_state(path):
|
||||
workdir, filename = os.path.split(os.path.abspath(__file__))
|
||||
path += '.json'
|
||||
state = json.load(open(f'{workdir}/data/{path}', mode='r'))
|
||||
print(state['measurements'])
|
||||
entity_state = LogEntry(data=state)
|
||||
return jsonify(entity_state.to_json())
|
||||
|
||||
@@ -74,6 +72,15 @@ class API:
|
||||
if file.endswith('.json'): sizes += os.path.getsize(f'{workdir}/data/{file}')
|
||||
return sizes
|
||||
|
||||
def get_files(self):
|
||||
workdir, filename = os.path.split(os.path.abspath(__file__))
|
||||
files = os.listdir(f'{workdir}/data')
|
||||
files_list = []
|
||||
for file in files:
|
||||
if file.endswith('.json'):
|
||||
files_list.append(file.replace('.json', ''))
|
||||
return files_list
|
||||
|
||||
|
||||
api = API()
|
||||
api.app.run(host='0.0.0.0', port=8000)
|
||||
|
||||
@@ -43,12 +43,12 @@ class LogEntry:
|
||||
def __init__(self, data):
|
||||
self.name = data['name']
|
||||
self.room = data['room']
|
||||
self.timestamp = data['measurements'][:-1]['timestamp']
|
||||
self.temperature = data['measurements'][:-1]['temperature']
|
||||
self.humidity = data['measurements'][:-1]['humidity']
|
||||
self.battery_percent = data['measurements'][:-1]['battery_percent']
|
||||
self.battery_volt = data['measurements'][:-1]['battery_volt']
|
||||
self.rssi = data['measurements'][:-1]['rssi']
|
||||
self.timestamp = data['measurements'][-1]['timestamp']
|
||||
self.temperature = data['measurements'][-1]['temperature']
|
||||
self.humidity = data['measurements'][-1]['humidity']
|
||||
self.battery_percent = data['measurements'][-1]['battery_percent']
|
||||
self.battery_volt = data['measurements'][-1]['battery_volt']
|
||||
self.rssi = data['measurements'][-1]['rssi']
|
||||
|
||||
def to_json(self):
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user