85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
"""Platform to retrieve uptime for Home Assistant."""
|
|
from __future__ import annotations
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components.sensor import (
|
|
PLATFORM_SCHEMA,
|
|
SensorDeviceClass,
|
|
SensorEntity,
|
|
)
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
|
|
from homeassistant.core import HomeAssistant
|
|
import homeassistant.helpers.config_validation as cv
|
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
from .const import DEFAULT_NAME, DOMAIN
|
|
|
|
PLATFORM_SCHEMA = vol.All(
|
|
cv.removed(CONF_UNIT_OF_MEASUREMENT, raise_if_present=False),
|
|
PLATFORM_SCHEMA.extend(
|
|
{
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
vol.Remove(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
|
},
|
|
),
|
|
)
|
|
|
|
|
|
async def async_setup_platform(
|
|
hass: HomeAssistant,
|
|
config: ConfigType,
|
|
async_add_entities: AddEntitiesCallback,
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
) -> None:
|
|
"""Set up the uptime sensor platform."""
|
|
async_create_issue(
|
|
hass,
|
|
DOMAIN,
|
|
"removed_yaml",
|
|
breaks_in_ha_version="2022.12.0",
|
|
is_fixable=False,
|
|
severity=IssueSeverity.WARNING,
|
|
translation_key="removed_yaml",
|
|
)
|
|
hass.async_create_task(
|
|
hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_IMPORT},
|
|
data=config,
|
|
)
|
|
)
|
|
|
|
|
|
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_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,
|
|
)
|