Remove last_reset attribute from tasmota energy sensors (#54836)

This commit is contained in:
Erik Montnemery 2021-08-18 18:25:33 +02:00 committed by GitHub
parent 5d19575a84
commit bca9360d52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 20 deletions

View File

@ -2,7 +2,6 @@
from __future__ import annotations
from datetime import datetime
import logging
from typing import Any
from hatasmota import const as hc, sensor as tasmota_sensor, status_sensor
@ -10,7 +9,11 @@ from hatasmota.entity import TasmotaEntity as HATasmotaEntity
from hatasmota.models import DiscoveryHashType
from homeassistant.components import sensor
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
@ -49,14 +52,11 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt as dt_util
from .const import DATA_REMOVE_DISCOVER_COMPONENT
from .discovery import TASMOTA_DISCOVERY_ENTITY_NEW
from .mixins import TasmotaAvailability, TasmotaDiscoveryUpdate
_LOGGER = logging.getLogger(__name__)
DEVICE_CLASS = "device_class"
STATE_CLASS = "state_class"
ICON = "icon"
@ -121,7 +121,7 @@ SENSOR_DEVICE_CLASS_ICON_MAP = {
hc.SENSOR_TODAY: {DEVICE_CLASS: DEVICE_CLASS_ENERGY},
hc.SENSOR_TOTAL: {
DEVICE_CLASS: DEVICE_CLASS_ENERGY,
STATE_CLASS: STATE_CLASS_MEASUREMENT,
STATE_CLASS: STATE_CLASS_TOTAL_INCREASING,
},
hc.SENSOR_TOTAL_START_TIME: {ICON: "mdi:progress-clock"},
hc.SENSOR_TVOC: {ICON: "mdi:air-filter"},
@ -188,7 +188,6 @@ async def async_setup_entry(
class TasmotaSensor(TasmotaAvailability, TasmotaDiscoveryUpdate, SensorEntity):
"""Representation of a Tasmota sensor."""
_attr_last_reset = None
_tasmota_entity: tasmota_sensor.TasmotaSensor
def __init__(self, **kwds: Any) -> None:
@ -212,17 +211,6 @@ class TasmotaSensor(TasmotaAvailability, TasmotaDiscoveryUpdate, SensorEntity):
self._state_timestamp = state
else:
self._state = state
if "last_reset" in kwargs:
try:
last_reset_dt = dt_util.parse_datetime(kwargs["last_reset"])
last_reset = dt_util.as_utc(last_reset_dt) if last_reset_dt else None
if last_reset is None:
raise ValueError
self._attr_last_reset = last_reset
except ValueError:
_LOGGER.warning(
"Invalid last_reset timestamp '%s'", kwargs["last_reset"]
)
self.async_write_ha_state()
@property

View File

@ -255,6 +255,9 @@ async def test_indexed_sensor_state_via_mqtt2(hass, mqtt_mock, setup_tasmota):
state = hass.states.get("sensor.tasmota_energy_total")
assert state.state == "unavailable"
assert not state.attributes.get(ATTR_ASSUMED_STATE)
assert (
state.attributes[sensor.ATTR_STATE_CLASS] == sensor.STATE_CLASS_TOTAL_INCREASING
)
async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/LWT", "Online")
state = hass.states.get("sensor.tasmota_energy_total")
@ -269,7 +272,6 @@ async def test_indexed_sensor_state_via_mqtt2(hass, mqtt_mock, setup_tasmota):
)
state = hass.states.get("sensor.tasmota_energy_total")
assert state.state == "1.2"
assert state.attributes["last_reset"] == "2018-11-23T15:33:47+00:00"
# Test polled state update
async_fire_mqtt_message(
@ -279,7 +281,6 @@ async def test_indexed_sensor_state_via_mqtt2(hass, mqtt_mock, setup_tasmota):
)
state = hass.states.get("sensor.tasmota_energy_total")
assert state.state == "5.6"
assert state.attributes["last_reset"] == "2018-11-23T16:33:47+00:00"
async def test_bad_indexed_sensor_state_via_mqtt(hass, mqtt_mock, setup_tasmota):