Skip to content

Commit

Permalink
refactor(integ_tests): add entity_id to mocked attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviml committed Jul 4, 2021
1 parent 5622f30 commit a535e2c
Show file tree
Hide file tree
Showing 36 changed files with 84 additions and 136 deletions.
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/type_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ async def _get_entities(self, entity_name: str) -> Optional[List[str]]:
f"Entities from `{entity_name}` (entity_id attribute): `{entities}`",
level="DEBUG",
)
# When the entity is not a group, this attribute returns the entity name
if isinstance(entities, str):
# If the entity groups other entities, this attribute will be a list
if not isinstance(entities, (list, tuple)):
return None
if entities is not None and len(entities) == 0:
raise ValueError(f"`{entity_name}` does not have any entities registered.")
Expand Down
2 changes: 0 additions & 2 deletions tests/integ_tests/action-types/arrow_left_click_test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions: [arrow_left_click]
expected_calls:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions: [arrow_left_click, 0.05, arrow_left_click]
expected_calls:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions:
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions:
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions:
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions:
[arrow_left_click, 0.05, arrow_left_click, 0.05, arrow_left_click]
Expand Down
10 changes: 4 additions & 6 deletions tests/integ_tests/action-types/arrow_right_click_test.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions: [arrow_right_click]
expected_calls:
- service: my_service
data:
attr1: 42
attr2: foo
- service: my_service
data:
attr1: 42
attr2: foo
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions: [brightness_down_click]
expected_calls:
Expand Down
8 changes: 3 additions & 5 deletions tests/integ_tests/action-types/brightness_up_click_test.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions: [brightness_up_click]
expected_calls:
- service: light/toggle
data:
entity_id: light.bedroom
- service: light/toggle
data:
entity_id: light.bedroom
8 changes: 3 additions & 5 deletions tests/integ_tests/action-types/toggle_test.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions: [toggle]
expected_calls:
- service: light/toggle
data:
entity_id: light.bedroom
- service: light/toggle
data:
entity_id: light.bedroom
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
entity_state_attributes:
supported_features: 191
brightness: 50
entity_state: "on"
fired_actions: [brightness_up_hold, 0.450, brightness_up_release]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions: [toggle, 1.0, toggle_hold]
expected_calls:
- service: light/toggle
data:
entity_id: light.livingroom
- service: light/turn_on
data:
entity_id: light.livingroom
xy_color: !!python/tuple [0.323, 0.329]
brightness: 255
- service: light/toggle
data:
entity_id: light.livingroom
- service: light/turn_on
data:
entity_id: light.livingroom
xy_color: !!python/tuple [0.323, 0.329]
brightness: 255
expected_calls_count: 2
3 changes: 1 addition & 2 deletions tests/integ_tests/example_config/group_light_test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
entity_state_attributes:
supported_features: 191
entity_entities: ["light.bedroom_1", "light.bedroom_2"]
entity_id: ["light.bedroom_1", "light.bedroom_2"]
entity_state: "off"
fired_actions: [toggle]
expected_calls:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# When the same action is called more than once within 300ms,
# then we ignore the rest of the calls that fall between that time.
entity_state_attributes:
supported_features: 191

entity_state: "off"
fired_actions: [toggle, toggle]
expected_calls:
- service: light/toggle
data:
entity_id: light.livingroom
- service: light/toggle
data:
entity_id: light.livingroom
20 changes: 9 additions & 11 deletions tests/integ_tests/integ_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from cx_core.type_controller import TypeController
from pytest_mock.plugin import MockerFixture

from tests.test_utils import fake_fn, get_controller
from tests.test_utils import get_controller


def get_integ_tests():
Expand All @@ -29,8 +29,8 @@ def read_config_yaml(file_name):
return list(data.values())[0]


def get_fake_entity_states(entity_state, entity_state_attributes):
async def inner(attribute: Optional[str] = None):
def get_fake_get_state(entity_state, entity_state_attributes):
async def inner(entity_name: str, attribute: Optional[str] = None):
if attribute is not None and attribute in entity_state_attributes:
return entity_state_attributes[attribute]
return entity_state
Expand All @@ -48,13 +48,16 @@ async def test_integ_configs(
):
entity_state_attributes = data.get("entity_state_attributes", {})
entity_state = data.get("entity_state", None)
entity_entities = data.get("entity_entities", None) # Used for group entities
fired_actions = data.get("fired_actions", [])
render_template_response = data.get("render_template_response")
extra = data.get("extra")
expected_calls = data.get("expected_calls", [])
expected_calls_count = data.get("expected_calls_count", len(expected_calls))

if "supported_features" not in entity_state_attributes:
entity_state_attributes["supported_features"] = 0b1111111111
if "entity_id" not in entity_state_attributes:
entity_state_attributes["entity_id"] = "my_entity"
config = read_config_yaml(config_file)
controller = get_controller(config["module"], config["class"])
if controller is None:
Expand All @@ -67,13 +70,8 @@ async def test_integ_configs(
)

if isinstance(controller, TypeController):
fake_entity_states = get_fake_entity_states(
entity_state, entity_state_attributes
)
mocker.patch.object(
controller, "get_state", fake_fn(entity_entities, async_=True)
)
mocker.patch.object(controller, "get_entity_state", fake_entity_states)
fake_get_state = get_fake_get_state(entity_state, entity_state_attributes)
mocker.patch.object(controller, "get_state", fake_get_state)
call_service_stub = mocker.patch.object(Hass, "call_service")

await controller.initialize()
Expand Down
5 changes: 1 addition & 4 deletions tests/integ_tests/merge_mapping/brightness_up_test.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [step_with_on_off_0_43_5]
expected_calls:
- service: my_script

- service: my_script
9 changes: 3 additions & 6 deletions tests/integ_tests/merge_mapping/toggle_test.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [toggle]
expected_calls:
- service: light/toggle
data:
entity_id: light.my_light

- service: light/toggle
data:
entity_id: light.my_light
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
entity_state_attributes:
supported_features: 191
entity_state: "off"
fired_actions: [6002]
extra:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
entity_state_attributes:
supported_features: 191

entity_state: "off"
fired_actions: ["color_wheel"]
extra:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [1000]
expected_calls:
- service: light/toggle
data:
entity_id: light.bedroom
entity_id: light.bedroom
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [1001]
expected_calls:
- service: light/toggle
data:
entity_id: light.bedroom
entity_id: light.bedroom
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [1002]
expected_calls:
- service: fake_service1002
- service: fake_service1002
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [1003]
expected_calls:
- service: light/toggle
data:
entity_id: light.bedroom
- service: light/toggle
data:
entity_id: light.bedroom
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [1003, 0.05, 1003]
expected_calls:
- service: fake_service1003
- service: fake_service1003
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [1003, 0.05, 1004, 0.05, 1003]
expected_calls:
- service: fake_service1003
- service: fake_service1003
4 changes: 1 addition & 3 deletions tests/integ_tests/multiple_clicks/toggle_1_click_test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: ["toggle"]
expected_calls:
- service: fake_service1
- service: fake_service1
4 changes: 1 addition & 3 deletions tests/integ_tests/multiple_clicks/toggle_2_clicks_test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: ["toggle", 0.05, "toggle"]
expected_calls:
- service: fake_service2
- service: fake_service2
4 changes: 1 addition & 3 deletions tests/integ_tests/multiple_clicks/toggle_3_clicks_test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: ["toggle", 0.05, "toggle", 0.05, "toggle"]
expected_calls:
- service: fake_service3
- service: fake_service3
7 changes: 3 additions & 4 deletions tests/integ_tests/multiple_clicks/toggle_5_clicks_test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: ["toggle", 0.05, "toggle", 0.05, "toggle", 0.05, "toggle", 0.05, "toggle"]
fired_actions:
["toggle", 0.05, "toggle", 0.05, "toggle", 0.05, "toggle", 0.05, "toggle"]
expected_calls:
- service: fake_service5
- service: fake_service5
23 changes: 19 additions & 4 deletions tests/integ_tests/multiple_clicks/toggle_8_clicks_test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: ["toggle", 0.05, "toggle", 0.05, "toggle", 0.05, "toggle", 0.05, "toggle", 0.05, "toggle", 0.05, "toggle", 0.05, "toggle"]
fired_actions:
[
"toggle",
0.05,
"toggle",
0.05,
"toggle",
0.05,
"toggle",
0.05,
"toggle",
0.05,
"toggle",
0.05,
"toggle",
0.05,
"toggle",
]
expected_calls:
- service: fake_service8
- service: fake_service8
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# from configuration, ControllerX should send color_temp and not xy_color
# because light_mode is auto.
entity_state_attributes:
supported_features: 0b1111111 # Everything supported from the device
color_temp: 200
entity_state: "on"
fired_actions: [arrow_right_hold, 0.450, arrow_right_release]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [toggle]
expected_calls:
- service: light/toggle
data:
entity_id: light.my_light
brightness: 255

- service: light/toggle
data:
entity_id: light.my_light
brightness: 255
Loading

0 comments on commit a535e2c

Please sign in to comment.