Skip to content

Commit

Permalink
Add sensors for consumption data
Browse files Browse the repository at this point in the history
The integration will now request the consumption data for each appliance every
15 minutes.

This consumption data is made available in 6 sensors:
- Heating energy consumed
- Hot water energy consumed
- Cooling energy consumed
- Heating energy delivered
- Hot water energy delivered
- Cooling energy delivered
  • Loading branch information
msvisser committed Nov 19, 2023
1 parent 5945ee2 commit ae5108a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
18 changes: 18 additions & 0 deletions custom_components/remeha_home/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""API for Remeha Home bound to Home Assistant OAuth."""
import base64
import datetime
import hashlib
import json
import logging
Expand Down Expand Up @@ -130,6 +131,23 @@ async def async_get_appliance_technical_information(
response.raise_for_status()
return await response.json()

async def async_get_consumption_data_for_today(self, appliance_id: str) -> dict:
"""Get technical information for an appliance."""
today = datetime.datetime.now().replace(
hour=0, minute=0, second=0, microsecond=0
)
end_of_today = today + datetime.timedelta(hours=23, minutes=59, seconds=59)

today_string = today.strftime("%Y-%m-%d %H:%M:%S.%fZ")
end_of_today_string = end_of_today.strftime("%Y-%m-%d %H:%M:%S.%fZ")

response = await self._async_api_request(
"GET",
f"/appliances/{appliance_id}/energyconsumption/daily?startDate={today_string}&endDate={end_of_today_string}",
)
response.raise_for_status()
return await response.json()


class RemehaHomeAuthFailed(Exception):
"""Error to indicate that authentication failed."""
Expand Down
50 changes: 49 additions & 1 deletion custom_components/remeha_home/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
BinarySensorEntityDescription,
BinarySensorDeviceClass,
)
from homeassistant.const import UnitOfTemperature, UnitOfPressure
from homeassistant.const import UnitOfEnergy, UnitOfTemperature, UnitOfPressure

DOMAIN = "remeha_home"

Expand All @@ -35,6 +35,54 @@
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="consumptionData.heatingEnergyConsumed",
name="Heating Energy Consumed",
entity_registry_enabled_default=False,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="consumptionData.hotWaterEnergyConsumed",
name="Hot Water Energy Consumed",
entity_registry_enabled_default=False,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="consumptionData.coolingEnergyConsumed",
name="Cooling Energy Consumed",
entity_registry_enabled_default=False,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="consumptionData.heatingEnergyDelivered",
name="Heating Energy Delivered",
entity_registry_enabled_default=False,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="consumptionData.hotWaterEnergyDelivered",
name="Hot Water Energy Delivered",
entity_registry_enabled_default=False,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="consumptionData.coolingEnergyDelivered",
name="Cooling Energy Delivered",
entity_registry_enabled_default=False,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
]

CLIMATE_ZONE_SENSOR_TYPES = [
Expand Down
49 changes: 48 additions & 1 deletion custom_components/remeha_home/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Coordinator for fetching the Remeha Home data."""
from datetime import timedelta
from datetime import datetime, timedelta
import logging

import async_timeout
Expand Down Expand Up @@ -31,6 +31,8 @@ def __init__(self, hass: HomeAssistantType, api: RemehaHomeAPI) -> None:
self.items = {}
self.device_info = {}
self.technical_info = {}
self.appliance_consumption_data = {}
self.appliance_last_consumption_data_update = {}

async def _async_update_data(self):
"""Fetch data from API endpoint.
Expand All @@ -51,6 +53,9 @@ async def _async_update_data(self):

raise UpdateFailed from err

# Save the current time for appliance usage data updates
now = datetime.now()

for appliance in data["appliances"]:
appliance_id = appliance["applianceId"]
self.items[appliance_id] = appliance
Expand All @@ -68,6 +73,48 @@ async def _async_update_data(self):
self.technical_info[appliance_id],
)

# Only update appliance usage data every 15 minutes
if (appliance_id not in self.appliance_last_consumption_data_update) or (
now - self.appliance_last_consumption_data_update[appliance_id]
>= timedelta(minutes=14, seconds=45)
):
try:
consumption_data = (
await self.api.async_get_consumption_data_for_today(
appliance_id
)
)
self.appliance_consumption_data[appliance_id] = consumption_data[
"data"
][0]
self.appliance_last_consumption_data_update[appliance_id] = now
_LOGGER.debug(
"Requested consumption data for appliance %s: %s",
appliance_id,
consumption_data,
)
except ClientResponseError as err:
_LOGGER.warning(
"Failed to request consumption data for appliance %s: %s",
appliance_id,
err,
)

# Get the cached consumption data for the appliance or use default values
if appliance_id in self.appliance_consumption_data:
appliance["consumptionData"] = self.appliance_consumption_data[
appliance_id
]
else:
appliance["consumptionData"] = {
"heatingEnergyConsumed": 0.0,
"hotWaterEnergyConsumed": 0.0,
"coolingEnergyConsumed": 0.0,
"heatingEnergyDelivered": 0.0,
"hotWaterEnergyDelivered": 0.0,
"coolingEnergyDelivered": 0.0,
}

self.device_info[appliance_id] = DeviceInfo(
identifiers={(DOMAIN, appliance_id)},
name=appliance["houseName"],
Expand Down

0 comments on commit ae5108a

Please sign in to comment.