From 1ca505c228fcb66c1b0704762cb79453156cdc44 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Tue, 12 Sep 2023 14:58:03 +0200 Subject: [PATCH] Use shorthand attributes in Wiffi (#99919) --- homeassistant/components/wiffi/__init__.py | 31 ++++----------- .../components/wiffi/binary_sensor.py | 10 ++--- homeassistant/components/wiffi/sensor.py | 38 ++++++++----------- 3 files changed, 27 insertions(+), 52 deletions(-) diff --git a/homeassistant/components/wiffi/__init__.py b/homeassistant/components/wiffi/__init__.py index 11ef186ba15..3a35ec1ed29 100644 --- a/homeassistant/components/wiffi/__init__.py +++ b/homeassistant/components/wiffi/__init__.py @@ -144,7 +144,8 @@ class WiffiEntity(Entity): def __init__(self, device, metric, options): """Initialize the base elements of a wiffi entity.""" 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)}, identifiers={(DOMAIN, device.mac_address)}, manufacturer="stall.biz", @@ -153,7 +154,7 @@ class WiffiEntity(Entity): sw_version=device.sw_version, configuration_url=device.configuration_url, ) - self._name = metric.description + self._attr_name = metric.description self._expiration_date = None self._value = None 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): """Reset value expiration date. @@ -221,8 +202,10 @@ class WiffiEntity(Entity): def _is_measurement_entity(self): """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): """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") diff --git a/homeassistant/components/wiffi/binary_sensor.py b/homeassistant/components/wiffi/binary_sensor.py index d0647b25297..cb1e1da41d8 100644 --- a/homeassistant/components/wiffi/binary_sensor.py +++ b/homeassistant/components/wiffi/binary_sensor.py @@ -39,13 +39,13 @@ class BoolEntity(WiffiEntity, BinarySensorEntity): def __init__(self, device, metric, options): """Initialize the entity.""" super().__init__(device, metric, options) - self._value = metric.value + self._attr_is_on = metric.value self.reset_expiration_date() @property - def is_on(self): - """Return the state of the entity.""" - return self._value + def available(self): + """Return true if value is valid.""" + return self._attr_is_on is not None @callback 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. """ self.reset_expiration_date() - self._value = metric.value + self._attr_is_on = metric.value self.async_write_ha_state() diff --git a/homeassistant/components/wiffi/sensor.py b/homeassistant/components/wiffi/sensor.py index 1036ac7986f..e460a346bd7 100644 --- a/homeassistant/components/wiffi/sensor.py +++ b/homeassistant/components/wiffi/sensor.py @@ -69,11 +69,13 @@ class NumberEntity(WiffiEntity, SensorEntity): def __init__(self, device, metric, options): """Initialize the entity.""" super().__init__(device, metric, options) - self._device_class = UOM_TO_DEVICE_CLASS_MAP.get(metric.unit_of_measurement) - self._unit_of_measurement = UOM_MAP.get( + self._attr_device_class = UOM_TO_DEVICE_CLASS_MAP.get( + metric.unit_of_measurement + ) + self._attr_native_unit_of_measurement = UOM_MAP.get( metric.unit_of_measurement, metric.unit_of_measurement ) - self._value = metric.value + self._attr_native_value = metric.value if self._is_measurement_entity(): self._attr_state_class = SensorStateClass.MEASUREMENT @@ -83,19 +85,9 @@ class NumberEntity(WiffiEntity, SensorEntity): self.reset_expiration_date() @property - def device_class(self): - """Return the automatically determined device class.""" - return self._device_class - - @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 + def available(self): + """Return true if value is valid.""" + return self._attr_native_value is not None @callback 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. """ 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 ) - self._value = metric.value + self._attr_native_value = metric.value self.async_write_ha_state() @@ -119,13 +111,13 @@ class StringEntity(WiffiEntity, SensorEntity): def __init__(self, device, metric, options): """Initialize the entity.""" super().__init__(device, metric, options) - self._value = metric.value + self._attr_native_value = metric.value self.reset_expiration_date() @property - def native_value(self): - """Return the value of the entity.""" - return self._value + def available(self): + """Return true if value is valid.""" + return self._attr_native_value is not None @callback 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. """ self.reset_expiration_date() - self._value = metric.value + self._attr_native_value = metric.value self.async_write_ha_state()