Skip to content

Commit

Permalink
normalize everything
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeyev committed Jun 25, 2024
1 parent b6eb958 commit 0e79689
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
3 changes: 2 additions & 1 deletion custom_components/eyeonwater/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ def __init__(
)
self.meter = meter
self._uuid = normalize_id(meter.meter_uuid)
self._id = normalize_id(meter.meter_id)
self._state = False
self._available = False
self._attr_unique_id = f"{description.key}_{self._uuid}"
self._attr_is_on = self._state
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._uuid)},
name=f"{WATER_METER_NAME} {self.meter.meter_id}",
name=f"{WATER_METER_NAME} {self._id}",
model=self.meter.meter_info.reading.model,
manufacturer=self.meter.meter_info.reading.customer_name,
hw_version=self.meter.meter_info.reading.hardware_version,
Expand Down
11 changes: 7 additions & 4 deletions custom_components/eyeonwater/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ def __init__(
super().__init__(coordinator)
self.meter = meter
self._uuid = normalize_id(meter.meter_uuid)
self._id = normalize_id(meter.meter_id)

self._state: pyonwater.DataPoint | None = None
self._available = False
self._historical_sensor = True

self._attr_name = f"{WATER_METER_NAME} {self.meter.meter_id} Statistic"
self._attr_name = f"{WATER_METER_NAME} {self._id} Statistic"
self._attr_device_class = SensorDeviceClass.WATER
self._attr_unique_id = f"{self._uuid}_statistic"
self._attr_native_unit_of_measurement = get_ha_native_unit_of_measurement(
Expand All @@ -96,7 +97,7 @@ def __init__(
self._attr_suggested_display_precision = 0
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._uuid)},
name=f"{WATER_METER_NAME} {self.meter.meter_id}",
name=f"{WATER_METER_NAME} {self._id}",
model=self.meter.meter_info.reading.model,
manufacturer=self.meter.meter_info.reading.customer_name,
hw_version=self.meter.meter_info.reading.hardware_version,
Expand Down Expand Up @@ -177,11 +178,12 @@ def __init__(
super().__init__(coordinator)
self.meter = meter
self._uuid = normalize_id(meter.meter_uuid)
self._id = normalize_id(meter.meter_id)

self._attr_unique_id = f"{self._uuid}_temperature"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._uuid)},
name=f"{WATER_METER_NAME} {self.meter.meter_id}",
name=f"{WATER_METER_NAME} {self._id}",
model=self.meter.meter_info.reading.model,
manufacturer=self.meter.meter_info.reading.customer_name,
hw_version=self.meter.meter_info.reading.hardware_version,
Expand Down Expand Up @@ -217,6 +219,7 @@ def __init__(
super().__init__(coordinator)
self.meter = meter
self._uuid = normalize_id(meter.meter_uuid)
self._id = normalize_id(meter.meter_id)

self._state: pyonwater.DataPoint | None = None
self._available = False
Expand All @@ -228,7 +231,7 @@ def __init__(
self._attr_suggested_display_precision = 0
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._uuid)},
name=f"{WATER_METER_NAME} {self.meter.meter_id}",
name=f"{WATER_METER_NAME} {self._id}",
model=self.meter.meter_info.reading.model,
manufacturer=self.meter.meter_info.reading.customer_name,
hw_version=self.meter.meter_info.reading.hardware_version,
Expand Down
11 changes: 6 additions & 5 deletions custom_components/eyeonwater/statistic_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def get_ha_native_unit_of_measurement(unit: pyonwater.NativeUnits):

def get_statistic_name(meter_id: str) -> str:
"""Generate statistic name for a meter."""
meter_id = normalize_id(meter_id)
return f"{WATER_METER_NAME} {meter_id} Statistic"


Expand All @@ -51,16 +52,16 @@ def normalize_id(uuid: str) -> str:
return uuid.lower()


def get_statistics_id(meter_uuid: str) -> str:
def get_statistics_id(meter_id: str) -> str:
"""Generate statistic ID for a meter."""
meter_uuid = normalize_id(meter_uuid)
return f"sensor.water_meter_{meter_uuid}_statistic"
meter_id = normalize_id(meter_id)
return f"sensor.water_meter_{meter_id}_statistic"


def get_statistic_metadata(meter: Meter) -> StatisticMetaData:
"""Build statistic metadata for a given meter."""
name = get_statistic_name(meter_id=meter.meter_id)
statistic_id = get_statistics_id(meter.meter_uuid) # should it be meter id or uuid?
statistic_id = get_statistics_id(meter.meter_id) # if should be meter id

if not valid_entity_id(statistic_id):
msg = "Invalid statistic_id {statistic_id} for meter {meter.meter_id}"
Expand Down Expand Up @@ -97,7 +98,7 @@ async def get_last_imported_time(
"""Return last imported data datetime."""
# https://github.com/home-assistant/core/blob/74e2d5c5c312cf3ba154b5206ceb19ba884c6fb4/homeassistant/components/tibber/sensor.py#L11

statistic_id = get_statistics_id(meter.meter_uuid) # should it be meter id or uuid?
statistic_id = get_statistics_id(meter.meter_id) # if should be meter id
last_stats = await get_instance(hass).async_add_executor_job(
get_last_statistics,
hass,
Expand Down

0 comments on commit 0e79689

Please sign in to comment.