From 7d70b42e4a5e257d23a81d903a356198a484ab93 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 30 Aug 2023 11:57:56 +0200 Subject: [PATCH] Use shorthand attributes for EnOcean (#99278) --- .../components/enocean/binary_sensor.py | 15 ++----- homeassistant/components/enocean/device.py | 3 +- homeassistant/components/enocean/light.py | 40 +++++-------------- homeassistant/components/enocean/sensor.py | 2 +- homeassistant/components/enocean/switch.py | 25 ++++-------- 5 files changed, 24 insertions(+), 61 deletions(-) diff --git a/homeassistant/components/enocean/binary_sensor.py b/homeassistant/components/enocean/binary_sensor.py index e7f94647941..25fc8c4f50a 100644 --- a/homeassistant/components/enocean/binary_sensor.py +++ b/homeassistant/components/enocean/binary_sensor.py @@ -60,21 +60,12 @@ class EnOceanBinarySensor(EnOceanEntity, BinarySensorEntity): device_class: BinarySensorDeviceClass | None, ) -> None: """Initialize the EnOcean binary sensor.""" - super().__init__(dev_id, dev_name) - self._device_class = device_class + super().__init__(dev_id) + self._attr_device_class = device_class self.which = -1 self.onoff = -1 self._attr_unique_id = f"{combine_hex(dev_id)}-{device_class}" - - @property - def name(self): - """Return the default name for the binary sensor.""" - return self.dev_name - - @property - def device_class(self): - """Return the class of this sensor.""" - return self._device_class + self._attr_name = dev_name def value_changed(self, packet): """Fire an event with the data that have changed. diff --git a/homeassistant/components/enocean/device.py b/homeassistant/components/enocean/device.py index 1c98b4dd234..220f940f37f 100644 --- a/homeassistant/components/enocean/device.py +++ b/homeassistant/components/enocean/device.py @@ -11,10 +11,9 @@ from .const import SIGNAL_RECEIVE_MESSAGE, SIGNAL_SEND_MESSAGE class EnOceanEntity(Entity): """Parent class for all entities associated with the EnOcean component.""" - def __init__(self, dev_id: list[int], dev_name: str) -> None: + def __init__(self, dev_id: list[int]) -> None: """Initialize the device.""" self.dev_id = dev_id - self.dev_name = dev_name async def async_added_to_hass(self): """Register callbacks.""" diff --git a/homeassistant/components/enocean/light.py b/homeassistant/components/enocean/light.py index e2a194af8ba..2500ad7ce94 100644 --- a/homeassistant/components/enocean/light.py +++ b/homeassistant/components/enocean/light.py @@ -53,47 +53,29 @@ class EnOceanLight(EnOceanEntity, LightEntity): _attr_color_mode = ColorMode.BRIGHTNESS _attr_supported_color_modes = {ColorMode.BRIGHTNESS} + _attr_brightness = 50 + _attr_is_on = False def __init__(self, sender_id: list[int], dev_id: list[int], dev_name: str) -> None: """Initialize the EnOcean light source.""" - super().__init__(dev_id, dev_name) - self._on_state = False - self._brightness = 50 + super().__init__(dev_id) self._sender_id = sender_id - self._attr_unique_id = f"{combine_hex(dev_id)}" - - @property - def name(self): - """Return the name of the device if any.""" - return self.dev_name - - @property - def brightness(self): - """Brightness of the light. - - This method is optional. Removing it indicates to Home Assistant - that brightness is not supported for this light. - """ - return self._brightness - - @property - def is_on(self): - """If light is on.""" - return self._on_state + self._attr_unique_id = str(combine_hex(dev_id)) + self._attr_name = dev_name def turn_on(self, **kwargs: Any) -> None: """Turn the light source on or sets a specific dimmer value.""" if (brightness := kwargs.get(ATTR_BRIGHTNESS)) is not None: - self._brightness = brightness + self._attr_brightness = brightness - bval = math.floor(self._brightness / 256.0 * 100.0) + bval = math.floor(self._attr_brightness / 256.0 * 100.0) if bval == 0: bval = 1 command = [0xA5, 0x02, bval, 0x01, 0x09] command.extend(self._sender_id) command.extend([0x00]) self.send_command(command, [], 0x01) - self._on_state = True + self._attr_is_on = True def turn_off(self, **kwargs: Any) -> None: """Turn the light source off.""" @@ -101,7 +83,7 @@ class EnOceanLight(EnOceanEntity, LightEntity): command.extend(self._sender_id) command.extend([0x00]) self.send_command(command, [], 0x01) - self._on_state = False + self._attr_is_on = False def value_changed(self, packet): """Update the internal state of this device. @@ -111,6 +93,6 @@ class EnOceanLight(EnOceanEntity, LightEntity): """ if packet.data[0] == 0xA5 and packet.data[1] == 0x02: val = packet.data[2] - self._brightness = math.floor(val / 100.0 * 256.0) - self._on_state = bool(val != 0) + self._attr_brightness = math.floor(val / 100.0 * 256.0) + self._attr_is_on = bool(val != 0) self.schedule_update_ha_state() diff --git a/homeassistant/components/enocean/sensor.py b/homeassistant/components/enocean/sensor.py index db386a2d9fc..f63fd7239d0 100644 --- a/homeassistant/components/enocean/sensor.py +++ b/homeassistant/components/enocean/sensor.py @@ -160,7 +160,7 @@ class EnOceanSensor(EnOceanEntity, RestoreSensor): description: EnOceanSensorEntityDescription, ) -> None: """Initialize the EnOcean sensor device.""" - super().__init__(dev_id, dev_name) + super().__init__(dev_id) self.entity_description = description self._attr_name = f"{description.name} {dev_name}" self._attr_unique_id = description.unique_id(dev_id) diff --git a/homeassistant/components/enocean/switch.py b/homeassistant/components/enocean/switch.py index c69821c8372..13920f08e85 100644 --- a/homeassistant/components/enocean/switch.py +++ b/homeassistant/components/enocean/switch.py @@ -76,24 +76,15 @@ async def async_setup_platform( class EnOceanSwitch(EnOceanEntity, SwitchEntity): """Representation of an EnOcean switch device.""" + _attr_is_on = False + def __init__(self, dev_id: list[int], dev_name: str, channel: int) -> None: """Initialize the EnOcean switch device.""" - super().__init__(dev_id, dev_name) + super().__init__(dev_id) self._light = None - self._on_state = False - self._on_state2 = False self.channel = channel self._attr_unique_id = generate_unique_id(dev_id, channel) - - @property - def is_on(self): - """Return whether the switch is on or off.""" - return self._on_state - - @property - def name(self): - """Return the device name.""" - return self.dev_name + self._attr_name = dev_name def turn_on(self, **kwargs: Any) -> None: """Turn on the switch.""" @@ -105,7 +96,7 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity): optional=optional, packet_type=0x01, ) - self._on_state = True + self._attr_is_on = True def turn_off(self, **kwargs: Any) -> None: """Turn off the switch.""" @@ -117,7 +108,7 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity): optional=optional, packet_type=0x01, ) - self._on_state = False + self._attr_is_on = False def value_changed(self, packet): """Update the internal state of the switch.""" @@ -129,7 +120,7 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity): divisor = packet.parsed["DIV"]["raw_value"] watts = raw_val / (10**divisor) if watts > 1: - self._on_state = True + self._attr_is_on = True self.schedule_update_ha_state() elif packet.data[0] == 0xD2: # actuator status telegram @@ -138,5 +129,5 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity): channel = packet.parsed["IO"]["raw_value"] output = packet.parsed["OV"]["raw_value"] if channel == self.channel: - self._on_state = output > 0 + self._attr_is_on = output > 0 self.schedule_update_ha_state()