Add config flow to random (#100858)
Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
parent
6fae50cb75
commit
0658c7b307
10 changed files with 524 additions and 28 deletions
|
@ -1,12 +1,16 @@
|
|||
"""Support for showing random numbers."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from random import randrange
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_DEVICE_CLASS,
|
||||
CONF_MAXIMUM,
|
||||
CONF_MINIMUM,
|
||||
CONF_NAME,
|
||||
|
@ -17,12 +21,12 @@ import homeassistant.helpers.config_validation as cv
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import DEFAULT_MAX, DEFAULT_MIN
|
||||
|
||||
ATTR_MAXIMUM = "maximum"
|
||||
ATTR_MINIMUM = "minimum"
|
||||
|
||||
DEFAULT_NAME = "Random Sensor"
|
||||
DEFAULT_MIN = 0
|
||||
DEFAULT_MAX = 20
|
||||
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
|
@ -42,26 +46,37 @@ async def async_setup_platform(
|
|||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the Random number sensor."""
|
||||
name = config.get(CONF_NAME)
|
||||
minimum = config.get(CONF_MINIMUM)
|
||||
maximum = config.get(CONF_MAXIMUM)
|
||||
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
|
||||
|
||||
async_add_entities([RandomSensor(name, minimum, maximum, unit)], True)
|
||||
async_add_entities([RandomSensor(config)], True)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Initialize config entry."""
|
||||
|
||||
async_add_entities(
|
||||
[RandomSensor(config_entry.options, config_entry.entry_id)], True
|
||||
)
|
||||
|
||||
|
||||
class RandomSensor(SensorEntity):
|
||||
"""Representation of a Random number sensor."""
|
||||
|
||||
_attr_icon = "mdi:hanger"
|
||||
_state: int | None = None
|
||||
|
||||
def __init__(self, name, minimum, maximum, unit_of_measurement):
|
||||
def __init__(self, config: Mapping[str, Any], entry_id: str | None = None) -> None:
|
||||
"""Initialize the Random sensor."""
|
||||
self._name = name
|
||||
self._minimum = minimum
|
||||
self._maximum = maximum
|
||||
self._unit_of_measurement = unit_of_measurement
|
||||
self._state = None
|
||||
self._name = config.get(CONF_NAME)
|
||||
self._minimum = config.get(CONF_MINIMUM, DEFAULT_MIN)
|
||||
self._maximum = config.get(CONF_MAXIMUM, DEFAULT_MAX)
|
||||
self._unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT)
|
||||
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
||||
if entry_id:
|
||||
self._attr_unique_id = entry_id
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue