Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App hassgraph: show lowest/highest in local time #2710

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 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,10 @@ def calculate_hourly_average(data):

return list(hourly_averages.items())

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

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

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

if timezone:
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 +164,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 +348,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
Loading