Skip to content

Commit

Permalink
3.x branch opened.
Browse files Browse the repository at this point in the history
Everything can change, API changes everywhere
  • Loading branch information
ldotlopez committed Feb 8, 2024
1 parent b5ca6c2 commit cea6064
Show file tree
Hide file tree
Showing 10 changed files with 473 additions and 334 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*.egg-info/
*.pyc
*.sublime-project
*.sublime-workspace
__pycache__
dist/
home-assistant-historical-sensor.sublime-workspace
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ name = "pypi"
[packages]

[dev-packages]
# homeassistant-historical-sensor = {editable = true, path = "."}
black = "*"
build = "*"
homeassistant = ">=2024.1.0"
homeassistant-historical-sensor = {editable = true, path = "."}
ipdb = "*"
ipython = "*"
isort = "*"
Expand Down
4 changes: 2 additions & 2 deletions custom_components/delorian/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/ldotlopez/ha-historical-sensor/issues",
"requirements": [
"homeassistant-historical-sensor==2.0.0rc4"
"homeassistant-historical-sensor==3.0.0a1"
],
"version": "2.0.0rc4"
"version": "3.0.0a1"
}
45 changes: 26 additions & 19 deletions custom_components/delorian/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
# Important methods include comments about code itself and reasons behind them
#

import itertools
import math
import statistics
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo

from homeassistant.components.recorder.models import StatisticData, StatisticMetaData
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
Expand All @@ -41,6 +42,7 @@
HistoricalState,
PollUpdateMixin,
)
from homeassistant_historical_sensor.state import group_by_interval

from .api import API
from .const import DOMAIN, NAME
Expand Down Expand Up @@ -92,18 +94,29 @@ async def async_update_historical(self):
# This functions is equivaled to the `Sensor.async_update` from
# HomeAssistant core
#
# Important: You must provide datetime with tzinfo
# Important: ts is in UTC

hist_states = [
upstream_data = self.api.fetch(
start=datetime.now() - timedelta(days=3), step=timedelta(minutes=15)
)

upstream_data_with_timestamps = [
(
dt.timestamp() if dt.tzinfo else dtutil.as_local(dt).timestamp(),
state,
)
for (dt, state) in upstream_data
]

historical_states = [
HistoricalState(
state=state,
dt=dtutil.as_local(dt), # Add tzinfo, required by HistoricalSensor
)
for (dt, state) in self.api.fetch(
start=datetime.now() - timedelta(days=3), step=timedelta(minutes=15)
ts=ts,
)
for (ts, state) in upstream_data_with_timestamps
]
self._attr_historical_states = hist_states

self._attr_historical_states = historical_states

@property
def statistic_id(self) -> str:
Expand Down Expand Up @@ -131,24 +144,18 @@ async def async_calculate_statistic_data(

accumulated = latest["sum"] if latest else 0

def hour_block_for_hist_state(hist_state: HistoricalState) -> datetime:
# XX:00:00 states belongs to previous hour block
if hist_state.dt.minute == 0 and hist_state.dt.second == 0:
dt = hist_state.dt - timedelta(hours=1)
return dt.replace(minute=0, second=0, microsecond=0)

else:
return hist_state.dt.replace(minute=0, second=0, microsecond=0)

ret = []
for dt, collection_it in itertools.groupby(
hist_states, key=hour_block_for_hist_state
for block_ts, collection_it in group_by_interval(
hist_states, granurality=60 * 60
):
collection = list(collection_it)

mean = statistics.mean([x.state for x in collection])
partial_sum = sum([x.state for x in collection])
accumulated = accumulated + partial_sum

dt = datetime.fromtimestamp(block_ts).replace(tzinfo=ZoneInfo("UTC"))

ret.append(
StatisticData(
start=dt,
Expand Down
40 changes: 40 additions & 0 deletions ha-historical-sensor.sublime-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"folders":
[
{
"file_exclude_patterns": [
"*.pyc",
"*.swp",
"Pipfile.lock"
],
"folder_exclude_patterns": [
"*.egg-info",
".mypy_cache",
".venv",
"__pycache__",
"dist",
],
"follow_symlinks": true,
"path": ".",
}
],
"settings": {
"python_interpreter": "${project_path}/.venv/bin/python",

"sublack.black_command": "${project_path}/.venv/bin/black",
"sublack.black_on_save": true,

"isort.sort_on_save": false,

"SublimeLinter.linters.flake8.executable": "${project_path}/.venv/bin/flake8",
"SublimeLinter.linters.flake8.disable": false,

"SublimeLinter.linters.mypy.executable": "${project_path}/.venv/bin/mypy",
"SublimeLinter.linters.mypy.disable": false,
// "SublimeLinter.linters.mypy.args": ["--ignore-missing-imports"],

"SublimeLinter.linters.pycodestyle.executable": "${project_path}/.venv/bin/pycodestyle",
"SublimeLinter.linters.pycodestyle.disable": true,
}

}
161 changes: 0 additions & 161 deletions homeassistant_historical_sensor/recorderutil.py

This file was deleted.

Loading

0 comments on commit cea6064

Please sign in to comment.