Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Deev committed Aug 20, 2023
1 parent 732bb49 commit db7b4aa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
14 changes: 7 additions & 7 deletions custom_components/eyeonwater/eow.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def get_flags(self, flag) -> bool:
def reading(self):
"""Returns the latest meter reading in gal."""
if self.last_historical_data:
return self.last_historical_data[-1]["sum"]
return self.last_historical_data[-1]["reading"]

reading = self.reading_data["latest_read"]
if READ_UNITS_FIELD not in reading:
Expand Down Expand Up @@ -239,21 +239,21 @@ async def get_historical_data(self, date: datetime, client: Client):
response_unit = d["display_unit"].upper()
statistics.append(
{
"start": timezone.localize(parser.parse(d["date"])),
"sum": self.convert(response_unit, d["bill_read"]),
"dt": timezone.localize(parser.parse(d["date"])),
"reading": self.convert(response_unit, d["bill_read"]),
}
)

for statistic in statistics:
start = statistic["start"]
if start.tzinfo is None or start.tzinfo.utcoffset(start) is None:
dt = statistic["dt"]
if dt.tzinfo is None or dt.tzinfo.utcoffset(dt) is None:
msg = "Naive timestamp"
raise Exception(msg)
if start.minute != 0 or start.second != 0 or start.microsecond != 0:
if dt.minute != 0 or dt.second != 0 or dt.microsecond != 0:
msg = "Invalid timestamp"
raise Exception(msg)

statistics.sort(key=lambda d: d["start"])
statistics.sort(key=lambda d: d["dt"])

return statistics

Expand Down
27 changes: 18 additions & 9 deletions custom_components/eyeonwater/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def __init__(self, meter: Meter) -> None:
name=f"{WATER_METER_NAME} {self.meter.meter_id}",
)

self._attr_has_entity_name = False
# self._attr_name = DOMAIN

self._attr_unique_id = meter.meter_uuid
self._attr_entity_id = meter.meter_uuid
# self._attr_entity_id = self.meter.meter_id

self._attr_has_entity_name = True
self._attr_name = None

self._attr_entity_registry_enabled_default = True
self._attr_state = None
Expand All @@ -73,6 +73,14 @@ def __init__(self, meter: Meter) -> None:
self._attr_native_unit_of_measurement = meter.native_unit_of_measurement
self._attr_device_class = SensorDeviceClass.WATER

# We DON'T opt-in for statistics (don't set state_class). Why?
#
# Those statistics are generated from a real sensor, this sensor, but we don't
# want that hass try to do anything with those statistics because we
# (HistoricalSensor) handle generation and importing
#
# self._attr_state_class = SensorStateClass.MEASUREMENT

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

Expand All @@ -84,8 +92,8 @@ async def async_added_to_hass(self) -> None:
async def async_update_historical(self):
statistics = [
HistoricalState(
dt=row["start"],
state=row["sum"],
dt=row["dt"],
state=row["reading"],
)
for row in self.meter.last_historical_data
]
Expand All @@ -104,7 +112,7 @@ def get_statistic_metadata(self) -> StatisticMetaData:
#
meta = super().get_statistic_metadata()
meta["has_sum"] = True
meta["has_mean"] = True
meta["has_mean"] = False

return meta

Expand All @@ -116,8 +124,9 @@ async def async_calculate_statistic_data(
) -> list[StatisticData]:
statistics = [
StatisticData(
dt=row["start"],
state=row["sum"],
start=row.dt,
state=row.state,
sum=row.state,
)
for row in hist_states
]
Expand Down

0 comments on commit db7b4aa

Please sign in to comment.