Skip to content

Commit

Permalink
Development branch. Testing ha-historical-sensors 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ldotlopez committed Feb 8, 2024
1 parent 88cc9ce commit 446adc8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 350 deletions.
3 changes: 3 additions & 0 deletions custom_components/ideenergy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@


from datetime import timedelta
from zoneinfo import ZoneInfo

DOMAIN = "ideenergy"

CONF_CONTRACT = "contract"

LOCAL_TZ = ZoneInfo("Europe/Madrid")

MEASURE_MAX_AGE = 60 * 50 # Fifty minutes
MAX_RETRIES = 3
MIN_SCAN_INTERVAL = 60
Expand Down
22 changes: 21 additions & 1 deletion custom_components/ideenergy/datacoordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
from datetime import datetime, timedelta, timezone
from typing import Any, TypedDict

import ideenergy
from homeassistant.core import dt_util
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

import ideenergy

from .barrier import Barrier, BarrierDeniedError
from .const import (
DATA_ATTR_HISTORICAL_CONSUMPTION,
Expand All @@ -33,6 +34,7 @@
DATA_ATTR_MEASURE_ACCUMULATED,
DATA_ATTR_MEASURE_INSTANT,
HISTORICAL_PERIOD_LENGHT,
LOCAL_TZ,
)
from .entity import IDeEntity

Expand Down Expand Up @@ -242,7 +244,9 @@ async def get_direct_reading_data(self) -> dict[str, int | float]:
async def get_historical_consumption_data(self) -> Any:
end = datetime.today()
start = end - HISTORICAL_PERIOD_LENGHT

data = await self.api.get_historical_consumption(start=start, end=end)
data.periods = [normalize_period_item(x) for x in data.periods]

return {DATA_ATTR_HISTORICAL_CONSUMPTION: data}

Expand All @@ -251,9 +255,25 @@ async def get_historical_generation_data(self) -> Any:
start = end - HISTORICAL_PERIOD_LENGHT
data = await self.api.get_historical_generation(start=start, end=end)

raise NotImplementedError()

return {DATA_ATTR_HISTORICAL_GENERATION: data}

async def get_historical_power_demand_data(self) -> Any:
data = await self.api.get_historical_power_demand()
data.demands = [normalize_dated_item(x) for x in data.demands]

return {DATA_ATTR_HISTORICAL_POWER_DEMAND: data}


def normalize_period_item(item):
item.start = item.start.replace(tzinfo=LOCAL_TZ)
item.end = item.end.replace(tzinfo=LOCAL_TZ)

return item


def normalize_dated_item(item):
item.dt = item.dt.replace(tzinfo=LOCAL_TZ)

return item
6 changes: 3 additions & 3 deletions custom_components/ideenergy/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify
from homeassistant_historical_sensor.recorderutil import (
delete_entity_invalid_states,
from homeassistant_historical_sensor.timemachine import (
delete_invalid_states,
hass_recorder_session,
)

Expand Down Expand Up @@ -96,7 +96,7 @@ async def async_delete_invalid_states(self) -> int:

def fn():
with hass_recorder_session(self.hass) as session:
return delete_entity_invalid_states(session, self)
return delete_invalid_states(session, self)

return await recorder.get_instance(self.hass).async_add_executor_job(fn)

Expand Down
239 changes: 0 additions & 239 deletions custom_components/ideenergy/fixes.py

This file was deleted.

4 changes: 2 additions & 2 deletions custom_components/ideenergy/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"issue_tracker": "https://github.com/ldotlopez/ha-ideenergy/issues",
"requirements": [
"ideenergy>=2.0.0rc1",
"homeassistant-historical-sensor==2.0.0rc5"
"homeassistant-historical-sensor==3.0.0a1"
],
"version": "2.1.2"
"version": "2024.0.0"
}
Loading

0 comments on commit 446adc8

Please sign in to comment.