Add energy management for efergy (#57472)

This commit is contained in:
Robert Hillis 2021-10-11 07:16:55 -04:00 committed by GitHub
parent 30154763f8
commit a827521138
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View file

@ -10,6 +10,8 @@ import voluptuous as vol
from homeassistant.components.efergy import EfergyEntity from homeassistant.components.efergy import EfergyEntity
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
@ -40,12 +42,14 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
name="Power Usage", name="Power Usage",
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
state_class=STATE_CLASS_MEASUREMENT,
), ),
SensorEntityDescription( SensorEntityDescription(
key="energy_day", key="energy_day",
name="Daily Consumption", name="Daily Consumption",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_TOTAL_INCREASING,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
@ -53,6 +57,7 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
name="Weekly Consumption", name="Weekly Consumption",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_TOTAL_INCREASING,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
@ -60,12 +65,14 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
name="Monthly Consumption", name="Monthly Consumption",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_TOTAL_INCREASING,
), ),
SensorEntityDescription( SensorEntityDescription(
key="energy_year", key="energy_year",
name="Yearly Consumption", name="Yearly Consumption",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
state_class=STATE_CLASS_TOTAL_INCREASING,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
@ -77,23 +84,27 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
key="cost_day", key="cost_day",
name="Daily Energy Cost", name="Daily Energy Cost",
device_class=DEVICE_CLASS_MONETARY, device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_TOTAL_INCREASING,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
key="cost_week", key="cost_week",
name="Weekly Energy Cost", name="Weekly Energy Cost",
device_class=DEVICE_CLASS_MONETARY, device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_TOTAL_INCREASING,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
key="cost_month", key="cost_month",
name="Monthly Energy Cost", name="Monthly Energy Cost",
device_class=DEVICE_CLASS_MONETARY, device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_TOTAL_INCREASING,
), ),
SensorEntityDescription( SensorEntityDescription(
key="cost_year", key="cost_year",
name="Yearly Energy Cost", name="Yearly Energy Cost",
device_class=DEVICE_CLASS_MONETARY, device_class=DEVICE_CLASS_MONETARY,
state_class=STATE_CLASS_TOTAL_INCREASING,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
@ -101,6 +112,7 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
name="Power Usage", name="Power Usage",
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
state_class=STATE_CLASS_MEASUREMENT,
), ),
) )

View file

@ -2,7 +2,12 @@
from datetime import timedelta from datetime import timedelta
from homeassistant.components.efergy.sensor import SENSOR_TYPES from homeassistant.components.efergy.sensor import SENSOR_TYPES
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.sensor import (
ATTR_STATE_CLASS,
DOMAIN as SENSOR_DOMAIN,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
)
from homeassistant.const import ( from homeassistant.const import (
ATTR_DEVICE_CLASS, ATTR_DEVICE_CLASS,
ATTR_UNIT_OF_MEASUREMENT, ATTR_UNIT_OF_MEASUREMENT,
@ -37,42 +42,52 @@ async def test_sensor_readings(
assert state.state == "1580" assert state.state == "1580"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
state = hass.states.get("sensor.energy_budget") state = hass.states.get("sensor.energy_budget")
assert state.state == "ok" assert state.state == "ok"
assert state.attributes.get(ATTR_DEVICE_CLASS) is None assert state.attributes.get(ATTR_DEVICE_CLASS) is None
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
assert state.attributes.get(ATTR_STATE_CLASS) is None
state = hass.states.get("sensor.daily_consumption") state = hass.states.get("sensor.daily_consumption")
assert state.state == "38.21" assert state.state == "38.21"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_ENERGY assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_ENERGY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
state = hass.states.get("sensor.weekly_consumption") state = hass.states.get("sensor.weekly_consumption")
assert state.state == "267.47" assert state.state == "267.47"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_ENERGY assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_ENERGY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
state = hass.states.get("sensor.monthly_consumption") state = hass.states.get("sensor.monthly_consumption")
assert state.state == "1069.88" assert state.state == "1069.88"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_ENERGY assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_ENERGY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
state = hass.states.get("sensor.yearly_consumption") state = hass.states.get("sensor.yearly_consumption")
assert state.state == "13373.50" assert state.state == "13373.50"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_ENERGY assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_ENERGY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == ENERGY_KILO_WATT_HOUR
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
state = hass.states.get("sensor.daily_energy_cost") state = hass.states.get("sensor.daily_energy_cost")
assert state.state == "5.27" assert state.state == "5.27"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_MONETARY assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_MONETARY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR"
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
state = hass.states.get("sensor.weekly_energy_cost") state = hass.states.get("sensor.weekly_energy_cost")
assert state.state == "36.89" assert state.state == "36.89"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_MONETARY assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_MONETARY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR"
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
state = hass.states.get("sensor.monthly_energy_cost") state = hass.states.get("sensor.monthly_energy_cost")
assert state.state == "147.56" assert state.state == "147.56"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_MONETARY assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_MONETARY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR"
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
state = hass.states.get("sensor.yearly_energy_cost") state = hass.states.get("sensor.yearly_energy_cost")
assert state.state == "1844.50" assert state.state == "1844.50"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_MONETARY assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_MONETARY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR"
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
entity = ent_reg.async_get("sensor.power_usage_728386") entity = ent_reg.async_get("sensor.power_usage_728386")
assert entity.disabled_by == er.DISABLED_INTEGRATION assert entity.disabled_by == er.DISABLED_INTEGRATION
ent_reg.async_update_entity(entity.entity_id, **{"disabled_by": None}) ent_reg.async_update_entity(entity.entity_id, **{"disabled_by": None})
@ -82,6 +97,7 @@ async def test_sensor_readings(
assert state.state == "1628" assert state.state == "1628"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
async def test_multi_sensor_readings( async def test_multi_sensor_readings(
@ -95,14 +111,17 @@ async def test_multi_sensor_readings(
assert state.state == "218" assert state.state == "218"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
state = hass.states.get("sensor.power_usage_0") state = hass.states.get("sensor.power_usage_0")
assert state.state == "1808" assert state.state == "1808"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
state = hass.states.get("sensor.power_usage_728387") state = hass.states.get("sensor.power_usage_728387")
assert state.state == "312" assert state.state == "312"
assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER assert state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == POWER_WATT
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
async def test_failed_update_and_reconnection( async def test_failed_update_and_reconnection(