Skip to content

Commit 99bb07a

Browse files
authored
Fix device-class resolution on sensor and binary sensor platforms (#480)
* fixed enum get-by-value resolution * added try/catch blocks for class instantiation
1 parent b12aee9 commit 99bb07a

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

custom_components/magic_areas/binary_sensor.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import logging
55

66
from homeassistant.components.binary_sensor import (
7-
DEVICE_CLASSES,
87
DOMAIN as BINARY_SENSOR_DOMAIN,
98
BinarySensorDeviceClass,
109
BinarySensorEntity,
@@ -63,9 +62,7 @@ def __init__(
6362
BinarySensorGroup.__init__(
6463
self,
6564
device_class=(
66-
BinarySensorDeviceClass[device_class.upper()]
67-
if device_class in DEVICE_CLASSES
68-
else None
65+
BinarySensorDeviceClass(device_class) if device_class else None
6966
),
7067
name=EMPTY_STRING,
7168
unique_id=self._attr_unique_id,
@@ -334,6 +331,16 @@ def create_aggregate_sensors(area: MagicArea) -> list[Entity]:
334331
len(entity_list),
335332
area.slug,
336333
)
337-
aggregates.append(AreaAggregateBinarySensor(area, device_class, entity_list))
334+
try:
335+
aggregates.append(
336+
AreaAggregateBinarySensor(area, device_class, entity_list)
337+
)
338+
except Exception as e: # pylint: disable=broad-exception-caught
339+
_LOGGER.error(
340+
"%s: Error creating '%s' aggregate sensor: %s",
341+
area.slug,
342+
device_class,
343+
str(e),
344+
)
338345

339346
return aggregates

custom_components/magic_areas/sensor.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from homeassistant.components.group.sensor import ATTR_MEAN, ATTR_SUM, SensorGroup
77
from homeassistant.components.sensor.const import (
8-
DEVICE_CLASSES,
98
DOMAIN as SENSOR_DOMAIN,
109
SensorDeviceClass,
1110
SensorStateClass,
@@ -133,24 +132,32 @@ def create_aggregate_sensors(area: MagicArea) -> list[Entity]:
133132
continue
134133

135134
_LOGGER.debug(
136-
"Creating aggregate sensor for device_class '%s' with %d entities (%s)",
135+
"%s: Creating aggregate sensor for device_class '%s' with %d entities",
136+
area.slug,
137137
device_class,
138138
len(entities),
139-
area.slug,
140139
)
141140

142-
# Infer most-popular unit of measurement
143-
unit_of_measurements = Counter(unit_of_measurement_map[device_class])
144-
most_common_unit_of_measurement = unit_of_measurements.most_common(1)[0][0]
145-
146-
aggregates.append(
147-
AreaAggregateSensor(
148-
area=area,
149-
device_class=device_class,
150-
entity_ids=entities,
151-
unit_of_measurement=most_common_unit_of_measurement,
141+
try:
142+
# Infer most-popular unit of measurement
143+
unit_of_measurements = Counter(unit_of_measurement_map[device_class])
144+
most_common_unit_of_measurement = unit_of_measurements.most_common(1)[0][0]
145+
146+
aggregates.append(
147+
AreaAggregateSensor(
148+
area=area,
149+
device_class=device_class,
150+
entity_ids=entities,
151+
unit_of_measurement=most_common_unit_of_measurement,
152+
)
153+
)
154+
except Exception as e: # pylint: disable=broad-exception-caught
155+
_LOGGER.error(
156+
"%s: Error creating '%s' aggregate sensor: %s",
157+
area.slug,
158+
device_class,
159+
str(e),
152160
)
153-
)
154161

155162
return aggregates
156163

@@ -181,12 +188,11 @@ def __init__(
181188
final_unit_of_measurement = unit_of_measurement
182189

183190
self._attr_suggested_display_precision = DEFAULT_SENSOR_PRECISION
184-
sensor_device_class_object: SensorDeviceClass | None = (
185-
SensorDeviceClass[device_class.upper()]
186-
if device_class in DEVICE_CLASSES
187-
else None
191+
192+
sensor_device_class: SensorDeviceClass | None = (
193+
SensorDeviceClass(device_class) if device_class else None
188194
)
189-
self.device_class = sensor_device_class_object
195+
self.device_class = sensor_device_class
190196

191197
state_class = SensorStateClass.MEASUREMENT
192198

@@ -198,7 +204,7 @@ def __init__(
198204
SensorGroup.__init__(
199205
self,
200206
hass=area.hass,
201-
device_class=sensor_device_class_object,
207+
device_class=sensor_device_class,
202208
entity_ids=entity_ids,
203209
ignore_non_numeric=True,
204210
sensor_type=ATTR_SUM if device_class in AGGREGATE_MODE_SUM else ATTR_MEAN,

hacs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
"homeassistant": "2025.1.0",
66
"render_readme": true,
77
"zip_release": true
8-
}
8+
}

0 commit comments

Comments
 (0)