Skip to content

Commit

Permalink
Fix inconsistent constructor signatures for device classes (#1606)
Browse files Browse the repository at this point in the history
This makes all `__init__()` parameters consistent for Device-derived
classes.
  • Loading branch information
rytilahti authored Nov 28, 2022
1 parent 02b0101 commit 4911ea2
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 9 deletions.
5 changes: 4 additions & 1 deletion miio/airconditioningcompanion.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,12 @@ def __init__(
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
timeout: int = None,
model: str = MODEL_ACPARTNER_V2,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=model)
super().__init__(
ip, token, start_id, debug, lazy_discover, timeout=timeout, model=model
)

if self.model not in MODELS_SUPPORTED:
_LOGGER.error(
Expand Down
5 changes: 4 additions & 1 deletion miio/airconditioningcompanionMCN.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ def __init__(
start_id: int = None,
debug: int = 0,
lazy_discover: bool = True,
timeout: int = None,
model: str = MODEL_ACPARTNER_MCN02,
) -> None:
if start_id is None:
start_id = random.randint(0, 999) # nosec
super().__init__(ip, token, start_id, debug, lazy_discover, model=model)
super().__init__(
ip, token, start_id, debug, lazy_discover, timeout=timeout, model=model
)

if model != MODEL_ACPARTNER_MCN02:
_LOGGER.error(
Expand Down
5 changes: 4 additions & 1 deletion miio/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,14 @@ def __init__(
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
timeout: int = None,
*,
model: str = None,
push_server=None,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=model)
super().__init__(
ip, token, start_id, debug, lazy_discover, timeout=timeout, model=model
)

self._alarm = Alarm(parent=self)
self._radio = Radio(parent=self)
Expand Down
5 changes: 4 additions & 1 deletion miio/huizuo.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def __init__(
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
timeout: int = None,
model: str = MODEL_HUIZUO_PIS123,
) -> None:

Expand All @@ -230,7 +231,9 @@ def __init__(
if model in MODELS_WITH_HEATER:
self.mapping.update(_ADDITIONAL_MAPPING_HEATER)

super().__init__(ip, token, start_id, debug, lazy_discover, model=model)
super().__init__(
ip, token, start_id, debug, lazy_discover, timeout=timeout, model=model
)

if model not in MODELS_SUPPORTED:
self._model = MODEL_HUIZUO_PIS123
Expand Down
5 changes: 4 additions & 1 deletion miio/integrations/fan/dmaker/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ def __init__(
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
timeout: int = None,
model: str = MODEL_FAN_P5,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=model)
super().__init__(
ip, token, start_id, debug, lazy_discover, timeout=timeout, model=model
)

@command(
default_output=format_output(
Expand Down
5 changes: 4 additions & 1 deletion miio/integrations/fan/dmaker/fan_miot.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,12 @@ def __init__(
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
timeout: int = None,
model: str = MODEL_FAN_1C,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=model)
super().__init__(
ip, token, start_id, debug, lazy_discover, timeout=timeout, model=model
)

@command(
default_output=format_output(
Expand Down
5 changes: 4 additions & 1 deletion miio/integrations/light/yeelight/yeelight.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,12 @@ def __init__(
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
timeout: int = None,
model: str = None,
) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover, model=model)
super().__init__(
ip, token, start_id, debug, lazy_discover, timeout=timeout, model=model
)

self._model_info = Yeelight._spec_helper.get_model_info(self.model)
self._light_type = YeelightSubLightType.Main
Expand Down
6 changes: 5 additions & 1 deletion miio/integrations/vacuum/roborock/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,14 @@ def __init__(
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
timeout: int = None,
*,
model=None,
):
super().__init__(ip, token, start_id, debug, model=model)
super().__init__(
ip, token, start_id, debug, lazy_discover, timeout, model=model
)
self.manual_seqnum = -1

@command()
Expand Down
9 changes: 8 additions & 1 deletion miio/integrations/vacuum/viomi/viomivacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,18 @@ def __init__(
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = False,
timeout: int = None,
*,
model: str = None,
) -> None:
super().__init__(
ip, token, start_id, debug, lazy_discover=lazy_discover, model=model
ip,
token,
start_id,
debug,
lazy_discover=lazy_discover,
timeout=timeout,
model=model,
)
self.manual_seqnum = -1
self._cache: Dict[str, Any] = {"edge_state": None, "rooms": {}, "maps": {}}
Expand Down
23 changes: 23 additions & 0 deletions miio/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,26 @@ def test_device_ctor_model(cls):
def test_device_supported_models(cls):
"""Make sure that every device subclass has a non-empty supported models."""
assert cls.supported_models


@pytest.mark.parametrize("cls", DEVICE_CLASSES)
def test_init_signature(cls, mocker):
"""Make sure that __init__ of every device-inheriting class accepts the expected
parameters."""
mocker.patch("miio.Device.send")
parent_init = mocker.spy(Device, "__init__")
kwargs = {
"ip": "IP",
"token": None,
"start_id": 0,
"debug": False,
"lazy_discover": True,
"timeout": None,
"model": None,
}
cls(**kwargs)

# A rather hacky way to check for the arguments, we cannot use assert_called_with
# as some arguments are passed by inheriting classes using kwargs
total_args = len(parent_init.call_args.args) + len(parent_init.call_args.kwargs)
assert total_args == 8

0 comments on commit 4911ea2

Please sign in to comment.