|
| 1 | +from datetime import timedelta |
| 2 | + |
1 | 3 | from homeassistant.components.vacuum import STATE_CLEANING, STATE_DOCKED |
2 | 4 | from homeassistant.const import CONF_ENTITY_ID |
3 | 5 | from homeassistant.core import HomeAssistant |
4 | 6 | from homeassistant.helpers.device_registry import DeviceEntry |
| 7 | +from homeassistant.util import dt |
5 | 8 | from pytest_homeassistant_custom_component.common import ( |
6 | 9 | RegistryEntryWithDefaults, |
| 10 | + async_fire_time_changed, |
7 | 11 | mock_device_registry, |
8 | 12 | mock_registry, |
9 | 13 | ) |
@@ -88,3 +92,77 @@ async def test_vacuum_robot( |
88 | 92 | await hass.async_block_till_done() |
89 | 93 |
|
90 | 94 | assert hass.states.get(power_sensor_id).state == "1.50" |
| 95 | + |
| 96 | + |
| 97 | +async def test_with_tapering_playbook(hass: HomeAssistant) -> None: |
| 98 | + vacuum_id = "vacuum.roomba" |
| 99 | + battery_id = "sensor.roomba_battery" |
| 100 | + |
| 101 | + mock_registry( |
| 102 | + hass, |
| 103 | + { |
| 104 | + vacuum_id: RegistryEntryWithDefaults( |
| 105 | + entity_id=vacuum_id, |
| 106 | + unique_id="unique_vacuum_1", |
| 107 | + platform="test", |
| 108 | + device_id="device_1", |
| 109 | + ), |
| 110 | + battery_id: RegistryEntryWithDefaults( |
| 111 | + entity_id=battery_id, |
| 112 | + unique_id="unique_battery_1", |
| 113 | + platform="test", |
| 114 | + device_id="device_1", |
| 115 | + device_class="battery", |
| 116 | + ), |
| 117 | + }, |
| 118 | + ) |
| 119 | + mock_device_registry( |
| 120 | + hass, |
| 121 | + { |
| 122 | + "device_1": DeviceEntry( |
| 123 | + id="device_1", |
| 124 | + ), |
| 125 | + }, |
| 126 | + ) |
| 127 | + |
| 128 | + power_sensor_id = "sensor.roomba_power" |
| 129 | + |
| 130 | + await run_powercalc_setup( |
| 131 | + hass, |
| 132 | + { |
| 133 | + CONF_ENTITY_ID: vacuum_id, |
| 134 | + CONF_CUSTOM_MODEL_DIRECTORY: get_test_profile_dir("vacuum_robot_tapering"), |
| 135 | + }, |
| 136 | + ) |
| 137 | + |
| 138 | + hass.states.async_set(battery_id, 30) |
| 139 | + hass.states.async_set(vacuum_id, STATE_DOCKED) |
| 140 | + await hass.async_block_till_done() |
| 141 | + |
| 142 | + assert hass.states.get(power_sensor_id).state == "20.00" |
| 143 | + |
| 144 | + hass.states.async_set(battery_id, 100) |
| 145 | + hass.states.async_set(vacuum_id, STATE_DOCKED) |
| 146 | + await hass.async_block_till_done() |
| 147 | + |
| 148 | + assert hass.states.get(power_sensor_id).state == "0.00" |
| 149 | + |
| 150 | + async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=1)) |
| 151 | + await hass.async_block_till_done() |
| 152 | + |
| 153 | + assert hass.states.get(power_sensor_id).state == "9.00" |
| 154 | + |
| 155 | + async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=3)) |
| 156 | + await hass.async_block_till_done() |
| 157 | + |
| 158 | + assert hass.states.get(power_sensor_id).state == "5.00" |
| 159 | + |
| 160 | + async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=5)) |
| 161 | + await hass.async_block_till_done() |
| 162 | + |
| 163 | + assert hass.states.get(power_sensor_id).state == "3.00" |
| 164 | + |
| 165 | + async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=60)) |
| 166 | + await hass.async_block_till_done() |
| 167 | + |
| 168 | + assert hass.states.get(power_sensor_id).state == "3.00" |
0 commit comments