Change precision of Nest sensors (#56993)

* Change precision of Nest sensors

* Add comment to temp rounding

Co-authored-by: Allen Porter <allen.porter@gmail.com>

* Update rounding and tests

* Add test for rounding

Co-authored-by: Allen Porter <allen.porter@gmail.com>
pull/58266/head
Michael Davie 2021-10-22 23:31:25 -04:00 committed by GitHub
parent 93b061e9d9
commit e481c862a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 4 deletions

View File

@ -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)

View File

@ -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"