Small code quality improvement/cleanup in random (#129542)

This commit is contained in:
G Johansson 2024-11-04 09:52:35 +01:00 committed by GitHub
parent 018acc0a3c
commit 0a1ba8a4a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 14 deletions

View File

@ -59,10 +59,9 @@ 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
self._attr_unique_id = entry_id
async def async_update(self) -> None:
"""Get new state and update the sensor's state."""

View File

@ -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

View File

@ -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
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)