Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions homeassistant/components/gatus/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ class GatusSensorEntityDescription(SensorEntityDescription):
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda result: result.status,
),
GatusSensorEntityDescription(
key="domain_expiration",
translation_key="domain_expiration",
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=UnitOfTime.DAYS,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda result: (
round(result.domain_expiration / 86_400_000_000_000, 2)
if result.domain_expiration is not None
else None
),
),
)


Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/gatus/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
},
"entity": {
"sensor": {
"domain_expiration": {
"name": "Domain expiration"
},
Comment thread
TN-1 marked this conversation as resolved.
"response_time": {
"name": "Response time"
},
Expand Down
9 changes: 8 additions & 1 deletion tests/components/gatus/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ def mock_gatus_client() -> Generator[AsyncMock]:
key="backend_service",
name="Backend Service",
group="Core",
results=[Result(success=True, status=200, duration=23123100)],
results=[
Result(
success=True,
status=200,
duration=23123100,
domain_expiration=25920000000000000,
)
],
)
]
)
Expand Down
55 changes: 55 additions & 0 deletions tests/components/gatus/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
# serializer version: 1
# name: test_sensor_setup_and_states[sensor.core_backend_service_domain_expiration-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.core_backend_service_domain_expiration',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Domain expiration',
'options': dict({
'sensor': dict({
'suggested_display_precision': 2,
}),
}),
'original_device_class': <SensorDeviceClass.DURATION: 'duration'>,
'original_icon': None,
'original_name': 'Domain expiration',
'platform': 'gatus',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'domain_expiration',
'unique_id': '1234567890abcdef1234567890abcdef_backend_service_domain_expiration',
'unit_of_measurement': <UnitOfTime.DAYS: 'd'>,
})
# ---
# name: test_sensor_setup_and_states[sensor.core_backend_service_domain_expiration-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.DEVICE_CLASS: 'device_class'>: 'duration',
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Core Backend Service Domain expiration',
<EntityStateAttribute.UNIT_OF_MEASUREMENT: 'unit_of_measurement'>: <UnitOfTime.DAYS: 'd'>,
}),
'context': <ANY>,
'entity_id': 'sensor.core_backend_service_domain_expiration',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '300.0',
})
# ---
# name: test_sensor_setup_and_states[sensor.core_backend_service_response_time-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
Expand Down
30 changes: 30 additions & 0 deletions tests/components/gatus/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def _to_endpoint_statuses(raw_data: list[dict[str, Any]]) -> list[EndpointStatus
success=r["success"],
status=r.get("status"),
duration=r.get("duration"),
domain_expiration=r.get("domainExpiration"),
)
for r in ep.get("results", [])
],
Expand Down Expand Up @@ -165,3 +166,32 @@ async def test_sensor_missing_status_code(
state = hass.states.get("sensor.backend_service_status_code")
assert state is not None
assert state.state == STATE_UNKNOWN


async def test_sensor_missing_domain_expiration(
hass: HomeAssistant,
mock_gatus_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test that a result missing domain expiration evaluates to STATE_UNKNOWN."""
mock_gatus_client.get_endpoints_statuses.return_value = [
EndpointStatus(
key="backend_service",
name="Backend Service",
group=None,
results=[
Result(
success=True,
status=200,
duration=12500000,
domain_expiration=None,
)
],
)
]

await setup_integration(hass, mock_config_entry)

state = hass.states.get("sensor.backend_service_domain_expiration")
assert state is not None
assert state.state == STATE_UNKNOWN
Loading