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
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-12-16 13:13:43 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
2021-03-22 18:47:44 +00:00
|
|
|
)
|
2022-03-12 11:36:08 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2021-12-16 13:13:43 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
|
2021-06-09 12:30:33 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-06-02 12:31:06 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-06-09 12:30:33 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-06-02 12:31:06 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2017-10-14 18:06:44 +00:00
|
|
|
|
2022-03-12 11:36:08 +00:00
|
|
|
from .const import DEFAULT_NAME, DOMAIN
|
2017-10-14 18:06:44 +00:00
|
|
|
|
2020-12-01 16:28:59 +00:00
|
|
|
PLATFORM_SCHEMA = vol.All(
|
2022-03-12 11:36:08 +00:00
|
|
|
cv.removed(CONF_UNIT_OF_MEASUREMENT, raise_if_present=False),
|
2020-12-01 16:28:59 +00:00
|
|
|
PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2022-03-12 11:36:08 +00:00
|
|
|
vol.Remove(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
|
|
|
},
|
2020-12-01 16:28:59 +00:00
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-10-14 18:06:44 +00:00
|
|
|
|
|
|
|
|
2021-06-09 12:30:33 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-10-14 18:06:44 +00:00
|
|
|
"""Set up the uptime sensor platform."""
|
2022-03-12 11:36:08 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data=config,
|
|
|
|
)
|
|
|
|
)
|
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
|
|
|
|
_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."""
|
2022-03-12 11:36:08 +00:00
|
|
|
self._attr_name = entry.title
|
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
|