2019-04-03 15:40:03 +00:00
|
|
|
"""Counter for the days until an HTTPS (TLS) certificate will expire."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
2021-11-18 13:11:44 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-12-09 19:15:46 +00:00
|
|
|
from datetime import datetime
|
2017-04-25 10:40:13 +00:00
|
|
|
|
2024-12-09 19:15:46 +00:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
2025-02-10 20:08:03 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
2017-04-24 20:01:00 +00:00
|
|
|
|
2024-12-09 19:15:46 +00:00
|
|
|
from .const import DOMAIN
|
2025-02-08 12:21:13 +00:00
|
|
|
from .coordinator import CertExpiryConfigEntry, CertExpiryDataUpdateCoordinator
|
2024-09-23 06:47:23 +00:00
|
|
|
from .entity import CertExpiryEntity
|
2017-04-24 20:01:00 +00:00
|
|
|
|
2017-04-25 10:40:13 +00:00
|
|
|
|
2022-01-03 18:16:42 +00:00
|
|
|
async def async_setup_entry(
|
2022-07-31 11:26:16 +00:00
|
|
|
hass: HomeAssistant,
|
2024-05-04 23:33:24 +00:00
|
|
|
entry: CertExpiryConfigEntry,
|
2025-02-10 20:08:03 +00:00
|
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
2022-01-03 18:16:42 +00:00
|
|
|
) -> None:
|
2019-08-28 17:35:09 +00:00
|
|
|
"""Add cert-expiry entry."""
|
2024-05-04 23:33:24 +00:00
|
|
|
coordinator = entry.runtime_data
|
2020-06-18 16:29:46 +00:00
|
|
|
|
2024-05-04 23:33:24 +00:00
|
|
|
sensors = [SSLCertificateTimestamp(coordinator)]
|
2020-06-18 16:29:46 +00:00
|
|
|
|
|
|
|
async_add_entities(sensors, True)
|
|
|
|
|
|
|
|
|
2021-03-22 19:05:13 +00:00
|
|
|
class SSLCertificateTimestamp(CertExpiryEntity, SensorEntity):
|
2020-06-18 16:29:46 +00:00
|
|
|
"""Implementation of the Cert Expiry timestamp sensor."""
|
2017-04-24 20:01:00 +00:00
|
|
|
|
2021-12-09 08:20:56 +00:00
|
|
|
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
2023-08-17 16:29:20 +00:00
|
|
|
_attr_translation_key = "certificate_expiry"
|
2019-07-08 09:33:23 +00:00
|
|
|
|
2022-07-31 11:26:16 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: CertExpiryDataUpdateCoordinator,
|
|
|
|
) -> None:
|
2021-07-23 05:54:06 +00:00
|
|
|
"""Initialize a Cert Expiry timestamp sensor."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
self._attr_unique_id = f"{coordinator.host}:{coordinator.port}-timestamp"
|
2023-08-10 11:05:58 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, f"{coordinator.host}:{coordinator.port}")},
|
|
|
|
name=coordinator.name,
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
)
|
2017-04-24 20:01:00 +00:00
|
|
|
|
2020-06-18 16:29:46 +00:00
|
|
|
@property
|
2021-11-18 13:11:44 +00:00
|
|
|
def native_value(self) -> datetime | None:
|
2020-06-18 16:29:46 +00:00
|
|
|
"""Return the state of the sensor."""
|
|
|
|
if self.coordinator.data:
|
2021-11-18 13:11:44 +00:00
|
|
|
return self.coordinator.data
|
2020-06-18 16:29:46 +00:00
|
|
|
return None
|