mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Small code quality improvement/cleanup in random (#129542)
This commit is contained in:
parent
018acc0a3c
commit
0a1ba8a4a3
@ -59,9 +59,8 @@ class RandomBinarySensor(BinarySensorEntity):
|
||||
|
||||
def __init__(self, config: Mapping[str, Any], entry_id: str | None = None) -> None:
|
||||
"""Initialize the Random binary sensor."""
|
||||
self._attr_name = config.get(CONF_NAME)
|
||||
self._attr_name = config[CONF_NAME]
|
||||
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
||||
if entry_id:
|
||||
self._attr_unique_id = entry_id
|
||||
|
||||
async def async_update(self) -> None:
|
||||
|
@ -95,7 +95,7 @@ def _generate_schema(domain: str, flow_type: _FlowType) -> vol.Schema:
|
||||
|
||||
|
||||
async def choose_options_step(options: dict[str, Any]) -> str:
|
||||
"""Return next step_id for options flow according to template_type."""
|
||||
"""Return next step_id for options flow according to entity_type."""
|
||||
return cast(str, options["entity_type"])
|
||||
|
||||
|
||||
@ -122,7 +122,7 @@ def _validate_unit(options: dict[str, Any]) -> None:
|
||||
|
||||
|
||||
def validate_user_input(
|
||||
template_type: str,
|
||||
entity_type: str,
|
||||
) -> Callable[
|
||||
[SchemaCommonFlowHandler, dict[str, Any]],
|
||||
Coroutine[Any, Any, dict[str, Any]],
|
||||
@ -136,10 +136,10 @@ def validate_user_input(
|
||||
_: SchemaCommonFlowHandler,
|
||||
user_input: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
"""Add template type to user input."""
|
||||
if template_type == Platform.SENSOR:
|
||||
"""Add entity type to user input."""
|
||||
if entity_type == Platform.SENSOR:
|
||||
_validate_unit(user_input)
|
||||
return {"entity_type": template_type} | user_input
|
||||
return {"entity_type": entity_type} | user_input
|
||||
|
||||
return _validate_user_input
|
||||
|
||||
|
@ -70,22 +70,22 @@ class RandomSensor(SensorEntity):
|
||||
"""Representation of a Random number sensor."""
|
||||
|
||||
_attr_translation_key = "random"
|
||||
_unrecorded_attributes = frozenset({ATTR_MAXIMUM, ATTR_MINIMUM})
|
||||
|
||||
def __init__(self, config: Mapping[str, Any], entry_id: str | None = None) -> None:
|
||||
"""Initialize the Random sensor."""
|
||||
self._attr_name = config.get(CONF_NAME)
|
||||
self._minimum = config.get(CONF_MINIMUM, DEFAULT_MIN)
|
||||
self._maximum = config.get(CONF_MAXIMUM, DEFAULT_MAX)
|
||||
self._attr_name = config[CONF_NAME]
|
||||
self._minimum = config[CONF_MINIMUM]
|
||||
self._maximum = config[CONF_MAXIMUM]
|
||||
self._attr_native_unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT)
|
||||
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
||||
self._attr_extra_state_attributes = {
|
||||
ATTR_MAXIMUM: self._maximum,
|
||||
ATTR_MINIMUM: self._minimum,
|
||||
}
|
||||
if entry_id:
|
||||
self._attr_unique_id = entry_id
|
||||
|
||||
async def async_update(self) -> None:
|
||||
"""Get a new number and updates the states."""
|
||||
"""Get a new number and update the state."""
|
||||
|
||||
self._attr_native_value = randrange(self._minimum, self._maximum + 1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user