Add config flow to Uptime (#67408)
This commit is contained in:
parent
23b8229143
commit
0467dc55fe
13 changed files with 308 additions and 20 deletions
|
@ -8,6 +8,7 @@ from homeassistant.components.sensor import (
|
|||
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
|
||||
|
@ -15,17 +16,15 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
DEFAULT_NAME = "Uptime"
|
||||
from .const import DEFAULT_NAME, DOMAIN
|
||||
|
||||
PLATFORM_SCHEMA = vol.All(
|
||||
cv.deprecated(CONF_UNIT_OF_MEASUREMENT),
|
||||
cv.removed(CONF_UNIT_OF_MEASUREMENT, raise_if_present=False),
|
||||
PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default="days"): vol.All(
|
||||
cv.string, vol.In(["minutes", "hours", "days", "seconds"])
|
||||
),
|
||||
}
|
||||
vol.Remove(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
|
@ -37,9 +36,22 @@ async def async_setup_platform(
|
|||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the uptime sensor platform."""
|
||||
name = config[CONF_NAME]
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=config,
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities([UptimeSensor(name)], True)
|
||||
|
||||
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):
|
||||
|
@ -48,7 +60,8 @@ class UptimeSensor(SensorEntity):
|
|||
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
||||
_attr_should_poll = False
|
||||
|
||||
def __init__(self, name: str) -> None:
|
||||
def __init__(self, entry: ConfigEntry) -> None:
|
||||
"""Initialize the uptime sensor."""
|
||||
self._attr_name = name
|
||||
self._attr_name = entry.title
|
||||
self._attr_native_value = dt_util.utcnow()
|
||||
self._attr_unique_id = entry.entry_id
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue