Skip to content

Commit 6019d1d

Browse files
authored
Merge pull request #103 from jwillemsen/jwi-limitsensors
Add a limit sensor, each device exports the limits we get from daikin…
2 parents 66d48be + 9fc2c68 commit 6019d1d

File tree

1 file changed

+82
-25
lines changed

1 file changed

+82
-25
lines changed

custom_components/daikin_onecta/sensor.py

+82-25
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
from homeassistant.const import UnitOfEnergy
1414
from homeassistant.core import callback
1515
from homeassistant.helpers.update_coordinator import CoordinatorEntity
16+
from homeassistant.core import HomeAssistant
17+
from homeassistant.helpers.entity import EntityCategory
1618

1719
from .const import COORDINATOR
20+
from .const import DAIKIN_API
1821
from .const import DAIKIN_DEVICES
1922
from .const import DOMAIN as DAIKIN_DOMAIN
2023
from .const import ENABLED_DEFAULT
@@ -38,6 +41,7 @@ async def async_setup(hass, async_add_entities):
3841
async def async_setup_entry(hass, config_entry, async_add_entities):
3942
"""Set up Daikin climate based on config_entry."""
4043
coordinator = hass.data[DAIKIN_DOMAIN][COORDINATOR]
44+
daikin_api = hass.data[DAIKIN_DOMAIN][DAIKIN_API]
4145
sensors = []
4246
supported_management_point_types = {
4347
"domesticHotWaterTank",
@@ -46,6 +50,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
4650
"climateControlMainZone",
4751
}
4852
for dev_id, device in hass.data[DAIKIN_DOMAIN][DAIKIN_DEVICES].items():
53+
# For each rate limit we provide a sensor
54+
for name in daikin_api.rate_limits.keys():
55+
sensors.append(DaikinLimitSensor(hass, device, coordinator, name))
56+
4957
management_points = device.daikin_data.get("managementPoints", [])
5058
for management_point in management_points:
5159
management_point_type = management_point["managementPointType"]
@@ -65,15 +73,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
6573
# operationMode is handled by the HWT and ClimateControl directly, so don't create a separate sensor for that
6674
pass
6775
elif value_value is not None and not isinstance(value_value, dict):
68-
sensor2 = DaikinValueSensor(
69-
device,
70-
coordinator,
71-
embedded_id,
72-
management_point_type,
73-
None,
74-
value,
76+
sensors.append(
77+
DaikinValueSensor(
78+
device,
79+
coordinator,
80+
embedded_id,
81+
management_point_type,
82+
None,
83+
value,
84+
)
7585
)
76-
sensors.append(sensor2)
7786

7887
sd = management_point.get("sensoryData")
7988
if sd is not None:
@@ -82,15 +91,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
8291
if sensory_data is not None:
8392
for sensor in sensory_data:
8493
_LOGGER.info("Device '%s' provides sensor '%s'", device.name, sensor)
85-
sensor2 = DaikinValueSensor(
86-
device,
87-
coordinator,
88-
embedded_id,
89-
management_point_type,
90-
"sensoryData",
91-
sensor,
94+
sensors.append(
95+
DaikinValueSensor(
96+
device,
97+
coordinator,
98+
embedded_id,
99+
management_point_type,
100+
"sensoryData",
101+
sensor,
102+
)
92103
)
93-
sensors.append(sensor2)
94104

95105
cd = management_point.get("consumptionData")
96106
if cd is not None:
@@ -126,16 +136,17 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
126136
periodName = SENSOR_PERIODS[period]
127137
sensor = f"{device.name} {management_point_type} {mode} {periodName}"
128138
_LOGGER.info("Proposing sensor '%s'", sensor)
129-
sensorv = DaikinEnergySensor(
130-
device,
131-
coordinator,
132-
embedded_id,
133-
management_point_type,
134-
mode,
135-
period,
136-
icon,
139+
sensors.append(
140+
DaikinEnergySensor(
141+
device,
142+
coordinator,
143+
embedded_id,
144+
management_point_type,
145+
mode,
146+
period,
147+
icon,
148+
)
137149
)
138-
sensors.append(sensorv)
139150
else:
140151
_LOGGER.info(
141152
"Ignoring consumption data '%s', not a supported operation_mode",
@@ -303,3 +314,49 @@ def available(self):
303314
def device_info(self):
304315
"""Return a device description for device registry."""
305316
return self._device.device_info()
317+
318+
319+
class DaikinLimitSensor(CoordinatorEntity, SensorEntity):
320+
321+
def __init__(
322+
self,
323+
hass: HomeAssistant,
324+
device: Appliance,
325+
coordinator,
326+
limit_key,
327+
) -> None:
328+
_LOGGER.info("Device '%s' LimitSensor '%s'", device.name, limit_key)
329+
super().__init__(coordinator)
330+
self._hass = hass
331+
self._device = device
332+
self._limit_key = limit_key
333+
self._attr_has_entity_name = True
334+
self._attr_icon = "mdi:information-outline"
335+
self._attr_entity_category = EntityCategory.DIAGNOSTIC
336+
self._attr_name = f"RateLimit {self._limit_key}"
337+
self._attr_unique_id = f"{self._device.getId()}_limitsensor_{self._limit_key}"
338+
self._attr_native_value = self.sensor_value()
339+
_LOGGER.info(
340+
"Device '%s:%s' supports sensor '%s'",
341+
device.name,
342+
self._attr_name,
343+
)
344+
345+
@callback
346+
def _handle_coordinator_update(self) -> None:
347+
self._attr_native_value = self.sensor_value()
348+
self.async_write_ha_state()
349+
350+
def sensor_value(self):
351+
daikin_api = self._hass.data[DAIKIN_DOMAIN][DAIKIN_API]
352+
return daikin_api.rate_limits[self._limit_key]
353+
354+
@property
355+
def available(self):
356+
"""Return the availability of the underlying device."""
357+
return self._device.available
358+
359+
@property
360+
def device_info(self):
361+
"""Return a device description for device registry."""
362+
return self._device.device_info()

0 commit comments

Comments
 (0)