From db7b4aa627482e9834d3425e113ad7278816237c Mon Sep 17 00:00:00 2001 From: Konstantin Deev Date: Sun, 20 Aug 2023 11:50:10 -0500 Subject: [PATCH] ok --- custom_components/eyeonwater/eow.py | 14 ++++++------- custom_components/eyeonwater/sensor.py | 27 +++++++++++++++++--------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/custom_components/eyeonwater/eow.py b/custom_components/eyeonwater/eow.py index d3d38cf..e8205c0 100644 --- a/custom_components/eyeonwater/eow.py +++ b/custom_components/eyeonwater/eow.py @@ -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: @@ -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 diff --git a/custom_components/eyeonwater/sensor.py b/custom_components/eyeonwater/sensor.py index 0d1df4f..e77b2ef 100644 --- a/custom_components/eyeonwater/sensor.py +++ b/custom_components/eyeonwater/sensor.py @@ -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 @@ -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() @@ -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 ] @@ -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 @@ -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 ]