Skip to content

Commit

Permalink
Style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ldotlopez committed Jul 13, 2023
1 parent 608e8ce commit 0ca5e2e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ verify_ssl = true
name = "pypi"

[packages]
homeassistant-historical-sensor = {editable = true, path = "."}

[dev-packages]
homeassistant = "*"
sqlalchemy = "*"
ipdb = "*"
ipython = "*"
homeassistant-historical-sensor = {editable = true, path = "."}
pre-commit = "*"
homeassistant = "*"

[requires]
python_version = "3"
6 changes: 3 additions & 3 deletions custom_components/delorian/manifest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"domain": "delorian",
"name": "Delorian - Testing integration for historical sensors",
"after_dependencies": [
"recorder"
],
"codeowners": [
"@ldotlopez"
],
"dependencies": [
"recorder"
],
"config_flow": true,
"documentation": "https://github.com/ldotlopez/ha-historical-sensor",
"iot_class": "cloud_polling",
Expand Down
11 changes: 10 additions & 1 deletion custom_components/delorian/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
#

import itertools
import logging
import statistics
from datetime import datetime, timedelta
from typing import List, Optional

from homeassistant.components import recorder
from homeassistant.components.recorder.models import StatisticData, StatisticMetaData
from homeassistant.components.recorder.statistics import StatisticsRow
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
Expand All @@ -50,6 +52,9 @@
PLATFORM = "sensor"


_LOGGER = logging.getLogger(__name__)


class Sensor(PollUpdateMixin, HistoricalSensor, SensorEntity):
#
# Base clases:
Expand Down Expand Up @@ -85,14 +90,17 @@ def __init__(self, *args, **kwargs):

self.api = API()

async def async_added_to_hass(self) -> None:
await super().async_added_to_hass()

async def async_update_historical(self):
# Fill `HistoricalSensor._attr_historical_states` with HistoricalState's
# This functions is equivaled to the `Sensor.async_update` from
# HomeAssistant core
#
# Important: You must provide datetime with tzinfo

self._attr_historical_states = [
hist_states = [
HistoricalState(
state=state,
dt=dtutil.as_local(dt), # Add tzinfo, required by HistoricalSensor
Expand All @@ -101,6 +109,7 @@ async def async_update_historical(self):
start=datetime.now() - timedelta(days=3), step=timedelta(minutes=15)
)
]
self._attr_historical_states = hist_states

@property
def statistic_id(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant_historical_sensor/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import math
import sys
from typing import Any, Dict
from typing import Any

from homeassistant.config import DATA_CUSTOMIZE
from homeassistant.const import (
Expand Down
19 changes: 15 additions & 4 deletions homeassistant_historical_sensor/recorderutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import logging
from contextlib import contextmanager
from typing import List, Optional
from typing import Literal

from homeassistant.components import recorder
from homeassistant.components.recorder import db_schema
Expand All @@ -43,15 +43,26 @@ def hass_recorder_session(hass: HomeAssistant):


async def get_last_statistics_wrapper(
hass: HomeAssistant, statistic_id: str
hass: HomeAssistant,
statistic_id: str,
*,
convert_units: bool = True,
types: set[Literal["last_reset", "max", "mean", "min", "state", "sum"]] = {
"last_reset",
"max",
"mean",
"min",
"state",
"sum",
},
) -> StatisticsRow | None:
res = await recorder.get_instance(hass).async_add_executor_job(
get_last_statistics,
hass,
1,
statistic_id,
True,
{"last_reset", "max", "mean", "min", "state", "sum"},
convert_units,
types,
)
if not res:
return None
Expand Down
1 change: 0 additions & 1 deletion homeassistant_historical_sensor/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import logging
from abc import abstractmethod
from datetime import timedelta
from typing import List, Optional

import sqlalchemy.exc
import sqlalchemy.orm
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ project_urls =
package_dir =
homeassistant-historical-sensor = homeassistant_historical_sensor
packages = find:
python_requires = >=3.9
python_requires = >=3.10


[options.packages.find]
Expand Down

0 comments on commit 0ca5e2e

Please sign in to comment.