Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shorthand attributes in Random #103206

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)