Use shorthand attributes in Random (#103206)

This commit is contained in:
Joost Lekkerkerker 2023-11-02 14:40:27 +01:00 committed by GitHub
parent a0741c74b2
commit 401bb90215
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 45 deletions

View file

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

View file

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