2019-04-03 15:40:03 +00:00
|
|
|
"""Platform to retrieve uptime for Home Assistant."""
|
2021-06-09 12:30:33 +00:00
|
|
|
from __future__ import annotations
|
2017-10-14 18:06:44 +00:00
|
|
|
|
2023-01-20 16:50:32 +00:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-06-09 12:30:33 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-07-16 13:27:20 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-06-09 12:30:33 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-06-02 12:31:06 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2017-10-14 18:06:44 +00:00
|
|
|
|
2023-01-20 16:50:32 +00:00
|
|
|
from .const import DOMAIN
|
2018-06-02 12:31:06 +00:00
|
|
|
|
2022-03-12 11:36:08 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the platform from config_entry."""
|
|
|
|
async_add_entities([UptimeSensor(entry)])
|
2017-10-14 18:06:44 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
class UptimeSensor(SensorEntity):
|
2017-10-14 18:06:44 +00:00
|
|
|
"""Representation of an uptime sensor."""
|
|
|
|
|
2021-12-19 12:06:36 +00:00
|
|
|
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
2022-07-16 13:27:20 +00:00
|
|
|
_attr_has_entity_name = True
|
2021-12-19 12:06:36 +00:00
|
|
|
_attr_should_poll = False
|
|
|
|
|
2022-03-12 11:36:08 +00:00
|
|
|
def __init__(self, entry: ConfigEntry) -> None:
|
2017-10-14 18:06:44 +00:00
|
|
|
"""Initialize the uptime sensor."""
|
2021-11-24 14:00:54 +00:00
|
|
|
self._attr_native_value = dt_util.utcnow()
|
2022-03-12 11:36:08 +00:00
|
|
|
self._attr_unique_id = entry.entry_id
|
2022-07-16 13:27:20 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
name=entry.title,
|
|
|
|
identifiers={(DOMAIN, entry.entry_id)},
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
)
|