core/homeassistant/components/uptime/sensor.py

41 lines
1.3 KiB
Python

"""Platform to retrieve uptime for Home Assistant."""
from __future__ import annotations
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.dt as dt_util
from .const import DOMAIN
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)])
class UptimeSensor(SensorEntity):
"""Representation of an uptime sensor."""
_attr_device_class = SensorDeviceClass.TIMESTAMP
_attr_has_entity_name = True
_attr_name = None
_attr_should_poll = False
def __init__(self, entry: ConfigEntry) -> None:
"""Initialize the uptime sensor."""
self._attr_native_value = dt_util.utcnow()
self._attr_unique_id = entry.entry_id
self._attr_device_info = DeviceInfo(
name=entry.title,
identifiers={(DOMAIN, entry.entry_id)},
entry_type=DeviceEntryType.SERVICE,
)