Use PH device class for hortimax pH readouts instead of a custom unit (#179107)

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Willem Vooijs
2026-08-14 14:04:52 +02:00
committed by GitHub
co-authored by Claude Sonnet 5
parent 97e52d0f5f
commit 6529d29799
4 changed files with 102 additions and 5 deletions
+5 -4
View File
@@ -144,7 +144,6 @@ UNIT_DESCRIPTIONS: Final[dict[str, SensorEntityDescription]] = {
(LIGHT_LUX, SensorDeviceClass.ILLUMINANCE, 0),
(UnitOfConductivity.MILLISIEMENS_PER_CM, SensorDeviceClass.CONDUCTIVITY, 2),
(UnitOfConductivity.MICROSIEMENS_PER_CM, SensorDeviceClass.CONDUCTIVITY, 0),
("pH", None, 1),
(UnitOfPressure.BAR, SensorDeviceClass.PRESSURE, 2),
(UnitOfPressure.MBAR, SensorDeviceClass.PRESSURE, 0),
(UnitOfPressure.HPA, SensorDeviceClass.PRESSURE, 0),
@@ -157,7 +156,9 @@ UNIT_DESCRIPTIONS: Final[dict[str, SensorEntityDescription]] = {
(UnitOfMass.KILOGRAMS, SensorDeviceClass.WEIGHT, 1),
(UnitOfMass.GRAMS, SensorDeviceClass.WEIGHT, 0),
)
} | {
# pH is dimensionless (a logarithmic ratio), so SensorDeviceClass.PH takes no unit.
"pH": SensorEntityDescription(
key="pH", device_class=SensorDeviceClass.PH, suggested_display_precision=1
)
}
# pH has no device class on purpose: SensorDeviceClass.PH accepts no unit, and
# keeping the "pH" unit is worth more than the class.
@@ -307,6 +307,25 @@
}
]
},
{
"name": "Ph",
"readoutIdentifier": "Ph-Measured",
"readoutValueType": "Double",
"unitIdentifier": "PH",
"device": "HOR00000000.000",
"source": {
"sourceName": "Valve group 003",
"sourceType": "IrrigationValveGroup",
"userDefinedName": null,
"sourceGroups": ["Irrigation"]
},
"values": [
{
"timestampUTC": "2026-06-12T08:00:00Z",
"value": 6.2
}
]
},
{
"name": "CO2 level",
"readoutIdentifier": "CO2Level-Measured",
@@ -564,6 +564,63 @@
'state': '1.75',
})
# ---
# name: test_all_entities[sensor.valve_group_003_ph-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': dict({
<SensorEntityCapabilityAttribute.STATE_CLASS: 'state_class'>: <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.valve_group_003_ph',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Ph',
'options': dict({
'sensor': dict({
'suggested_display_precision': 1,
}),
}),
'original_device_class': <SensorDeviceClass.PH: 'ph'>,
'original_icon': None,
'original_name': 'Ph',
'platform': 'hortimax',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'HOR00000000.000::IrrigationValveGroup::Valve group 003::Ph-Measured',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[sensor.valve_group_003_ph-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.DEVICE_CLASS: 'device_class'>: 'ph',
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Valve group 003 Ph',
<SensorEntityCapabilityAttribute.STATE_CLASS: 'state_class'>: <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'context': <ANY>,
'entity_id': 'sensor.valve_group_003_ph',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '6.2',
})
# ---
# name: test_all_entities[sensor.valve_group_003_substrate_conductivity-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
+21 -1
View File
@@ -9,7 +9,13 @@ from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_UNIT_OF_MEASUREMENT,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@@ -77,6 +83,20 @@ async def test_unclassified_readouts_are_disabled(
assert conductivity.disabled_by is None
@pytest.mark.usefixtures("mock_hortos_client")
async def test_ph_has_a_device_class_but_no_unit(
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None:
"""Test pH gets its device class rather than a unit, since pH is dimensionless."""
await setup_integration(hass, mock_config_entry)
state = hass.states.get("sensor.valve_group_003_ph")
assert state is not None
assert state.state == "6.2"
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.PH
assert ATTR_UNIT_OF_MEASUREMENT not in state.attributes
@pytest.mark.usefixtures("mock_hortos_client")
async def test_new_readouts_are_added(
hass: HomeAssistant,