From a1e4d4ddd7c5eef1755172f32816ba6a8409969a Mon Sep 17 00:00:00 2001 From: Alexandre CUER Date: Tue, 4 Jun 2024 10:56:43 +0200 Subject: [PATCH] Remove duplicate code in emoncms (#118610) * Remove duplicate & property extra_state_attributes * Add methods _update_attributes and _update_value * correction in _update_value * Update homeassistant/components/emoncms/sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/emoncms/sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/emoncms/sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/emoncms/sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --- homeassistant/components/emoncms/sensor.py | 46 +++++++++------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/emoncms/sensor.py b/homeassistant/components/emoncms/sensor.py index c981fa0cf6c..cf21cb75847 100644 --- a/homeassistant/components/emoncms/sensor.py +++ b/homeassistant/components/emoncms/sensor.py @@ -169,7 +169,6 @@ class EmonCmsSensor(SensorEntity): self._value_template = value_template self._attr_native_unit_of_measurement = unit_of_measurement self._sensorid = sensorid - self._elem = elem if unit_of_measurement in ("kWh", "Wh"): self._attr_device_class = SensorDeviceClass.ENERGY @@ -195,7 +194,24 @@ class EmonCmsSensor(SensorEntity): elif unit_of_measurement == "hPa": self._attr_device_class = SensorDeviceClass.PRESSURE self._attr_state_class = SensorStateClass.MEASUREMENT + self._update_attributes(elem) + def _update_attributes(self, elem: dict[str, Any]) -> None: + """Update entity attributes.""" + self._attr_extra_state_attributes = { + ATTR_FEEDID: elem["id"], + ATTR_TAG: elem["tag"], + ATTR_FEEDNAME: elem["name"], + } + if elem["value"] is not None: + self._attr_extra_state_attributes[ATTR_SIZE] = elem["size"] + self._attr_extra_state_attributes[ATTR_USERID] = elem["userid"] + self._attr_extra_state_attributes[ATTR_LASTUPDATETIME] = elem["time"] + self._attr_extra_state_attributes[ATTR_LASTUPDATETIMESTR] = ( + template.timestamp_local(float(elem["time"])) + ) + + self._attr_native_value = None if self._value_template is not None: self._attr_native_value = ( self._value_template.render_with_possible_json_value( @@ -204,21 +220,6 @@ class EmonCmsSensor(SensorEntity): ) elif elem["value"] is not None: self._attr_native_value = round(float(elem["value"]), DECIMALS) - else: - self._attr_native_value = None - - @property - def extra_state_attributes(self) -> dict[str, Any]: - """Return the sensor extra attributes.""" - return { - ATTR_FEEDID: self._elem["id"], - ATTR_TAG: self._elem["tag"], - ATTR_FEEDNAME: self._elem["name"], - ATTR_SIZE: self._elem["size"], - ATTR_USERID: self._elem["userid"], - ATTR_LASTUPDATETIME: self._elem["time"], - ATTR_LASTUPDATETIMESTR: template.timestamp_local(float(self._elem["time"])), - } def update(self) -> None: """Get the latest data and updates the state.""" @@ -246,18 +247,7 @@ class EmonCmsSensor(SensorEntity): if elem is None: return - self._elem = elem - - if self._value_template is not None: - self._attr_native_value = ( - self._value_template.render_with_possible_json_value( - elem["value"], STATE_UNKNOWN - ) - ) - elif elem["value"] is not None: - self._attr_native_value = round(float(elem["value"]), DECIMALS) - else: - self._attr_native_value = None + self._update_attributes(elem) class EmonCmsData: