Use shorthand attributes in Wiffi (#99919)

This commit is contained in:
Joost Lekkerkerker 2023-09-12 14:58:03 +02:00 committed by GitHub
parent 26ada30720
commit 1ca505c228
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 52 deletions

View File

@ -144,7 +144,8 @@ class WiffiEntity(Entity):
def __init__(self, device, metric, options): def __init__(self, device, metric, options):
"""Initialize the base elements of a wiffi entity.""" """Initialize the base elements of a wiffi entity."""
self._id = generate_unique_id(device, metric) self._id = generate_unique_id(device, metric)
self._device_info = DeviceInfo( self._attr_unique_id = self._id
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, device.mac_address)}, connections={(dr.CONNECTION_NETWORK_MAC, device.mac_address)},
identifiers={(DOMAIN, device.mac_address)}, identifiers={(DOMAIN, device.mac_address)},
manufacturer="stall.biz", manufacturer="stall.biz",
@ -153,7 +154,7 @@ class WiffiEntity(Entity):
sw_version=device.sw_version, sw_version=device.sw_version,
configuration_url=device.configuration_url, configuration_url=device.configuration_url,
) )
self._name = metric.description self._attr_name = metric.description
self._expiration_date = None self._expiration_date = None
self._value = None self._value = None
self._timeout = options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT) self._timeout = options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT)
@ -173,26 +174,6 @@ class WiffiEntity(Entity):
) )
) )
@property
def device_info(self):
"""Return wiffi device info which is shared between all entities of a device."""
return self._device_info
@property
def unique_id(self):
"""Return unique id for entity."""
return self._id
@property
def name(self):
"""Return entity name."""
return self._name
@property
def available(self):
"""Return true if value is valid."""
return self._value is not None
def reset_expiration_date(self): def reset_expiration_date(self):
"""Reset value expiration date. """Reset value expiration date.
@ -221,8 +202,10 @@ class WiffiEntity(Entity):
def _is_measurement_entity(self): def _is_measurement_entity(self):
"""Measurement entities have a value in present time.""" """Measurement entities have a value in present time."""
return not self._name.endswith("_gestern") and not self._is_metered_entity() return (
not self._attr_name.endswith("_gestern") and not self._is_metered_entity()
)
def _is_metered_entity(self): def _is_metered_entity(self):
"""Metered entities have a value that keeps increasing until reset.""" """Metered entities have a value that keeps increasing until reset."""
return self._name.endswith("_pro_h") or self._name.endswith("_heute") return self._attr_name.endswith("_pro_h") or self._attr_name.endswith("_heute")

View File

@ -39,13 +39,13 @@ class BoolEntity(WiffiEntity, BinarySensorEntity):
def __init__(self, device, metric, options): def __init__(self, device, metric, options):
"""Initialize the entity.""" """Initialize the entity."""
super().__init__(device, metric, options) super().__init__(device, metric, options)
self._value = metric.value self._attr_is_on = metric.value
self.reset_expiration_date() self.reset_expiration_date()
@property @property
def is_on(self): def available(self):
"""Return the state of the entity.""" """Return true if value is valid."""
return self._value return self._attr_is_on is not None
@callback @callback
def _update_value_callback(self, device, metric): def _update_value_callback(self, device, metric):
@ -54,5 +54,5 @@ class BoolEntity(WiffiEntity, BinarySensorEntity):
Called if a new message has been received from the wiffi device. Called if a new message has been received from the wiffi device.
""" """
self.reset_expiration_date() self.reset_expiration_date()
self._value = metric.value self._attr_is_on = metric.value
self.async_write_ha_state() self.async_write_ha_state()

View File

@ -69,11 +69,13 @@ class NumberEntity(WiffiEntity, SensorEntity):
def __init__(self, device, metric, options): def __init__(self, device, metric, options):
"""Initialize the entity.""" """Initialize the entity."""
super().__init__(device, metric, options) super().__init__(device, metric, options)
self._device_class = UOM_TO_DEVICE_CLASS_MAP.get(metric.unit_of_measurement) self._attr_device_class = UOM_TO_DEVICE_CLASS_MAP.get(
self._unit_of_measurement = UOM_MAP.get( metric.unit_of_measurement
)
self._attr_native_unit_of_measurement = UOM_MAP.get(
metric.unit_of_measurement, metric.unit_of_measurement metric.unit_of_measurement, metric.unit_of_measurement
) )
self._value = metric.value self._attr_native_value = metric.value
if self._is_measurement_entity(): if self._is_measurement_entity():
self._attr_state_class = SensorStateClass.MEASUREMENT self._attr_state_class = SensorStateClass.MEASUREMENT
@ -83,19 +85,9 @@ class NumberEntity(WiffiEntity, SensorEntity):
self.reset_expiration_date() self.reset_expiration_date()
@property @property
def device_class(self): def available(self):
"""Return the automatically determined device class.""" """Return true if value is valid."""
return self._device_class return self._attr_native_value is not None
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement of this entity."""
return self._unit_of_measurement
@property
def native_value(self):
"""Return the value of the entity."""
return self._value
@callback @callback
def _update_value_callback(self, device, metric): def _update_value_callback(self, device, metric):
@ -104,11 +96,11 @@ class NumberEntity(WiffiEntity, SensorEntity):
Called if a new message has been received from the wiffi device. Called if a new message has been received from the wiffi device.
""" """
self.reset_expiration_date() self.reset_expiration_date()
self._unit_of_measurement = UOM_MAP.get( self._attr_native_unit_of_measurement = UOM_MAP.get(
metric.unit_of_measurement, metric.unit_of_measurement metric.unit_of_measurement, metric.unit_of_measurement
) )
self._value = metric.value self._attr_native_value = metric.value
self.async_write_ha_state() self.async_write_ha_state()
@ -119,13 +111,13 @@ class StringEntity(WiffiEntity, SensorEntity):
def __init__(self, device, metric, options): def __init__(self, device, metric, options):
"""Initialize the entity.""" """Initialize the entity."""
super().__init__(device, metric, options) super().__init__(device, metric, options)
self._value = metric.value self._attr_native_value = metric.value
self.reset_expiration_date() self.reset_expiration_date()
@property @property
def native_value(self): def available(self):
"""Return the value of the entity.""" """Return true if value is valid."""
return self._value return self._attr_native_value is not None
@callback @callback
def _update_value_callback(self, device, metric): def _update_value_callback(self, device, metric):
@ -134,5 +126,5 @@ class StringEntity(WiffiEntity, SensorEntity):
Called if a new message has been received from the wiffi device. Called if a new message has been received from the wiffi device.
""" """
self.reset_expiration_date() self.reset_expiration_date()
self._value = metric.value self._attr_native_value = metric.value
self.async_write_ha_state() self.async_write_ha_state()