Skip to content

Commit 42d0415

Browse files
authored
Update Shelly Neo water valve device class and units (home-assistant#152080)
1 parent 1428b41 commit 42d0415

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

homeassistant/components/shelly/const.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from homeassistant.components.number import NumberMode
3232
from homeassistant.components.sensor import SensorDeviceClass
33+
from homeassistant.const import UnitOfVolumeFlowRate
3334

3435
DOMAIN: Final = "shelly"
3536

@@ -287,6 +288,15 @@ class BLEScannerMode(StrEnum):
287288
ROLE_TO_DEVICE_CLASS_MAP = {
288289
"current_humidity": SensorDeviceClass.HUMIDITY,
289290
"current_temperature": SensorDeviceClass.TEMPERATURE,
291+
"flow_rate": SensorDeviceClass.VOLUME_FLOW_RATE,
292+
"water_pressure": SensorDeviceClass.PRESSURE,
293+
"water_temperature": SensorDeviceClass.TEMPERATURE,
294+
}
295+
296+
# Mapping for units that require conversion to a Home Assistant recognized unit
297+
# e.g. "m3/min" to "m³/min"
298+
DEVICE_UNIT_MAP = {
299+
"m3/min": UnitOfVolumeFlowRate.CUBIC_METERS_PER_MINUTE,
290300
}
291301

292302
# We want to check only the first 5 KB of the script if it contains emitEvent()

homeassistant/components/shelly/number.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
get_blu_trv_device_info,
4141
get_device_entry_gen,
4242
get_virtual_component_ids,
43+
get_virtual_component_unit,
4344
)
4445

4546
PARALLEL_UPDATES = 0
@@ -189,10 +190,7 @@ async def async_set_native_value(self, value: float) -> None:
189190
config["meta"]["ui"]["view"], NumberMode.BOX
190191
),
191192
step_fn=lambda config: config["meta"]["ui"].get("step"),
192-
# If the unit is not set, the device sends an empty string
193-
unit=lambda config: config["meta"]["ui"]["unit"]
194-
if config["meta"]["ui"]["unit"]
195-
else None,
193+
unit=get_virtual_component_unit,
196194
method="number_set",
197195
),
198196
"valve_position": RpcNumberDescription(

homeassistant/components/shelly/sensor.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
get_device_uptime,
6262
get_shelly_air_lamp_life,
6363
get_virtual_component_ids,
64+
get_virtual_component_unit,
6465
is_rpc_wifi_stations_disabled,
6566
)
6667

@@ -1376,9 +1377,7 @@ def __init__(
13761377
"number": RpcSensorDescription(
13771378
key="number",
13781379
sub_key="value",
1379-
unit=lambda config: config["meta"]["ui"]["unit"]
1380-
if config["meta"]["ui"]["unit"]
1381-
else None,
1380+
unit=get_virtual_component_unit,
13821381
device_class_fn=lambda config: ROLE_TO_DEVICE_CLASS_MAP.get(config["role"])
13831382
if "role" in config
13841383
else None,

homeassistant/components/shelly/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
COMPONENT_ID_PATTERN,
5858
CONF_COAP_PORT,
5959
CONF_GEN,
60+
DEVICE_UNIT_MAP,
6061
DEVICES_WITHOUT_FIRMWARE_CHANGELOG,
6162
DOMAIN,
6263
FIRMWARE_UNSUPPORTED_ISSUE_ID,
@@ -653,6 +654,15 @@ def get_virtual_component_ids(config: dict[str, Any], platform: str) -> list[str
653654
return ids
654655

655656

657+
def get_virtual_component_unit(config: dict[str, Any]) -> str | None:
658+
"""Return the unit of a virtual component.
659+
660+
If the unit is not set, the device sends an empty string
661+
"""
662+
unit = config["meta"]["ui"]["unit"]
663+
return DEVICE_UNIT_MAP.get(unit, unit) if unit else None
664+
665+
656666
@callback
657667
def async_remove_orphaned_entities(
658668
hass: HomeAssistant,

tests/components/shelly/test_sensor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ async def test_rpc_remove_text_virtual_sensor_when_orphaned(
11381138
("name", "entity_id", "original_unit", "expected_unit"),
11391139
[
11401140
("Virtual number sensor", "sensor.test_name_virtual_number_sensor", "W", "W"),
1141+
("Unit map", "sensor.test_name_unit_map", "m3/min", "m³/min"),
11411142
(None, "sensor.test_name_number_203", "", None),
11421143
],
11431144
)

0 commit comments

Comments
 (0)