From 401bb90215091b933671ab138315bd851d0a1aee Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Thu, 2 Nov 2023 14:40:27 +0100 Subject: [PATCH] Use shorthand attributes in Random (#103206) --- .../components/random/binary_sensor.py | 23 ++----------- homeassistant/components/random/sensor.py | 32 ++++--------------- 2 files changed, 10 insertions(+), 45 deletions(-) diff --git a/homeassistant/components/random/binary_sensor.py b/homeassistant/components/random/binary_sensor.py index 9ada2ecd621..0c5b4a8b0dd 100644 --- a/homeassistant/components/random/binary_sensor.py +++ b/homeassistant/components/random/binary_sensor.py @@ -54,31 +54,14 @@ async def async_setup_entry( class RandomBinarySensor(BinarySensorEntity): """Representation of a Random binary sensor.""" - _state: bool | None = None - def __init__(self, config: Mapping[str, Any], entry_id: str | None = None) -> None: """Initialize the Random binary sensor.""" - self._name = config.get(CONF_NAME) - self._device_class = config.get(CONF_DEVICE_CLASS) + self._attr_name = config.get(CONF_NAME) + self._attr_device_class = config.get(CONF_DEVICE_CLASS) if entry_id: self._attr_unique_id = entry_id - @property - def name(self): - """Return the name of the sensor.""" - return self._name - - @property - def is_on(self): - """Return true if sensor is on.""" - return self._state - - @property - def device_class(self): - """Return the sensor class of the sensor.""" - return self._device_class - async def async_update(self) -> None: """Get new state and update the sensor's state.""" - self._state = bool(getrandbits(1)) + self._attr_is_on = bool(getrandbits(1)) diff --git a/homeassistant/components/random/sensor.py b/homeassistant/components/random/sensor.py index 50f088f7043..f1ca4290d83 100644 --- a/homeassistant/components/random/sensor.py +++ b/homeassistant/components/random/sensor.py @@ -65,39 +65,21 @@ async def async_setup_entry( class RandomSensor(SensorEntity): """Representation of a Random number sensor.""" - _state: int | None = None - def __init__(self, config: Mapping[str, Any], entry_id: str | None = None) -> None: """Initialize the Random sensor.""" - self._name = config.get(CONF_NAME) + self._attr_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_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 - @property - def name(self): - """Return the name of the device.""" - return self._name - - @property - def native_value(self): - """Return the state of the device.""" - return self._state - - @property - def native_unit_of_measurement(self): - """Return the unit this state is expressed in.""" - return self._unit_of_measurement - - @property - def extra_state_attributes(self): - """Return the attributes of the sensor.""" - return {ATTR_MAXIMUM: self._maximum, ATTR_MINIMUM: self._minimum} - async def async_update(self) -> None: """Get a new number and updates the states.""" - self._state = randrange(self._minimum, self._maximum + 1) + self._attr_native_value = randrange(self._minimum, self._maximum + 1)