Use shorthand attributes for EnOcean (#99278)
This commit is contained in:
parent
38267699e5
commit
7d70b42e4a
5 changed files with 24 additions and 61 deletions
|
@ -60,21 +60,12 @@ class EnOceanBinarySensor(EnOceanEntity, BinarySensorEntity):
|
||||||
device_class: BinarySensorDeviceClass | None,
|
device_class: BinarySensorDeviceClass | None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the EnOcean binary sensor."""
|
"""Initialize the EnOcean binary sensor."""
|
||||||
super().__init__(dev_id, dev_name)
|
super().__init__(dev_id)
|
||||||
self._device_class = device_class
|
self._attr_device_class = device_class
|
||||||
self.which = -1
|
self.which = -1
|
||||||
self.onoff = -1
|
self.onoff = -1
|
||||||
self._attr_unique_id = f"{combine_hex(dev_id)}-{device_class}"
|
self._attr_unique_id = f"{combine_hex(dev_id)}-{device_class}"
|
||||||
|
self._attr_name = dev_name
|
||||||
@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
|
|
||||||
|
|
||||||
def value_changed(self, packet):
|
def value_changed(self, packet):
|
||||||
"""Fire an event with the data that have changed.
|
"""Fire an event with the data that have changed.
|
||||||
|
|
|
@ -11,10 +11,9 @@ from .const import SIGNAL_RECEIVE_MESSAGE, SIGNAL_SEND_MESSAGE
|
||||||
class EnOceanEntity(Entity):
|
class EnOceanEntity(Entity):
|
||||||
"""Parent class for all entities associated with the EnOcean component."""
|
"""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."""
|
"""Initialize the device."""
|
||||||
self.dev_id = dev_id
|
self.dev_id = dev_id
|
||||||
self.dev_name = dev_name
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
|
|
|
@ -53,47 +53,29 @@ class EnOceanLight(EnOceanEntity, LightEntity):
|
||||||
|
|
||||||
_attr_color_mode = ColorMode.BRIGHTNESS
|
_attr_color_mode = ColorMode.BRIGHTNESS
|
||||||
_attr_supported_color_modes = {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:
|
def __init__(self, sender_id: list[int], dev_id: list[int], dev_name: str) -> None:
|
||||||
"""Initialize the EnOcean light source."""
|
"""Initialize the EnOcean light source."""
|
||||||
super().__init__(dev_id, dev_name)
|
super().__init__(dev_id)
|
||||||
self._on_state = False
|
|
||||||
self._brightness = 50
|
|
||||||
self._sender_id = sender_id
|
self._sender_id = sender_id
|
||||||
self._attr_unique_id = f"{combine_hex(dev_id)}"
|
self._attr_unique_id = str(combine_hex(dev_id))
|
||||||
|
self._attr_name = dev_name
|
||||||
@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
|
|
||||||
|
|
||||||
def turn_on(self, **kwargs: Any) -> None:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the light source on or sets a specific dimmer value."""
|
"""Turn the light source on or sets a specific dimmer value."""
|
||||||
if (brightness := kwargs.get(ATTR_BRIGHTNESS)) is not None:
|
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:
|
if bval == 0:
|
||||||
bval = 1
|
bval = 1
|
||||||
command = [0xA5, 0x02, bval, 0x01, 0x09]
|
command = [0xA5, 0x02, bval, 0x01, 0x09]
|
||||||
command.extend(self._sender_id)
|
command.extend(self._sender_id)
|
||||||
command.extend([0x00])
|
command.extend([0x00])
|
||||||
self.send_command(command, [], 0x01)
|
self.send_command(command, [], 0x01)
|
||||||
self._on_state = True
|
self._attr_is_on = True
|
||||||
|
|
||||||
def turn_off(self, **kwargs: Any) -> None:
|
def turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the light source off."""
|
"""Turn the light source off."""
|
||||||
|
@ -101,7 +83,7 @@ class EnOceanLight(EnOceanEntity, LightEntity):
|
||||||
command.extend(self._sender_id)
|
command.extend(self._sender_id)
|
||||||
command.extend([0x00])
|
command.extend([0x00])
|
||||||
self.send_command(command, [], 0x01)
|
self.send_command(command, [], 0x01)
|
||||||
self._on_state = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
def value_changed(self, packet):
|
def value_changed(self, packet):
|
||||||
"""Update the internal state of this device.
|
"""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:
|
if packet.data[0] == 0xA5 and packet.data[1] == 0x02:
|
||||||
val = packet.data[2]
|
val = packet.data[2]
|
||||||
self._brightness = math.floor(val / 100.0 * 256.0)
|
self._attr_brightness = math.floor(val / 100.0 * 256.0)
|
||||||
self._on_state = bool(val != 0)
|
self._attr_is_on = bool(val != 0)
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
|
@ -160,7 +160,7 @@ class EnOceanSensor(EnOceanEntity, RestoreSensor):
|
||||||
description: EnOceanSensorEntityDescription,
|
description: EnOceanSensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the EnOcean sensor device."""
|
"""Initialize the EnOcean sensor device."""
|
||||||
super().__init__(dev_id, dev_name)
|
super().__init__(dev_id)
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._attr_name = f"{description.name} {dev_name}"
|
self._attr_name = f"{description.name} {dev_name}"
|
||||||
self._attr_unique_id = description.unique_id(dev_id)
|
self._attr_unique_id = description.unique_id(dev_id)
|
||||||
|
|
|
@ -76,24 +76,15 @@ async def async_setup_platform(
|
||||||
class EnOceanSwitch(EnOceanEntity, SwitchEntity):
|
class EnOceanSwitch(EnOceanEntity, SwitchEntity):
|
||||||
"""Representation of an EnOcean switch device."""
|
"""Representation of an EnOcean switch device."""
|
||||||
|
|
||||||
|
_attr_is_on = False
|
||||||
|
|
||||||
def __init__(self, dev_id: list[int], dev_name: str, channel: int) -> None:
|
def __init__(self, dev_id: list[int], dev_name: str, channel: int) -> None:
|
||||||
"""Initialize the EnOcean switch device."""
|
"""Initialize the EnOcean switch device."""
|
||||||
super().__init__(dev_id, dev_name)
|
super().__init__(dev_id)
|
||||||
self._light = None
|
self._light = None
|
||||||
self._on_state = False
|
|
||||||
self._on_state2 = False
|
|
||||||
self.channel = channel
|
self.channel = channel
|
||||||
self._attr_unique_id = generate_unique_id(dev_id, channel)
|
self._attr_unique_id = generate_unique_id(dev_id, channel)
|
||||||
|
self._attr_name = dev_name
|
||||||
@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
|
|
||||||
|
|
||||||
def turn_on(self, **kwargs: Any) -> None:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn on the switch."""
|
"""Turn on the switch."""
|
||||||
|
@ -105,7 +96,7 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity):
|
||||||
optional=optional,
|
optional=optional,
|
||||||
packet_type=0x01,
|
packet_type=0x01,
|
||||||
)
|
)
|
||||||
self._on_state = True
|
self._attr_is_on = True
|
||||||
|
|
||||||
def turn_off(self, **kwargs: Any) -> None:
|
def turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn off the switch."""
|
"""Turn off the switch."""
|
||||||
|
@ -117,7 +108,7 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity):
|
||||||
optional=optional,
|
optional=optional,
|
||||||
packet_type=0x01,
|
packet_type=0x01,
|
||||||
)
|
)
|
||||||
self._on_state = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
def value_changed(self, packet):
|
def value_changed(self, packet):
|
||||||
"""Update the internal state of the switch."""
|
"""Update the internal state of the switch."""
|
||||||
|
@ -129,7 +120,7 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity):
|
||||||
divisor = packet.parsed["DIV"]["raw_value"]
|
divisor = packet.parsed["DIV"]["raw_value"]
|
||||||
watts = raw_val / (10**divisor)
|
watts = raw_val / (10**divisor)
|
||||||
if watts > 1:
|
if watts > 1:
|
||||||
self._on_state = True
|
self._attr_is_on = True
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
elif packet.data[0] == 0xD2:
|
elif packet.data[0] == 0xD2:
|
||||||
# actuator status telegram
|
# actuator status telegram
|
||||||
|
@ -138,5 +129,5 @@ class EnOceanSwitch(EnOceanEntity, SwitchEntity):
|
||||||
channel = packet.parsed["IO"]["raw_value"]
|
channel = packet.parsed["IO"]["raw_value"]
|
||||||
output = packet.parsed["OV"]["raw_value"]
|
output = packet.parsed["OV"]["raw_value"]
|
||||||
if channel == self.channel:
|
if channel == self.channel:
|
||||||
self._on_state = output > 0
|
self._attr_is_on = output > 0
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue