From e481c862a6c6cef671ea0a05a5f0b93da8688d7d Mon Sep 17 00:00:00 2001 From: Michael Davie Date: Fri, 22 Oct 2021 23:31:25 -0400 Subject: [PATCH] Change precision of Nest sensors (#56993) * Change precision of Nest sensors * Add comment to temp rounding Co-authored-by: Allen Porter * Update rounding and tests * Add test for rounding Co-authored-by: Allen Porter --- homeassistant/components/nest/sensor_sdm.py | 10 +++++--- tests/components/nest/sensor_sdm_test.py | 27 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/nest/sensor_sdm.py b/homeassistant/components/nest/sensor_sdm.py index 52d81ab7dd9..786efde43f7 100644 --- a/homeassistant/components/nest/sensor_sdm.py +++ b/homeassistant/components/nest/sensor_sdm.py @@ -89,7 +89,10 @@ class TemperatureSensor(SensorBase): def native_value(self) -> float: """Return the state of the sensor.""" trait: TemperatureTrait = self._device.traits[TemperatureTrait.NAME] - return trait.ambient_temperature_celsius + # Round for display purposes because the API returns 5 decimal places. + # This can be removed if the SDM API issue is fixed, or a frontend + # display fix is added for all integrations. + return float(round(trait.ambient_temperature_celsius, 1)) class HumiditySensor(SensorBase): @@ -104,7 +107,8 @@ class HumiditySensor(SensorBase): return f"{self._device_info.device_name} Humidity" @property - def native_value(self) -> float: + def native_value(self) -> int: """Return the state of the sensor.""" trait: HumidityTrait = self._device.traits[HumidityTrait.NAME] - return trait.ambient_humidity_percent + # Cast without loss of precision because the API always returns an integer. + return int(trait.ambient_humidity_percent) diff --git a/tests/components/nest/sensor_sdm_test.py b/tests/components/nest/sensor_sdm_test.py index bfac288742d..72b2ecfc529 100644 --- a/tests/components/nest/sensor_sdm_test.py +++ b/tests/components/nest/sensor_sdm_test.py @@ -64,7 +64,7 @@ async def test_thermostat_device(hass): humidity = hass.states.get("sensor.my_sensor_humidity") assert humidity is not None - assert humidity.state == "35.0" + assert humidity.state == "35" assert humidity.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE assert humidity.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_HUMIDITY assert humidity.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT @@ -230,3 +230,28 @@ async def test_device_with_unknown_type(hass): assert device.name == "My Sensor" assert device.model is None assert device.identifiers == {("nest", "some-device-id")} + + +async def test_temperature_rounding(hass): + """Test the rounding of overly precise temperatures.""" + devices = { + "some-device-id": Device.MakeDevice( + { + "name": "some-device-id", + "type": THERMOSTAT_TYPE, + "traits": { + "sdm.devices.traits.Info": { + "customName": "My Sensor", + }, + "sdm.devices.traits.Temperature": { + "ambientTemperatureCelsius": 25.15678, + }, + }, + }, + auth=None, + ) + } + await async_setup_sensor(hass, devices) + + temperature = hass.states.get("sensor.my_sensor_temperature") + assert temperature.state == "25.2"