Skip to content

Commit

Permalink
add capacity and daily output
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarv committed Jan 17, 2024
1 parent 445def5 commit deb3ed4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
2 changes: 1 addition & 1 deletion custom_components/bwt_perla/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"homekit": {},
"iot_class": "local_polling",
"issue_tracker": "https://github.com/dkarv/bwt_api/issues",
"requirements": ["bwt_api==0.4.1"],
"requirements": ["bwt_api==0.4.2"],
"ssdp": [],
"zeroconf": []
}
73 changes: 65 additions & 8 deletions custom_components/bwt_perla/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
from zoneinfo import ZoneInfo

from bwt_api.api import treated_to_blended
from bwt_api.data import BwtStatus
from bwt_api.exception import WrongCodeException

Expand Down Expand Up @@ -114,6 +115,36 @@ async def async_setup_entry(
),
HolidayModeSensor(coordinator),
HolidayStartSensor(coordinator),
CalculatedSensor(
coordinator,
"day_output",
UnitOfVolume.LITERS,
lambda data: data.treated_day,
),
CalculatedSensor(
coordinator,
"month_output",
UnitOfVolume.LITERS,
lambda data: data.treated_month,
),
CalculatedSensor(
coordinator,
"year_output",
UnitOfVolume.LITERS,
lambda data: data.treated_year,
),
CalculatedSensor(
coordinator,
"capacity_1",
UnitOfVolume.MILLILITERS,
lambda data: data.capacity_1,
),
CalculatedSensor(
coordinator,
"capacity_2",
UnitOfVolume.MILLILITERS,
lambda data: data.capacity_2,
),
]
)

Expand All @@ -122,13 +153,13 @@ async def async_setup_entry(
GAUGE_ICON = "mdi:gauge"


class TotalOutputSensor(CoordinatorEntity, SensorEntity):
class TotalOutputSensor(CoordinatorEntity[BwtCoordinator], SensorEntity):
"""Total water [liter] that passed through the output."""

_attr_icon = WATER_ICON
_attr_native_unit_of_measurement = UnitOfVolume.LITERS
_attr_device_class = SensorDeviceClass.WATER
_attr_state_class = SensorStateClass.TOTAL_INCREASING
_attr_state_class = SensorStateClass.TOTAL

def __init__(self, coordinator) -> None:
"""Initialize the sensor with the common coordinator."""
Expand All @@ -143,7 +174,7 @@ def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()


class ErrorSensor(CoordinatorEntity, SensorEntity):
class ErrorSensor(CoordinatorEntity[BwtCoordinator], SensorEntity):
"""Errors reported by the device."""

def __init__(self, coordinator) -> None:
Expand All @@ -160,7 +191,7 @@ def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()


class WarningSensor(CoordinatorEntity, SensorEntity):
class WarningSensor(CoordinatorEntity[BwtCoordinator], SensorEntity):
"""Warnings reported by the device."""

def __init__(self, coordinator) -> None:
Expand All @@ -177,7 +208,7 @@ def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()


class SimpleSensor(CoordinatorEntity, SensorEntity):
class SimpleSensor(CoordinatorEntity[BwtCoordinator], SensorEntity):
"""Simplest sensor with least configuration options."""

def __init__(self, coordinator: BwtCoordinator, key: str, extract) -> None:
Expand Down Expand Up @@ -221,7 +252,7 @@ def __init__(
self._attr_state_class = SensorStateClass.MEASUREMENT


class StateSensor(CoordinatorEntity, SensorEntity):
class StateSensor(CoordinatorEntity[BwtCoordinator], SensorEntity):
"""State of the machine."""

_attr_device_class = SensorDeviceClass.ENUM
Expand All @@ -240,7 +271,7 @@ def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()


class HolidayModeSensor(CoordinatorEntity, BinarySensorEntity):
class HolidayModeSensor(CoordinatorEntity[BwtCoordinator], BinarySensorEntity):
"""Current holiday mode state."""

def __init__(self, coordinator) -> None:
Expand All @@ -256,7 +287,7 @@ def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()


class HolidayStartSensor(CoordinatorEntity, SensorEntity):
class HolidayStartSensor(CoordinatorEntity[BwtCoordinator], SensorEntity):
"""Future start of holiday mode if active."""

_attr_device_class = SensorDeviceClass.TIMESTAMP
Expand All @@ -278,3 +309,29 @@ def _handle_coordinator_update(self) -> None:
else:
self._attr_native_value = None
self.async_write_ha_state()


class CalculatedSensor(CoordinatorEntity[BwtCoordinator], SensorEntity):
"""Sensor calculating blended water from treated water."""

_attr_state_class = SensorStateClass.TOTAL_INCREASING
suggested_display_precision = 0
suggested_unit_of_measurement = UnitOfVolume.LITERS

def __init__(self, coordinator, key: str, unit: UnitOfVolume, extract) -> None:
"""Initialize the sensor with the common coordinator."""
super().__init__(coordinator)
self._attr_translation_key = key
self._attr_unique_id = self._attr_translation_key
self._attr_native_unit_of_measurement = unit
self._extract = extract

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr_native_value = treated_to_blended(
self._extract(self.coordinator.data),
self.coordinator.data.in_hardness.dH,
self.coordinator.data.out_hardness.dH,
)
self.async_write_ha_state()

0 comments on commit deb3ed4

Please sign in to comment.