Skip to content

Commit

Permalink
App hassgraph: show lowest/highest in local time
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaosKid42 committed Sep 23, 2024
1 parent 294d3a9 commit 38df3cc
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions apps/hassgraph/hass_graph.star
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("animation.star", "animation")
load("encoding/base64.star", "base64")
load("encoding/json.star", "json")
load("http.star", "http")
load("render.star", "render")
load("schema.star", "schema")
Expand Down Expand Up @@ -58,7 +59,15 @@ PLACEHOLDER_DATA = [

MAX_TIME_PERIOD = 24

TIME_FORMAT = "2006-01-02T15:04:05Z"

def main(config):
timezone = None
location = config.get("location")
if location:
loc = json.decode(location)
timezone = loc["timezone"]

if not config.str("ha_instance") or not config.str("ha_entity") or not config.str("ha_token"):
print("Using placeholder data, please configure the app")
data = PLACEHOLDER_DATA
Expand All @@ -79,7 +88,7 @@ def main(config):
unit = data[0]["attributes"]["unit_of_measurement"]
points = calculate_hourly_average(data)
current_value = data[-1]["state"]
stats = calc_stats(data)
stats = calc_stats(timezone, data)

return render_app(config, current_value, points, stats, unit)

Expand Down Expand Up @@ -114,7 +123,13 @@ def calculate_hourly_average(data):

return list(hourly_averages.items())

def calc_stats(data):
def localize_timestamp(timezone, timestamp):
if timezone:
return time.parse_time(timestamp).in_location(timezone).format(TIME_FORMAT)
else:
return timestamp

def calc_stats(timezone, data):
highest_value = float("-inf")
highest_timestamp = None
lowest_value = float("inf")
Expand All @@ -138,6 +153,10 @@ def calc_stats(data):

average_value = total_value / count if count else 0
average_value = (average_value * 10) // 1 / 10

lowest_timestamp = localize_timestamp(timezone, lowest_timestamp)
highest_timestamp = localize_timestamp(timezone, highest_timestamp)

return {
"lowest_value": str(lowest_value),
"lowest_time": lowest_timestamp.split("T")[1][:5] if lowest_value != float("inf") else "N/A",
Expand All @@ -147,7 +166,7 @@ def calc_stats(data):
}

def get_entity_data(config, start_time):
start_time_str = start_time.format("2006-01-02T15:04:05Z")
start_time_str = start_time.format(TIME_FORMAT)
url = config.str("ha_instance") + "/api/history/period/" + start_time_str + "?filter_entity_id=" + config.str("ha_entity")
headers = {
"Authorization": "Bearer " + config.str("ha_token"),
Expand Down Expand Up @@ -331,6 +350,12 @@ def get_schema():
icon = "list",
default = True,
),
schema.Location(
id = "location",
name = "Location",
icon = "locationDot",
desc = "Location for which to display time",
),
schema.Dropdown(
id = "icon",
default = icons[0].value,
Expand Down

0 comments on commit 38df3cc

Please sign in to comment.