mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Use shorthand attributes in Random (#103206)
This commit is contained in:
parent
a0741c74b2
commit
401bb90215
@ -54,31 +54,14 @@ async def async_setup_entry(
|
|||||||
class RandomBinarySensor(BinarySensorEntity):
|
class RandomBinarySensor(BinarySensorEntity):
|
||||||
"""Representation of a Random binary sensor."""
|
"""Representation of a Random binary sensor."""
|
||||||
|
|
||||||
_state: bool | None = None
|
|
||||||
|
|
||||||
def __init__(self, config: Mapping[str, Any], entry_id: str | None = None) -> None:
|
def __init__(self, config: Mapping[str, Any], entry_id: str | None = None) -> None:
|
||||||
"""Initialize the Random binary sensor."""
|
"""Initialize the Random binary sensor."""
|
||||||
self._name = config.get(CONF_NAME)
|
self._attr_name = config.get(CONF_NAME)
|
||||||
self._device_class = config.get(CONF_DEVICE_CLASS)
|
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
|
||||||
if entry_id:
|
if entry_id:
|
||||||
self._attr_unique_id = 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:
|
async def async_update(self) -> None:
|
||||||
"""Get new state and update the sensor's state."""
|
"""Get new state and update the sensor's state."""
|
||||||
|
|
||||||
self._state = bool(getrandbits(1))
|
self._attr_is_on = bool(getrandbits(1))
|
||||||
|
@ -65,39 +65,21 @@ async def async_setup_entry(
|
|||||||
class RandomSensor(SensorEntity):
|
class RandomSensor(SensorEntity):
|
||||||
"""Representation of a Random number sensor."""
|
"""Representation of a Random number sensor."""
|
||||||
|
|
||||||
_state: int | None = None
|
|
||||||
|
|
||||||
def __init__(self, config: Mapping[str, Any], entry_id: str | None = None) -> None:
|
def __init__(self, config: Mapping[str, Any], entry_id: str | None = None) -> None:
|
||||||
"""Initialize the Random sensor."""
|
"""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._minimum = config.get(CONF_MINIMUM, DEFAULT_MIN)
|
||||||
self._maximum = config.get(CONF_MAXIMUM, DEFAULT_MAX)
|
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_device_class = config.get(CONF_DEVICE_CLASS)
|
||||||
|
self._attr_extra_state_attributes = {
|
||||||
|
ATTR_MAXIMUM: self._maximum,
|
||||||
|
ATTR_MINIMUM: self._minimum,
|
||||||
|
}
|
||||||
if entry_id:
|
if entry_id:
|
||||||
self._attr_unique_id = 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:
|
async def async_update(self) -> None:
|
||||||
"""Get a new number and updates the states."""
|
"""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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user