Skip to content

Commit

Permalink
Add native_unit_of_measurement to temperature
Browse files Browse the repository at this point in the history
Remove seconds and milliseconds from device uptime
  • Loading branch information
Kane610 committed Aug 31, 2023
1 parent b1b141b commit f1ed425
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion homeassistant/components/unifi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
UnitOfTemperature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory, UnitOfInformation, UnitOfPower
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f1ed425

Please sign in to comment.