There are two components with open pull requests that seem to meet your need exactly. Hopefully they are accepted and merged for the next release:
Sometimes a device only provides power information (measured in W or kW).
This sensor does an integration of Power and time in order to measure energy consumed in kWh (standard energy measurement unit)
A utility meter sensor provides functionality to track consumptions of various utilities (e.g. energy, gaz, water, heating). From a User perspective utility meters operate in cycles (usually monthly) for billing purposes, this sensor will automatically reset the counter based on the configured cycle. On a reset an attribute will store the previous counter, providing the means for comparison operations (“did I spend more or less this month?”)
If you are feeling adventurous, you could pull the code from either (or both) of these and use them as custom components probably; until they get merged.