diff --git a/custom_components/rinnai_heater/__init__.py b/custom_components/rinnai_heater/__init__.py index fe94ace..120c9b3 100644 --- a/custom_components/rinnai_heater/__init__.py +++ b/custom_components/rinnai_heater/__init__.py @@ -10,7 +10,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers import device_registry as dr -from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, SENSORS_BUS_ARRAY, SENSORS_TELA_ARRAY, TEMPERATURES_MAP +from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, SENSORS_BUS_ARRAY, SENSORS_TELA_ARRAY, SENSORS_CONSUMO_ARRAY PLATFORMS = [Platform.SENSOR, Platform.BINARY_SENSOR, Platform.BUTTON, Platform.WATER_HEATER] @@ -116,6 +116,8 @@ def async_remove_rinnai_heater_sensor(self, update_callback): async def _async_refresh_data(self, now=None): try: await self.bus() + await self.consumo() + await self.tela() # await self.tela() except Exception as e: _LOGGER.exception("error reading heater data", exc_info=True) @@ -144,37 +146,36 @@ async def request(self, endpoint: str): except Exception as e: _LOGGER.exception( f"Error fetching /{endpoint} data", exc_info=True) + self.data = dict() # clear data on error so entities become unavailable return False finally: self._reading = False async def inc(self): - return self.update_data(await self.request("inc"), False) + return self.update_data(await self.request("inc"), SENSORS_TELA_ARRAY) async def dec(self): - return self.update_data(await self.request("dec"), False) + return self.update_data(await self.request("dec"), SENSORS_TELA_ARRAY) async def lig(self): - return self.update_data(await self.request("lig"), False) + return self.update_data(await self.request("lig"), SENSORS_TELA_ARRAY) async def bus(self): - return self.update_data(await self.request("bus"), True) + return self.update_data(await self.request("bus"), SENSORS_BUS_ARRAY) async def tela(self): - return self.update_data(await self.request("tela_"), False) + return self.update_data(await self.request("tela_"), SENSORS_TELA_ARRAY) - def update_data(self, response: list[str], bus: bool, update_entities=True): + async def consumo(self): + return self.update_data(await self.request("consumo"), SENSORS_CONSUMO_ARRAY) + + def update_data(self, response: list[str], sensors: dict[int, str], update_entities=True): if response is None: return False - arr = SENSORS_BUS_ARRAY if bus else SENSORS_TELA_ARRAY - - for address, name in arr.items(): + for address, name in sensors.items(): self.data[name] = response[address] - if not bus: - self.data["target_temperature"] = TEMPERATURES_MAP[self.data["target_temperature_raw"]] - if update_entities: for update_callback in self._sensors: update_callback() diff --git a/custom_components/rinnai_heater/const.py b/custom_components/rinnai_heater/const.py index 245ba7b..f93e44d 100644 --- a/custom_components/rinnai_heater/const.py +++ b/custom_components/rinnai_heater/const.py @@ -40,10 +40,10 @@ Sensor("actuations", 1, None, Platform.SENSOR, None, True, "mdi:shimmer", None, False), Sensor("burning_hours", 1, "h", Platform.SENSOR, SensorDeviceClass.DURATION, True, "mdi:fire", None, False), Sensor("standby_hours", 1, "h", Platform.SENSOR, SensorDeviceClass.DURATION, True, "mdi:fire-off", None, False), - Sensor("fan_diagnostic", 0.1, None, Platform.SENSOR, None, True, "mdi:fan", None, True ), + Sensor("fan_diagnostic", 0.1, None, Platform.SENSOR, None, False, "mdi:fan", None, True ), Sensor("fan_speed", 0.1, "Hz", Platform.SENSOR, SensorDeviceClass.FREQUENCY, True, "mdi:fan", None, False), Sensor("pov_current", 0.1, "mA", Platform.SENSOR, SensorDeviceClass.CURRENT, True, "mdi:current-ac", None, False), - Sensor("power", 0.01, "kcal/min", Platform.SENSOR, SensorDeviceClass.ENERGY, True, "mdi:gas-burner", None, False), + Sensor("power", 0.01, "kcal/min", Platform.SENSOR, SensorDeviceClass.POWER, True, "mdi:gas-burner", None, False), Sensor("water_inlet_temperature", 0.01, "°C", Platform.SENSOR, SensorDeviceClass.TEMPERATURE, True, "mdi:thermometer-water", None, False), Sensor("water_outlet_temperature", 0.01, "°C", Platform.SENSOR, SensorDeviceClass.TEMPERATURE, True, "mdi:thermometer-water", None, False), Sensor("water_flow", 0.01, "L/min", Platform.SENSOR, SensorDeviceClass.VOLUME_FLOW_RATE, True, "mdi:water", None, False), @@ -52,11 +52,16 @@ Sensor("target_temperature", 0.01, "°C", Platform.SENSOR, SensorDeviceClass.TEMPERATURE, True, "mdi:water-thermometer", None, False), Sensor("device_ip", None, None, Platform.SENSOR, None, True, "mdi:ip-network", None, True ), Sensor("device_ip_priority", None, None, Platform.SENSOR, None, True, "mdi:ip-network", None, True ), - Sensor("target_temperature_raw", 1, None, Platform.SENSOR, None, True, "mdi:water-thermometer", None, True ), - Sensor("serial_number", None, None, Platform.SENSOR, None, True, None, None, True ), - Sensor("uptime", 1, "s", Platform.SENSOR, SensorDeviceClass.DURATION, True, None, None, False), - Sensor("mac_address", None, None, Platform.SENSOR, None, True, "mdi:network", None, True ), + Sensor("target_temperature_raw", 1, None, Platform.SENSOR, None, False, "mdi:water-thermometer", None, True ), + Sensor("serial_number", None, None, Platform.SENSOR, None, False, None, None, True ), + Sensor("uptime", 1, "s", Platform.SENSOR, SensorDeviceClass.DURATION, False, None, None, False), + Sensor("mac_address", None, None, Platform.SENSOR, None, False, "mdi:network", None, True ), Sensor("wifi_signal", 1, "dB", Platform.SENSOR, SensorDeviceClass.SIGNAL_STRENGTH, True, None, None, True ), + + Sensor("water_usage", 1, "L", Platform.SENSOR, SensorDeviceClass.WATER, True, None, None, False), + Sensor("gas_usage", 1, "kcal", Platform.SENSOR, SensorDeviceClass.ENERGY, True, "mdi:meter-gas", None, False), + Sensor("water_usage_last_week", 1, "L", Platform.SENSOR, SensorDeviceClass.WATER, True, None, None, False), + Sensor("gas_usage_last_week", 1, "kcal", Platform.SENSOR, SensorDeviceClass.ENERGY, True, "mdi:meter-gas", None, False), ] SENSORS_BUS_ARRAY = { @@ -94,3 +99,10 @@ 7: "target_temperature_raw", 8: "uptime", } + +SENSORS_CONSUMO_ARRAY = { + 1: "water_usage", + 2: "gas_usage", + 4: "water_usage_last_week", + 5: "gas_usage_last_week", +} \ No newline at end of file diff --git a/custom_components/rinnai_heater/sensor.py b/custom_components/rinnai_heater/sensor.py index 318fb02..a3e33b4 100644 --- a/custom_components/rinnai_heater/sensor.py +++ b/custom_components/rinnai_heater/sensor.py @@ -2,7 +2,7 @@ import re from typing import Any, Dict, Optional -from homeassistant.components.sensor import SensorEntity, EntityCategory, SensorStateClass +from homeassistant.components.sensor import SensorEntity, EntityCategory, SensorStateClass, SensorDeviceClass from homeassistant.const import Platform from homeassistant.core import callback @@ -43,7 +43,10 @@ def __init__(self, heater, sensor_info): self._attr_entity_category = EntityCategory.DIAGNOSTIC if sensor_info.debug else None if self._coeff is not None: - self._attr_state_class = SensorStateClass.MEASUREMENT + if self._attr_device_class == SensorDeviceClass.WATER or self._attr_device_class == SensorDeviceClass.ENERGY: + self._attr_state_class = SensorStateClass.TOTAL_INCREASING + else: + self._attr_state_class = SensorStateClass.MEASUREMENT self._attr_suggested_display_precision = str( self._coeff).count('0')