Skip to content

Commit

Permalink
Use shorthand attributes in Random (#103206)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek committed Nov 2, 2023
1 parent a0741c7 commit 401bb90
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 45 deletions.
23 changes: 3 additions & 20 deletions homeassistant/components/random/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
32 changes: 7 additions & 25 deletions homeassistant/components/random/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 401bb90

Please sign in to comment.