Skip to content

Commit

Permalink
Update test for changes to restore
Browse files Browse the repository at this point in the history
  • Loading branch information
rgc99 committed Feb 7, 2022
1 parent aa5b2c0 commit 86ac73f
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions tests/test_restore_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from homeassistant.config import load_yaml_config_file
from homeassistant.setup import async_setup_component

from custom_components.irrigation_unlimited.const import COORDINATOR, DOMAIN
from custom_components.irrigation_unlimited.const import (
COORDINATOR,
DOMAIN,
EVENT_INCOMPLETE,
)
from custom_components.irrigation_unlimited.__init__ import CONFIG_SCHEMA
from custom_components.irrigation_unlimited.irrigation_unlimited import IUCoordinator
from custom_components.irrigation_unlimited.entity import IURestore
Expand Down Expand Up @@ -76,6 +80,25 @@ def mock_state_coordinator():
yield


@pytest.fixture(name="mock_state_coordinator_is_on")
def mock_state_coordinator_is_on():
"""Patch HA history"""
with patch(
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state"
) as mock:
# pylint: disable=line-too-long
dct = {
"configuration": '{"version": "1.0.0", "controllers": [{"index": 0, "name": "My Garden", "state": "on", "enabled": true, "icon": "mdi:water", "status": "on", "zones": [{"index": 0, "name": "Front Lawn", "state": "on", "enabled": true, "icon": "mdi:valve-open", "zone_id": "1", "status": "on", "adjustment": "", "current_duration": "90", "schedules": []}, {"index": 1, "name": "Vege Patch", "state": "off", "enabled": true, "icon": "mdi:valve-closed", "zone_id": "2", "status": "off", "adjustment": "", "current_duration": "", "schedules": []}, {"index": 2, "name": "Roses", "state": "off", "enabled": true, "icon": "mdi:valve-closed", "zone_id": "3", "status": "off", "adjustment": "", "current_duration": "", "schedules": []}, {"index": 3, "name": "Back Yard", "state": "off", "enabled": true, "icon": "mdi:valve-closed", "zone_id": "4", "status": "off", "adjustment": "", "current_duration": "", "schedules": []}], "sequences": [{"index": 0, "name": "Main run", "state": "on", "enabled": true, "icon": "mdi:play-circle-outline", "status": "on", "default_duration": "600", "default_delay": "20", "duration_factor": 1.0, "total_delay": "40", "total_duration": "1800", "adjusted_duration": "1800", "current_duration": "300", "adjustment": "", "sequence_zones": [{"index": 0, "state": "on", "enabled": true, "icon": "mdi:play-circle-outline", "status": "on", "delay": "20", "base_duration": "600", "adjusted_duration": "600", "final_duration": "600", "zones": [1], "current_duration": "90", "adjustment": ""}, {"index": 1, "state": "off", "enabled": true, "icon": "mdi:stop-circle-outline", "status": "off", "delay": "20", "base_duration": "600", "adjusted_duration": "600", "final_duration": "600", "zones": [2], "current_duration": "", "adjustment": ""}, {"index": 2, "state": "off", "enabled": true, "icon": "mdi:stop-circle-outline", "status": "off", "delay": "20", "base_duration": "600", "adjusted_duration": "600", "final_duration": "600", "zones": [3], "current_duration": "", "adjustment": ""}]}]}]}'
}
mock.return_value = ha.State(
"irrigation_unlimited.coordinator",
"ok",
dct,
datetime.fromisoformat("2021-01-04 04:30:00+00:00"),
)
yield


# pylint: disable=unused-argument
# pylint: disable=redefined-outer-name
async def test_restore_none(
Expand Down Expand Up @@ -360,10 +383,35 @@ async def test_restore_coordinator(
}
]
}
assert list(IURestore(data, coordinator).is_on) == [
assert list(IURestore(data, coordinator).report_is_on()) == [
"0,-,0,-",
"0,2,1,2",
"0,3,1,2",
"0,2,2,2",
"0,3,2,2",
]


async def test_restore_coordinator_events(
hass: ha.HomeAssistant,
skip_dependencies,
skip_history,
mock_state_coordinator_is_on,
):
"""Test restoring coordinator in on state"""

event_data = []

def handle_event(event: ha.Event):
nonlocal event_data
event_data.append(event.data)

hass.bus.async_listen(f"{DOMAIN}_{EVENT_INCOMPLETE}", handle_event)

full_path = TEST_CONFIG_DIR + "test_restore_entity_sequence.yaml"
config = CONFIG_SCHEMA(load_yaml_config_file(full_path))
await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()

# This is work in progress. No event is currently fired.
assert event_data == []

0 comments on commit 86ac73f

Please sign in to comment.