Skip to content

Commit 0bead67

Browse files
authored
Add device uptime to Airobot integration (home-assistant#158516)
1 parent 2895849 commit 0bead67

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

homeassistant/components/airobot/sensor.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from collections.abc import Callable
66
from dataclasses import dataclass
7+
from datetime import datetime, timedelta
78

89
from pyairobotrest.models import ThermostatStatus
910

@@ -23,6 +24,8 @@
2324
from homeassistant.core import HomeAssistant
2425
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2526
from homeassistant.helpers.typing import StateType
27+
from homeassistant.util.dt import utcnow
28+
from homeassistant.util.variance import ignore_variance
2629

2730
from . import AirobotConfigEntry
2831
from .entity import AirobotEntity
@@ -34,10 +37,15 @@
3437
class AirobotSensorEntityDescription(SensorEntityDescription):
3538
"""Describes Airobot sensor entity."""
3639

37-
value_fn: Callable[[ThermostatStatus], StateType]
40+
value_fn: Callable[[ThermostatStatus], StateType | datetime]
3841
supported_fn: Callable[[ThermostatStatus], bool] = lambda _: True
3942

4043

44+
uptime_to_stable_datetime = ignore_variance(
45+
lambda value: utcnow().replace(microsecond=0) - timedelta(seconds=value),
46+
timedelta(minutes=2),
47+
)
48+
4149
SENSOR_TYPES: tuple[AirobotSensorEntityDescription, ...] = (
4250
AirobotSensorEntityDescription(
4351
key="air_temperature",
@@ -96,6 +104,14 @@ class AirobotSensorEntityDescription(SensorEntityDescription):
96104
entity_category=EntityCategory.DIAGNOSTIC,
97105
value_fn=lambda status: status.errors,
98106
),
107+
AirobotSensorEntityDescription(
108+
key="device_uptime",
109+
translation_key="device_uptime",
110+
device_class=SensorDeviceClass.TIMESTAMP,
111+
entity_category=EntityCategory.DIAGNOSTIC,
112+
value_fn=lambda status: uptime_to_stable_datetime(status.device_uptime),
113+
entity_registry_enabled_default=False,
114+
),
99115
)
100116

101117

@@ -129,6 +145,6 @@ def __init__(
129145
self._attr_unique_id = f"{coordinator.data.status.device_id}_{description.key}"
130146

131147
@property
132-
def native_value(self) -> StateType:
148+
def native_value(self) -> StateType | datetime:
133149
"""Return the state of the sensor."""
134150
return self.entity_description.value_fn(self.coordinator.data.status)

tests/components/airobot/snapshots/test_sensor.ambr

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,55 @@
5555
'state': '22.0',
5656
})
5757
# ---
58+
# name: test_sensors[sensor.test_thermostat_device_uptime-entry]
59+
EntityRegistryEntrySnapshot({
60+
'aliases': set({
61+
}),
62+
'area_id': None,
63+
'capabilities': None,
64+
'config_entry_id': <ANY>,
65+
'config_subentry_id': <ANY>,
66+
'device_class': None,
67+
'device_id': <ANY>,
68+
'disabled_by': None,
69+
'domain': 'sensor',
70+
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
71+
'entity_id': 'sensor.test_thermostat_device_uptime',
72+
'has_entity_name': True,
73+
'hidden_by': None,
74+
'icon': None,
75+
'id': <ANY>,
76+
'labels': set({
77+
}),
78+
'name': None,
79+
'options': dict({
80+
}),
81+
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
82+
'original_icon': None,
83+
'original_name': 'Device uptime',
84+
'platform': 'airobot',
85+
'previous_unique_id': None,
86+
'suggested_object_id': None,
87+
'supported_features': 0,
88+
'translation_key': 'device_uptime',
89+
'unique_id': 'T01A1B2C3_device_uptime',
90+
'unit_of_measurement': None,
91+
})
92+
# ---
93+
# name: test_sensors[sensor.test_thermostat_device_uptime-state]
94+
StateSnapshot({
95+
'attributes': ReadOnlyDict({
96+
'device_class': 'timestamp',
97+
'friendly_name': 'Test Thermostat Device uptime',
98+
}),
99+
'context': <ANY>,
100+
'entity_id': 'sensor.test_thermostat_device_uptime',
101+
'last_changed': <ANY>,
102+
'last_reported': <ANY>,
103+
'last_updated': <ANY>,
104+
'state': '2023-12-31T21:13:20+00:00',
105+
})
106+
# ---
58107
# name: test_sensors[sensor.test_thermostat_error_count-entry]
59108
EntityRegistryEntrySnapshot({
60109
'aliases': set({

tests/components/airobot/test_sensor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def platforms() -> list[Platform]:
1616
return [Platform.SENSOR]
1717

1818

19+
@pytest.mark.freeze_time("2024-01-01 00:00:00+00:00")
1920
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
2021
async def test_sensors(
2122
hass: HomeAssistant,

0 commit comments

Comments
 (0)