From 99726408e54cc91d7e2ee8dec4b930c050bb5d82 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 31 Aug 2023 20:09:57 +0200 Subject: [PATCH] Add native_unit_of_measurement to temperature Remove seconds and milliseconds from device uptime --- homeassistant/components/unifi/sensor.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/unifi/sensor.py b/homeassistant/components/unifi/sensor.py index 0319cd97a4d964..7cb0b2bbfe3d6e 100644 --- a/homeassistant/components/unifi/sensor.py +++ b/homeassistant/components/unifi/sensor.py @@ -27,6 +27,7 @@ SensorDeviceClass, SensorEntity, SensorEntityDescription, + UnitOfTemperature, ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory, UnitOfInformation, UnitOfPower @@ -88,6 +89,16 @@ def async_wlan_client_value_fn(controller: UniFiController, wlan: Wlan) -> int: ) +@callback +def async_device_uptime_value_fn( + controller: UniFiController, device: Device +) -> datetime: + """Calculate the uptime of the device.""" + return (dt_util.now() - timedelta(seconds=device.uptime)).replace( + second=0, microsecond=0 + ) + + @callback def async_device_outlet_power_supported_fn( controller: UniFiController, obj_id: str @@ -288,12 +299,13 @@ class UnifiSensorEntityDescription( should_poll=False, supported_fn=lambda controller, obj_id: True, unique_id_fn=lambda controller, obj_id: f"device_uptime-{obj_id}", - value_fn=lambda ctrlr, device: dt_util.now() - timedelta(seconds=device.uptime), + value_fn=async_device_uptime_value_fn, ), UnifiSensorEntityDescription[Devices, Device]( key="Device temperature", device_class=SensorDeviceClass.TEMPERATURE, entity_category=EntityCategory.DIAGNOSTIC, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, has_entity_name=True, allowed_fn=lambda controller, obj_id: True, api_handler_fn=lambda api: api.devices,