Skip to content

Commit

Permalink
Add test for vacuum cleaner profile
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Aug 21, 2022
1 parent 2d8920c commit 059f3df
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions tests/sensors/test_power.py
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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"

0 comments on commit 059f3df

Please sign in to comment.