Skip to content

Commit

Permalink
test(integ_tests): add more integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviml committed Sep 21, 2020
1 parent 6e82103 commit 7295e64
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 19 deletions.
6 changes: 3 additions & 3 deletions docs/examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ bedroom_speaker:
Controlling a light (just on/off) with E1743 with ZHA:

```yaml
bedroom_speaker:
bedroom_light:
module: controllerx
class: E1743Controller
controller: 00:67:88:56:06:78:9b:3f
integration: zha
light: light.simple_light
actions:
- on
- off
- "on"
- "off"
```

Controlling two lights with Aqara double key wireless switch (z2m):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: ["on", 450, "off"]
expected_calls:
- service: light/turn_on
data:
entity_id: light.simple_light
transition: 0.3
- service: light/turn_off
data:
entity_id: light.simple_light
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fired_actions: ["brightness_up", "brightness_down", "brightness_stop"]
expected_calls_count: 0
9 changes: 9 additions & 0 deletions tests/integ_tests/actions_attribute/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bedroom_light:
module: controllerx
class: E1743Controller
controller: 00:67:88:56:06:78:9b:3f
integration: zha
light: light.simple_light
actions:
- "on"
- "off"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
entity_state_attributes:
supported_features: 175
entity_state: "on"
fired_actions: ["toggle", 1000, "toggle_hold"]
expected_calls:
- service: light/toggle
data:
entity_id: light.bedroom
- service: light/turn_on
data:
entity_id: light.bedroom
color_temp: 370
brightness: 255
transition: 0.3
expected_calls_count: 2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
expected_calls_count: 0
40 changes: 24 additions & 16 deletions tests/integ_tests/integ_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,39 @@ def read_config_yaml(file_name):
return list(data.values())[0]


def get_controller(module_name, class_name):
module = importlib.import_module(module_name)
class_ = getattr(module, class_name)
return class_()


def get_fake_entity_states(entity_state, entity_state_attributes):
async def inner(entity_id, attribute=None):
if attribute is not None and attribute in entity_state_attributes:
return entity_state_attributes[attribute]
return entity_state

return inner


integration_tests = get_integ_tests()


@pytest.mark.asyncio
@pytest.mark.parametrize("config_file, data", integration_tests)
async def test_example_config(hass_mock, mocker, config_file, data):
entity_state_attributes = data["entity_state_attributes"]
entity_state = data["entity_state"]
fired_actions = data["fired_actions"]
expected_calls = data["expected_calls"]
entity_state_attributes = data.get("entity_state_attributes", {})
entity_state = data.get("entity_state", None)
fired_actions = data.get("fired_actions", [])
expected_calls = data.get("expected_calls", [])
expected_calls_count = data.get("expected_calls_count", len(expected_calls))

config = read_config_yaml(config_file)
module_name = config["module"]
class_name = config["class"]
module = importlib.import_module(module_name)
class_ = getattr(module, class_name)
controller = class_()
controller = get_controller(config["module"], config["class"])
controller.args = config

async def fake_supported_features(entity_id, attribute=None):
if attribute is not None and attribute in entity_state_attributes:
return entity_state_attributes[attribute]
return entity_state

mocker.patch.object(controller, "get_entity_state", fake_supported_features)
fake_entity_states = get_fake_entity_states(entity_state, entity_state_attributes)
mocker.patch.object(controller, "get_entity_state", fake_entity_states)
call_service_stub = mocker.patch.object(controller, "call_service")

await controller.initialize()
Expand All @@ -64,7 +71,8 @@ async def fake_supported_features(entity_id, attribute=None):
elif isinstance(action, int):
await asyncio.sleep(action / 1000)

await asyncio.wait(tasks) # Finish pending tasks
if tasks: # Finish pending tasks if any
await asyncio.wait(tasks)
assert call_service_stub.call_count == expected_calls_count
calls = [mocker.call(call["service"], **call["data"]) for call in expected_calls]
call_service_stub.assert_has_calls(calls)

0 comments on commit 7295e64

Please sign in to comment.