diff --git a/custom_components/history_services/common.py b/custom_components/history_services/common.py index 659c8ce..d5405e9 100644 --- a/custom_components/history_services/common.py +++ b/custom_components/history_services/common.py @@ -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 @@ -20,19 +20,21 @@ 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]) - if "start" in call.data: - start_time = dt_util.as_utc(call.data["start"]) + last_hours = call.data["last_hours"] + now = dt_util.utcnow() + + end_time = now if "end" in call.data: end_time = dt_util.as_utc(call.data["end"]) + start_time = end_time - td(hours = last_hours) + if "start" in call.data: + start_time = dt_util.as_utc(call.data["start"]) + start_time_local = dt_util.as_local(start_time).isoformat() end_time_local = dt_util.as_local(end_time).isoformat() timespan = { "start": start_time_local, "end": end_time_local } diff --git a/custom_components/history_services/const.py b/custom_components/history_services/const.py index 6431e52..395a444 100644 --- a/custom_components/history_services/const.py +++ b/custom_components/history_services/const.py @@ -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, }) diff --git a/custom_components/history_services/manifest.json b/custom_components/history_services/manifest.json index b676cd3..39c6b16 100644 --- a/custom_components/history_services/manifest.json +++ b/custom_components/history_services/manifest.json @@ -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" } diff --git a/custom_components/history_services/services.yaml b/custom_components/history_services/services.yaml index 225f3e1..4965bdb 100644 --- a/custom_components/history_services/services.yaml +++ b/custom_components/history_services/services.yaml @@ -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" @@ -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"