diff --git a/tests/sensors/test_power.py b/tests/sensors/test_power.py index 348f234b7..8bd32bd59 100644 --- a/tests/sensors/test_power.py +++ b/tests/sensors/test_power.py @@ -3,13 +3,19 @@ import pytest from homeassistant.components import input_boolean, sensor from homeassistant.components.utility_meter.sensor import SensorDeviceClass +from homeassistant.components.vacuum import ( + ATTR_BATTERY_LEVEL, + STATE_CLEANING, + STATE_DOCKED, + STATE_RETURNING, +) from homeassistant.const import ( CONF_ENTITIES, CONF_ENTITY_ID, CONF_NAME, CONF_PLATFORM, STATE_OFF, - STATE_ON, + STATE_ON, CONF_ATTRIBUTE, ) from homeassistant.core import EVENT_HOMEASSISTANT_START, CoreState, HomeAssistant from homeassistant.setup import async_setup_component @@ -29,8 +35,10 @@ DOMAIN, DUMMY_ENTITY_ID, CalculationStrategy, + CONF_LINEAR, + CONF_CALIBRATE, + CONF_CALCULATION_ENABLED_CONDITION, ) -from custom_components.powercalc.errors import UnsupportedMode from ..common import ( create_input_boolean, @@ -193,3 +201,68 @@ async def test_error_when_no_strategy_has_been_configured( ) assert "Skipping sensor setup" in caplog.text + + +async def test_strategy_enabled_condition(hass: HomeAssistant): + """ + Test calculation_enabled_condition is working correctly. + This is used for example on robot vacuum cleaners. + This test simulates a vacuum cleaner going through following stages: + - cleaning + - returning + - docked + When the state is docked the calculation is activated and linear calibration is used to map the consumption while charging + """ + vacuum_entity_id = "vacuum.my_robot_cleaner" + power_entity_id = "sensor.my_robot_cleaner_power" + + await run_powercalc_setup_yaml_config( + hass, + { + CONF_ENTITY_ID: vacuum_entity_id, + CONF_CALCULATION_ENABLED_CONDITION: "{{ is_state('vacuum.my_robot_cleaner', 'docked') }}", + CONF_LINEAR: { + CONF_ATTRIBUTE: "battery_level", + CONF_CALIBRATE: [ + "1 -> 20", + "79 -> 20", + "80 -> 15", + "99 -> 8", + "100 -> 1.5" + ] + } + } + ) + + assert hass.states.get(power_entity_id) + + hass.states.async_set(vacuum_entity_id, STATE_CLEANING, {ATTR_BATTERY_LEVEL: 40}) + await hass.async_block_till_done() + + assert hass.states.get(power_entity_id).state == "0.00" + + hass.states.async_set(vacuum_entity_id, STATE_RETURNING, {ATTR_BATTERY_LEVEL: 40}) + await hass.async_block_till_done() + + assert hass.states.get(power_entity_id).state == "0.00" + + hass.states.async_set(vacuum_entity_id, STATE_DOCKED, {ATTR_BATTERY_LEVEL: 20}) + await hass.async_block_till_done() + + assert hass.states.get(power_entity_id).state == "20.00" + + hass.states.async_set(vacuum_entity_id, STATE_DOCKED, {ATTR_BATTERY_LEVEL: 60}) + await hass.async_block_till_done() + + assert hass.states.get(power_entity_id).state == "20.00" + + hass.states.async_set(vacuum_entity_id, STATE_DOCKED, {ATTR_BATTERY_LEVEL: 80}) + await hass.async_block_till_done() + + assert hass.states.get(power_entity_id).state == "15.00" + + hass.states.async_set(vacuum_entity_id, STATE_DOCKED, {ATTR_BATTERY_LEVEL: 100}) + await hass.async_block_till_done() + + assert hass.states.get(power_entity_id).state == "1.50" +