rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.71k stars 530 forks source link

Energy consumption cannot be added to the energy UI #1739

Open songwei2088w opened 1 week ago

songwei2088w commented 1 week ago

Unable to add the entity type name status_class, so it cannot be added to energy

eskalator commented 6 days ago

As i recall it is state_class property of the sensor entity that is lacking and that energy card uses/demands.

I've tinkered a little in the sensor.py namely adding state_class property

def state_class(self):
    """Return the state class of this device."""
    return self._config.get(CONF_STATE_CLASS)

and modyfying flow schema to accomodate this

def flow_schema(dps):
   """Return schema used in config flow."""
    return {
        vol.Optional(CONF_UNIT_OF_MEASUREMENT): str,
        vol.Optional(CONF_DEVICE_CLASS): vol.In(DEVICE_CLASSES),
        vol.Optional(CONF_STATE_CLASS): vol.In(STATE_CLASSES),
        vol.Optional(CONF_SCALING): vol.All(
            vol.Coerce(float), vol.Range(min=-1000000.0, max=1000000.0)
        ),
    }

The result is that i get bullet list to choose state class of the sensor during entity configuration, but it is not saved in resulting entity. As i have no experience in python(only common logic/observation-based copy paste ^^) i cannot continue this as it goes way beyond my skills. It seems like an easy thing to do for the author/maintainer who has the knowledge of the whole integration structure and used functions and it is very quality-of-life valuable feature.