mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Fully migrate to attribute shorthand in velbus (#59797)
* Move velbus completly to _attr instead of propertys * Commit all sugestions * One more sugestion * Fixed light.py
This commit is contained in:
parent
881d35ab17
commit
c27948a82a
@ -169,26 +169,23 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
class VelbusEntity(Entity):
|
class VelbusEntity(Entity):
|
||||||
"""Representation of a Velbus entity."""
|
"""Representation of a Velbus entity."""
|
||||||
|
|
||||||
|
_attr_should_poll: bool = False
|
||||||
|
|
||||||
def __init__(self, channel: VelbusChannel) -> None:
|
def __init__(self, channel: VelbusChannel) -> None:
|
||||||
"""Initialize a Velbus entity."""
|
"""Initialize a Velbus entity."""
|
||||||
self._channel = channel
|
self._channel = channel
|
||||||
|
self._attr_name = channel.get_name()
|
||||||
@property
|
self._attr_device_info = DeviceInfo(
|
||||||
def unique_id(self) -> str:
|
identifiers={
|
||||||
"""Get unique ID."""
|
(DOMAIN, str(channel.get_module_address())),
|
||||||
if (serial := self._channel.get_module_serial()) == "":
|
},
|
||||||
serial = str(self._channel.get_module_address())
|
manufacturer="Velleman",
|
||||||
return f"{serial}-{self._channel.get_channel_number()}"
|
model=channel.get_module_type_name(),
|
||||||
|
name=channel.get_full_name(),
|
||||||
@property
|
sw_version=channel.get_module_sw_version(),
|
||||||
def name(self) -> str:
|
)
|
||||||
"""Return the display name of this entity."""
|
serial = channel.get_module_serial() or str(channel.get_module_address())
|
||||||
return self._channel.get_name()
|
self._attr_unique_id = f"{serial}-{channel.get_channel_number()}"
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self) -> bool:
|
|
||||||
"""Disable polling."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Add listener for state changes."""
|
"""Add listener for state changes."""
|
||||||
@ -196,16 +193,3 @@ class VelbusEntity(Entity):
|
|||||||
|
|
||||||
async def _on_update(self) -> None:
|
async def _on_update(self) -> None:
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo:
|
|
||||||
"""Return the device info."""
|
|
||||||
return DeviceInfo(
|
|
||||||
identifiers={
|
|
||||||
(DOMAIN, str(self._channel.get_module_address())),
|
|
||||||
},
|
|
||||||
manufacturer="Velleman",
|
|
||||||
model=self._channel.get_module_type_name(),
|
|
||||||
name=self._channel.get_full_name(),
|
|
||||||
sw_version=self._channel.get_module_sw_version(),
|
|
||||||
)
|
|
||||||
|
@ -51,11 +51,6 @@ class VelbusLight(VelbusEntity, LightEntity):
|
|||||||
_channel: VelbusDimmer
|
_channel: VelbusDimmer
|
||||||
_attr_supported_feature = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
|
_attr_supported_feature = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
|
||||||
|
|
||||||
def __init__(self, channel: VelbusDimmer) -> None:
|
|
||||||
"""Initialize the dimmer."""
|
|
||||||
super().__init__(channel)
|
|
||||||
self._attr_name = self._channel.get_name()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return true if the light is on."""
|
"""Return true if the light is on."""
|
||||||
|
@ -50,33 +50,32 @@ class VelbusSensor(VelbusEntity, SensorEntity):
|
|||||||
"""Initialize a sensor Velbus entity."""
|
"""Initialize a sensor Velbus entity."""
|
||||||
super().__init__(channel)
|
super().__init__(channel)
|
||||||
self._is_counter: bool = counter
|
self._is_counter: bool = counter
|
||||||
|
# define the unique id
|
||||||
@property
|
|
||||||
def unique_id(self) -> str:
|
|
||||||
"""Return unique ID for counter sensors."""
|
|
||||||
unique_id = super().unique_id
|
|
||||||
if self._is_counter:
|
if self._is_counter:
|
||||||
unique_id = f"{unique_id}-counter"
|
self._attr_unique_id = f"{self._attr_unique_id}-counter"
|
||||||
return unique_id
|
# define the name
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str:
|
|
||||||
"""Return the name for the sensor."""
|
|
||||||
name = super().name
|
|
||||||
if self._is_counter:
|
if self._is_counter:
|
||||||
name = f"{name}-counter"
|
self._attr_name = f"{self._attr_name}-counter"
|
||||||
return name
|
# define the device class
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self) -> str | None:
|
|
||||||
"""Return the device class of the sensor."""
|
|
||||||
if self._is_counter:
|
if self._is_counter:
|
||||||
return DEVICE_CLASS_ENERGY
|
self._attr_device_class = DEVICE_CLASS_ENERGY
|
||||||
if self._channel.is_counter_channel():
|
elif channel.is_counter_channel():
|
||||||
return DEVICE_CLASS_POWER
|
self._attr_device_class = DEVICE_CLASS_POWER
|
||||||
if self._channel.is_temperature():
|
elif channel.is_temperature():
|
||||||
return DEVICE_CLASS_TEMPERATURE
|
self._attr_device_class = DEVICE_CLASS_TEMPERATURE
|
||||||
return None
|
# define the icon
|
||||||
|
if self._is_counter:
|
||||||
|
self._attr_icon = "mdi:counter"
|
||||||
|
# the state class
|
||||||
|
if self._is_counter:
|
||||||
|
self._attr_state_class = STATE_CLASS_TOTAL_INCREASING
|
||||||
|
else:
|
||||||
|
self._attr_state_class = STATE_CLASS_MEASUREMENT
|
||||||
|
# unit
|
||||||
|
if self._is_counter:
|
||||||
|
self._attr_native_unit_of_measurement = channel.get_counter_unit()
|
||||||
|
else:
|
||||||
|
self._attr_native_unit_of_measurement = channel.get_unit()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> float | int | None:
|
def native_value(self) -> float | int | None:
|
||||||
@ -84,24 +83,3 @@ class VelbusSensor(VelbusEntity, SensorEntity):
|
|||||||
if self._is_counter:
|
if self._is_counter:
|
||||||
return float(self._channel.get_counter_state())
|
return float(self._channel.get_counter_state())
|
||||||
return float(self._channel.get_state())
|
return float(self._channel.get_state())
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self) -> str:
|
|
||||||
"""Return the unit this state is expressed in."""
|
|
||||||
if self._is_counter:
|
|
||||||
return str(self._channel.get_counter_unit())
|
|
||||||
return str(self._channel.get_unit())
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self) -> str | None:
|
|
||||||
"""Icon to use in the frontend."""
|
|
||||||
if self._is_counter:
|
|
||||||
return "mdi:counter"
|
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def state_class(self) -> str:
|
|
||||||
"""Return the state class of this device."""
|
|
||||||
if self._is_counter:
|
|
||||||
return STATE_CLASS_TOTAL_INCREASING
|
|
||||||
return STATE_CLASS_MEASUREMENT
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user