Skip to content

Commit

Permalink
feat: Added argument for "Data from last x hours"
Browse files Browse the repository at this point in the history
- For better interop with automations
- Argument "Start" overrides this
  • Loading branch information
davidrapan committed Jul 1, 2024
1 parent 77bcc18 commit c72f365
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
12 changes: 7 additions & 5 deletions custom_components/history_services/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging

from pathlib import Path
from datetime import datetime as dt
from datetime import datetime as dt, timedelta as td

from homeassistant.core import HomeAssistant, ServiceCall, ServiceResponse, SupportsResponse, callback, valid_entity_id
from homeassistant.components.recorder import history
Expand All @@ -20,13 +20,15 @@ def open_file(filepath, mode, x):
return x(file)

def get_significant_states(hass: HomeAssistant, call: ServiceCall):
now = dt_util.utcnow()
start_time = now - ONE_DAY
end_time = now

entity_id = call.data["entity_id"]
entity_ids = list([entity_id])

last_hours = call.data["last_hours"]

now = dt_util.utcnow()
start_time = now - td(hours = last_hours)
end_time = now

if "start" in call.data:
start_time = dt_util.as_utc(call.data["start"])

Expand Down
1 change: 1 addition & 0 deletions custom_components/history_services/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

SERVICE_SCHEMA = vol.Schema({
vol.Required("entity_id"): cv.entity_id,
vol.Required("last_hours"): int,
vol.Optional("start"): cv.datetime,
vol.Optional("end"): cv.datetime,
})
Expand Down
2 changes: 1 addition & 1 deletion custom_components/history_services/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/davidrapan/ha-history/issues",
"requirements": ["simplekml"],
"version": "0.1.1"
"version": "0.1.2"
}
18 changes: 18 additions & 0 deletions custom_components/history_services/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ export:
required: true
selector:
entity:
last_hours:
name: Hours
description: "Data from last X hours"
required: true
default: 24
selector:
number:
min: 1
max: 120
start:
name: Start
description: "Beginning of the interval"
Expand All @@ -31,6 +40,15 @@ export_device_tracker:
selector:
entity:
domain: device_tracker
last_hours:
name: Hours
description: "Data from last X hours"
required: true
default: 24
selector:
number:
min: 1
max: 120
start:
name: Start
description: "Beginning of the interval"
Expand Down

0 comments on commit c72f365

Please sign in to comment.