immersive-home/app/lib/home_apis/hass_ws/handlers/history.gd

42 lines
978 B
GDScript3
Raw Normal View History

2024-04-11 17:51:30 +03:00
const HASS_API = preload ("../hass.gd")
var api: HASS_API
var integration_exists: bool = false
func _init(hass: HASS_API):
self.api = hass
func get_history(entity_id: String, start: String, interval: String="5minute", end=null):
2024-05-20 13:53:22 +03:00
var meta_response = await api.connection.send_request_packet({
2024-04-11 17:51:30 +03:00
"type": "recorder/get_statistics_metadata",
"statistic_ids": [
entity_id
]
})
if meta_response.status != OK:
return null
2024-05-20 13:53:22 +03:00
var data_response = await api.connection.send_request_packet({
2024-04-11 17:51:30 +03:00
"type": "recorder/statistics_during_period",
"start_time": start,
"statistic_ids": [
entity_id
],
"period": interval,
2024-04-11 17:51:30 +03:00
"types": [
"state",
"mean"
]
})
if data_response.status != OK:
return null
2024-04-11 17:51:30 +03:00
return {
"unit": meta_response.payload.result[0]["display_unit_of_measurement"],
"has_mean": meta_response.payload.result[0]["has_mean"],
"unit_class": meta_response.payload.result[0]["unit_class"],
"data": data_response.payload.result[entity_id]
2024-05-06 19:22:12 +03:00
}