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>
This commit is contained in:
parent
43a9a4f9ed
commit
a1e4d4ddd7
1 changed files with 18 additions and 28 deletions
|
@ -169,7 +169,6 @@ class EmonCmsSensor(SensorEntity):
|
||||||
self._value_template = value_template
|
self._value_template = value_template
|
||||||
self._attr_native_unit_of_measurement = unit_of_measurement
|
self._attr_native_unit_of_measurement = unit_of_measurement
|
||||||
self._sensorid = sensorid
|
self._sensorid = sensorid
|
||||||
self._elem = elem
|
|
||||||
|
|
||||||
if unit_of_measurement in ("kWh", "Wh"):
|
if unit_of_measurement in ("kWh", "Wh"):
|
||||||
self._attr_device_class = SensorDeviceClass.ENERGY
|
self._attr_device_class = SensorDeviceClass.ENERGY
|
||||||
|
@ -195,7 +194,24 @@ class EmonCmsSensor(SensorEntity):
|
||||||
elif unit_of_measurement == "hPa":
|
elif unit_of_measurement == "hPa":
|
||||||
self._attr_device_class = SensorDeviceClass.PRESSURE
|
self._attr_device_class = SensorDeviceClass.PRESSURE
|
||||||
self._attr_state_class = SensorStateClass.MEASUREMENT
|
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:
|
if self._value_template is not None:
|
||||||
self._attr_native_value = (
|
self._attr_native_value = (
|
||||||
self._value_template.render_with_possible_json_value(
|
self._value_template.render_with_possible_json_value(
|
||||||
|
@ -204,21 +220,6 @@ class EmonCmsSensor(SensorEntity):
|
||||||
)
|
)
|
||||||
elif elem["value"] is not None:
|
elif elem["value"] is not None:
|
||||||
self._attr_native_value = round(float(elem["value"]), DECIMALS)
|
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:
|
def update(self) -> None:
|
||||||
"""Get the latest data and updates the state."""
|
"""Get the latest data and updates the state."""
|
||||||
|
@ -246,18 +247,7 @@ class EmonCmsSensor(SensorEntity):
|
||||||
if elem is None:
|
if elem is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
self._elem = elem
|
self._update_attributes(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
|
|
||||||
|
|
||||||
|
|
||||||
class EmonCmsData:
|
class EmonCmsData:
|
||||||
|
|
Loading…
Add table
Reference in a new issue