mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 10:44:04 +00:00
Use ruff and ruff format (#534)
Replaces the previously used linting and code formatting tools with ruff.
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
import logging
|
||||
import re
|
||||
|
||||
from voluptuous import Coerce # type: ignore
|
||||
from voluptuous import REMOVE_EXTRA, All, Any, Invalid, Optional, Range, Schema
|
||||
from voluptuous import (
|
||||
REMOVE_EXTRA,
|
||||
All,
|
||||
Any,
|
||||
Coerce, # type: ignore
|
||||
Invalid,
|
||||
Optional,
|
||||
Range,
|
||||
Schema,
|
||||
)
|
||||
|
||||
from ..protocol import TPLinkSmartHomeProtocol
|
||||
|
||||
@@ -305,7 +313,9 @@ class FakeTransportProtocol(TPLinkSmartHomeProtocol):
|
||||
|
||||
self.proto = proto
|
||||
|
||||
def set_alias(self, x, child_ids=[]):
|
||||
def set_alias(self, x, child_ids=None):
|
||||
if child_ids is None:
|
||||
child_ids = []
|
||||
_LOGGER.debug("Setting alias to %s, child_ids: %s", x["alias"], child_ids)
|
||||
if child_ids:
|
||||
for child in self.proto["system"]["get_sysinfo"]["children"]:
|
||||
@@ -314,7 +324,9 @@ class FakeTransportProtocol(TPLinkSmartHomeProtocol):
|
||||
else:
|
||||
self.proto["system"]["get_sysinfo"]["alias"] = x["alias"]
|
||||
|
||||
def set_relay_state(self, x, child_ids=[]):
|
||||
def set_relay_state(self, x, child_ids=None):
|
||||
if child_ids is None:
|
||||
child_ids = []
|
||||
_LOGGER.debug("Setting relay state to %s", x["state"])
|
||||
|
||||
if not child_ids and "children" in self.proto["system"]["get_sysinfo"]:
|
||||
@@ -362,12 +374,10 @@ class FakeTransportProtocol(TPLinkSmartHomeProtocol):
|
||||
_LOGGER.debug("Current light state: %s", light_state)
|
||||
new_state = light_state
|
||||
|
||||
if state_changes["on_off"] == 1: # turn on requested
|
||||
if not light_state[
|
||||
"on_off"
|
||||
]: # if we were off, use the dft_on_state as a base
|
||||
_LOGGER.debug("Bulb was off, using dft_on_state")
|
||||
new_state = light_state["dft_on_state"]
|
||||
# turn on requested, if we were off, use the dft_on_state as a base
|
||||
if state_changes["on_off"] == 1 and not light_state["on_off"]:
|
||||
_LOGGER.debug("Bulb was off, using dft_on_state")
|
||||
new_state = light_state["dft_on_state"]
|
||||
|
||||
# override the existing settings
|
||||
new_state.update(state_changes)
|
||||
@@ -384,7 +394,7 @@ class FakeTransportProtocol(TPLinkSmartHomeProtocol):
|
||||
self.proto["system"]["get_sysinfo"]["light_state"] = new_state
|
||||
|
||||
def set_preferred_state(self, new_state, *args):
|
||||
"""Implementation of set_preferred_state."""
|
||||
"""Implement set_preferred_state."""
|
||||
self.proto["system"]["get_sysinfo"]["preferred_state"][
|
||||
new_state["index"]
|
||||
] = new_state
|
||||
@@ -459,11 +469,11 @@ class FakeTransportProtocol(TPLinkSmartHomeProtocol):
|
||||
child_ids = []
|
||||
|
||||
def get_response_for_module(target):
|
||||
if target not in proto.keys():
|
||||
if target not in proto:
|
||||
return error(msg="target not found")
|
||||
|
||||
def get_response_for_command(cmd):
|
||||
if cmd not in proto[target].keys():
|
||||
if cmd not in proto[target]:
|
||||
return error(msg=f"command {cmd} not found")
|
||||
|
||||
params = request[target][cmd]
|
||||
|
@@ -156,7 +156,7 @@ async def test_credentials(discovery_data: dict, mocker):
|
||||
mocker.patch("kasa.cli.state", new=_state)
|
||||
|
||||
# Get the type string parameter from the discovery_info
|
||||
for cli_device_type in {
|
||||
for cli_device_type in { # noqa: B007
|
||||
i
|
||||
for i in TYPE_TO_CLASS
|
||||
if TYPE_TO_CLASS[i] == Discover._get_device_class(discovery_data)
|
||||
|
@@ -71,7 +71,7 @@ async def test_discover_single(discovery_data: dict, mocker, custom_port):
|
||||
x = await Discover.discover_single(host, port=custom_port)
|
||||
assert issubclass(x.__class__, SmartDevice)
|
||||
assert x._sys_info is not None
|
||||
assert x.port == custom_port or 9999
|
||||
assert x.port == custom_port or x.port == 9999
|
||||
|
||||
|
||||
@pytest.mark.parametrize("custom_port", [123, None])
|
||||
@@ -82,7 +82,7 @@ async def test_connect_single(discovery_data: dict, mocker, custom_port):
|
||||
|
||||
dev = await Discover.connect_single(host, port=custom_port)
|
||||
assert issubclass(dev.__class__, SmartDevice)
|
||||
assert dev.port == custom_port or 9999
|
||||
assert dev.port == custom_port or dev.port == 9999
|
||||
|
||||
|
||||
async def test_connect_single_query_fails(discovery_data: dict, mocker):
|
||||
|
@@ -28,9 +28,10 @@ async def test_state_info(dev):
|
||||
|
||||
@pytest.mark.requires_dummy
|
||||
async def test_invalid_connection(dev):
|
||||
with patch.object(FakeTransportProtocol, "query", side_effect=SmartDeviceException):
|
||||
with pytest.raises(SmartDeviceException):
|
||||
await dev.update()
|
||||
with patch.object(
|
||||
FakeTransportProtocol, "query", side_effect=SmartDeviceException
|
||||
), pytest.raises(SmartDeviceException):
|
||||
await dev.update()
|
||||
|
||||
|
||||
@has_emeter
|
||||
|
Reference in New Issue
Block a user