From 1626b3b7c9e2853aeb36f9a851508686239fb14b Mon Sep 17 00:00:00 2001 From: hahn-th <15319212+hahn-th@users.noreply.github.com> Date: Wed, 30 Apr 2025 19:51:43 +0200 Subject: [PATCH] Add absolute humidity sensor to homematicip_cloud (#143709) * add absolute humidity sensor * drop unused tests; rename test * Fix docstrings --- .../components/homematicip_cloud/sensor.py | 31 +++++ .../fixtures/homematicip_cloud.json | 124 ++++++++++++++++++ .../homematicip_cloud/test_device.py | 2 +- .../homematicip_cloud/test_sensor.py | 39 ++++++ 4 files changed, 195 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/homematicip_cloud/sensor.py b/homeassistant/components/homematicip_cloud/sensor.py index bddac78df1c..ba739273788 100644 --- a/homeassistant/components/homematicip_cloud/sensor.py +++ b/homeassistant/components/homematicip_cloud/sensor.py @@ -46,6 +46,7 @@ from homeassistant.components.sensor import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( + CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER, LIGHT_LUX, PERCENTAGE, UnitOfEnergy, @@ -127,6 +128,7 @@ async def async_setup_entry( ): entities.append(HomematicipTemperatureSensor(hap, device)) entities.append(HomematicipHumiditySensor(hap, device)) + entities.append(HomematicipAbsoluteHumiditySensor(hap, device)) elif isinstance(device, (RoomControlDeviceAnalog,)): entities.append(HomematicipTemperatureSensor(hap, device)) if isinstance( @@ -348,6 +350,35 @@ class HomematicipTemperatureSensor(HomematicipGenericEntity, SensorEntity): return state_attr +class HomematicipAbsoluteHumiditySensor(HomematicipGenericEntity, SensorEntity): + """Representation of the HomematicIP absolute humidity sensor.""" + + _attr_native_unit_of_measurement = CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER + _attr_state_class = SensorStateClass.MEASUREMENT + + def __init__(self, hap: HomematicipHAP, device) -> None: + """Initialize the thermometer device.""" + super().__init__(hap, device, post="Absolute Humidity") + + @property + def native_value(self) -> int | None: + """Return the state.""" + if self.functional_channel is None: + return None + + value = self.functional_channel.vaporAmount + + # Handle case where value might be None + if ( + self.functional_channel.vaporAmount is None + or self.functional_channel.vaporAmount == "" + ): + return None + + # Convert from g/m³ to mg/m³ + return int(float(value) * 1000) + + class HomematicipIlluminanceSensor(HomematicipGenericEntity, SensorEntity): """Representation of the HomematicIP Illuminance sensor.""" diff --git a/tests/components/homematicip_cloud/fixtures/homematicip_cloud.json b/tests/components/homematicip_cloud/fixtures/homematicip_cloud.json index ff57cd168c9..65f8afe55fa 100644 --- a/tests/components/homematicip_cloud/fixtures/homematicip_cloud.json +++ b/tests/components/homematicip_cloud/fixtures/homematicip_cloud.json @@ -8296,6 +8296,130 @@ "serializedGlobalTradeItemNumber": "3014F7110000000000DSDPCB", "type": "DOOR_BELL_CONTACT_INTERFACE", "updateState": "UP_TO_DATE" + }, + "3014F71100000000000SVCTH": { + "availableFirmwareVersion": "1.0.10", + "connectionType": "HMIP_RF", + "deviceArchetype": "HMIP", + "firmwareVersion": "1.0.10", + "firmwareVersionInteger": 65546, + "functionalChannels": { + "0": { + "busConfigMismatch": null, + "coProFaulty": false, + "coProRestartNeeded": false, + "coProUpdateFailure": false, + "configPending": false, + "controlsMountingOrientation": null, + "daliBusState": null, + "defaultLinkedGroup": [], + "deviceAliveSignalEnabled": null, + "deviceCommunicationError": null, + "deviceDriveError": null, + "deviceDriveModeError": null, + "deviceId": "3014F71100000000000SVCTH", + "deviceOperationMode": null, + "deviceOverheated": false, + "deviceOverloaded": false, + "devicePowerFailureDetected": false, + "deviceUndervoltage": false, + "displayContrast": null, + "displayMode": null, + "displayMountingOrientation": null, + "dutyCycle": false, + "functionalChannelType": "DEVICE_BASE", + "groupIndex": 0, + "groups": ["00000000-0000-0000-0000-000000000033"], + "index": 0, + "invertedDisplayColors": null, + "label": "", + "lockJammed": null, + "lowBat": false, + "mountingOrientation": null, + "multicastRoutingEnabled": false, + "operationDays": null, + "particulateMatterSensorCommunicationError": null, + "particulateMatterSensorError": null, + "powerShortCircuit": null, + "profilePeriodLimitReached": null, + "routerModuleEnabled": false, + "routerModuleSupported": false, + "rssiDeviceValue": -84, + "rssiPeerValue": null, + "sensorCommunicationError": null, + "sensorError": null, + "shortCircuitDataLine": null, + "supportedOptionalFeatures": { + "IFeatureBusConfigMismatch": false, + "IFeatureDeviceCoProError": false, + "IFeatureDeviceCoProRestart": false, + "IFeatureDeviceCoProUpdate": false, + "IFeatureDeviceCommunicationError": false, + "IFeatureDeviceDaliBusError": false, + "IFeatureDeviceDriveError": false, + "IFeatureDeviceDriveModeError": false, + "IFeatureDeviceIdentify": false, + "IFeatureDeviceOverheated": false, + "IFeatureDeviceOverloaded": false, + "IFeatureDeviceParticulateMatterSensorCommunicationError": false, + "IFeatureDeviceParticulateMatterSensorError": false, + "IFeatureDevicePowerFailure": false, + "IFeatureDeviceSensorCommunicationError": false, + "IFeatureDeviceSensorError": false, + "IFeatureDeviceTemperatureHumiditySensorCommunicationError": false, + "IFeatureDeviceTemperatureHumiditySensorError": false, + "IFeatureDeviceTemperatureOutOfRange": true, + "IFeatureDeviceUndervoltage": false, + "IFeatureMulticastRouter": false, + "IFeaturePowerShortCircuit": false, + "IFeatureProfilePeriodLimit": false, + "IFeatureRssiValue": true, + "IFeatureShortCircuitDataLine": false, + "IOptionalFeatureDefaultLinkedGroup": false, + "IOptionalFeatureDeviceAliveSignalEnabled": false, + "IOptionalFeatureDeviceErrorLockJammed": false, + "IOptionalFeatureDeviceOperationMode": false, + "IOptionalFeatureDisplayContrast": false, + "IOptionalFeatureDisplayMode": false, + "IOptionalFeatureDutyCycle": true, + "IOptionalFeatureInvertedDisplayColors": false, + "IOptionalFeatureLowBat": true, + "IOptionalFeatureMountingOrientation": false, + "IOptionalFeatureOperationDays": false + }, + "temperatureHumiditySensorCommunicationError": null, + "temperatureHumiditySensorError": null, + "temperatureOutOfRange": false, + "unreach": false + }, + "1": { + "actualTemperature": 19.7, + "channelRole": "WEATHER_SENSOR", + "deviceId": "3014F71100000000000SVCTH", + "functionalChannelType": "CLIMATE_SENSOR_CHANNEL", + "groupIndex": 1, + "groups": ["00000000-0000-0000-0000-000000000035"], + "humidity": 36, + "index": 1, + "label": "", + "vaporAmount": 6.098938251390021 + } + }, + "homeId": "00000000-0000-0000-0000-000000000001", + "id": "3014F71100000000000SVCTH", + "label": "elvshctv", + "lastStatusUpdate": 1744114372880, + "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", + "manuallyUpdateForced": false, + "manufacturerCode": 9, + "measuredAttributes": {}, + "modelId": 555, + "modelType": "ELV-SH-CTH", + "oem": "eQ-3", + "permanentlyReachable": false, + "serializedGlobalTradeItemNumber": "3014F71100000000000SVCTH", + "type": "TEMPERATURE_HUMIDITY_SENSOR_COMPACT", + "updateState": "UP_TO_DATE" } }, "groups": { diff --git a/tests/components/homematicip_cloud/test_device.py b/tests/components/homematicip_cloud/test_device.py index 3d3dd170ddd..fd72f275489 100644 --- a/tests/components/homematicip_cloud/test_device.py +++ b/tests/components/homematicip_cloud/test_device.py @@ -28,7 +28,7 @@ async def test_hmip_load_all_supported_devices( test_devices=None, test_groups=None ) - assert len(mock_hap.hmip_device_by_entity_id) == 310 + assert len(mock_hap.hmip_device_by_entity_id) == 325 async def test_hmip_remove_device( diff --git a/tests/components/homematicip_cloud/test_sensor.py b/tests/components/homematicip_cloud/test_sensor.py index 2dda3116032..eebee050d51 100644 --- a/tests/components/homematicip_cloud/test_sensor.py +++ b/tests/components/homematicip_cloud/test_sensor.py @@ -720,3 +720,42 @@ async def test_hmip_esi_led_energy_counter_usage_high_tariff( ) assert ha_state.state == "23825.748" + + +async def test_hmip_absolute_humidity_sensor( + hass: HomeAssistant, default_mock_hap_factory: HomeFactory +) -> None: + """Test absolute humidity sensor (vaporAmount).""" + entity_id = "sensor.elvshctv_absolute_humidity" + entity_name = "elvshctv Absolute Humidity" + device_model = "ELV-SH-CTH" + mock_hap = await default_mock_hap_factory.async_get_mock_hap( + test_devices=["elvshctv"] + ) + + ha_state, hmip_device = get_and_check_entity_basics( + hass, mock_hap, entity_id, entity_name, device_model + ) + + assert ha_state.state == "6098" + + +async def test_hmip_absolute_humidity_sensor_invalid_value( + hass: HomeAssistant, default_mock_hap_factory: HomeFactory +) -> None: + """Test absolute humidity sensor with invalid value for vaporAmount.""" + entity_id = "sensor.elvshctv_absolute_humidity" + entity_name = "elvshctv Absolute Humidity" + device_model = "ELV-SH-CTH" + mock_hap = await default_mock_hap_factory.async_get_mock_hap( + test_devices=["elvshctv"] + ) + + ha_state, hmip_device = get_and_check_entity_basics( + hass, mock_hap, entity_id, entity_name, device_model + ) + + await async_manipulate_test_data(hass, hmip_device, "vaporAmount", None, 1) + ha_state = hass.states.get(entity_id) + + assert ha_state.state == STATE_UNKNOWN