mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-12-01 21:58:17 +00:00
Update to use http port from discovery if present
This commit is contained in:
@@ -159,6 +159,7 @@ def create_discovery_mock(ip: str, fixture_data: dict):
|
||||
https: bool
|
||||
login_version: int | None = None
|
||||
port_override: int | None = None
|
||||
http_port: int | None = None
|
||||
|
||||
@property
|
||||
def model(self) -> str:
|
||||
@@ -194,9 +195,15 @@ def create_discovery_mock(ip: str, fixture_data: dict):
|
||||
):
|
||||
login_version = max([int(i) for i in et])
|
||||
https = discovery_result["mgt_encrypt_schm"]["is_support_https"]
|
||||
http_port = discovery_result["mgt_encrypt_schm"].get("http_port")
|
||||
if not http_port: # noqa: SIM108
|
||||
# Not all discovery responses set the http port, i.e. smartcam.
|
||||
default_port = 443 if https else 80
|
||||
else:
|
||||
default_port = http_port
|
||||
dm = _DiscoveryMock(
|
||||
ip,
|
||||
80,
|
||||
default_port,
|
||||
20002,
|
||||
discovery_data,
|
||||
fixture_data,
|
||||
@@ -204,6 +211,7 @@ def create_discovery_mock(ip: str, fixture_data: dict):
|
||||
encrypt_type,
|
||||
https,
|
||||
login_version,
|
||||
http_port=http_port,
|
||||
)
|
||||
else:
|
||||
sys_info = fixture_data["system"]["get_sysinfo"]
|
||||
|
||||
@@ -63,8 +63,9 @@ def _get_connection_type_device_class(discovery_info):
|
||||
connection_type = DeviceConnectionParameters.from_values(
|
||||
dr.device_type,
|
||||
dr.mgt_encrypt_schm.encrypt_type,
|
||||
dr.mgt_encrypt_schm.lv,
|
||||
dr.mgt_encrypt_schm.is_support_https,
|
||||
login_version=dr.mgt_encrypt_schm.lv,
|
||||
https=dr.mgt_encrypt_schm.is_support_https,
|
||||
http_port=dr.mgt_encrypt_schm.http_port,
|
||||
)
|
||||
else:
|
||||
connection_type = DeviceConnectionParameters.from_values(
|
||||
|
||||
@@ -157,14 +157,15 @@ async def test_discover_single(discovery_mock, custom_port, mocker):
|
||||
)
|
||||
# Make sure discovery does not call update()
|
||||
assert update_mock.call_count == 0
|
||||
if discovery_mock.default_port == 80:
|
||||
if discovery_mock.default_port != 9999:
|
||||
assert x.alias is None
|
||||
|
||||
ct = DeviceConnectionParameters.from_values(
|
||||
discovery_mock.device_type,
|
||||
discovery_mock.encrypt_type,
|
||||
discovery_mock.login_version,
|
||||
discovery_mock.https,
|
||||
login_version=discovery_mock.login_version,
|
||||
https=discovery_mock.https,
|
||||
http_port=discovery_mock.http_port,
|
||||
)
|
||||
config = DeviceConfig(
|
||||
host=host,
|
||||
@@ -425,9 +426,9 @@ async def test_discover_single_http_client(discovery_mock, mocker):
|
||||
|
||||
x: Device = await Discover.discover_single(host)
|
||||
|
||||
assert x.config.uses_http == (discovery_mock.default_port == 80)
|
||||
assert x.config.uses_http == (discovery_mock.default_port != 9999)
|
||||
|
||||
if discovery_mock.default_port == 80:
|
||||
if discovery_mock.default_port != 9999:
|
||||
assert x.protocol._transport._http_client.client != http_client
|
||||
x.config.http_client = http_client
|
||||
assert x.protocol._transport._http_client.client == http_client
|
||||
@@ -442,9 +443,9 @@ async def test_discover_http_client(discovery_mock, mocker):
|
||||
|
||||
devices = await Discover.discover(discovery_timeout=0)
|
||||
x: Device = devices[host]
|
||||
assert x.config.uses_http == (discovery_mock.default_port == 80)
|
||||
assert x.config.uses_http == (discovery_mock.default_port != 9999)
|
||||
|
||||
if discovery_mock.default_port == 80:
|
||||
if discovery_mock.default_port != 9999:
|
||||
assert x.protocol._transport._http_client.client != http_client
|
||||
x.config.http_client = http_client
|
||||
assert x.protocol._transport._http_client.client == http_client
|
||||
@@ -674,8 +675,9 @@ async def test_discover_try_connect_all(discovery_mock, mocker):
|
||||
cparams = DeviceConnectionParameters.from_values(
|
||||
discovery_mock.device_type,
|
||||
discovery_mock.encrypt_type,
|
||||
discovery_mock.login_version,
|
||||
discovery_mock.https,
|
||||
login_version=discovery_mock.login_version,
|
||||
https=discovery_mock.https,
|
||||
http_port=discovery_mock.http_port,
|
||||
)
|
||||
protocol = get_protocol(
|
||||
DeviceConfig(discovery_mock.ip, connection_type=cparams)
|
||||
@@ -687,10 +689,13 @@ async def test_discover_try_connect_all(discovery_mock, mocker):
|
||||
protocol_class = IotProtocol
|
||||
transport_class = XorTransport
|
||||
|
||||
default_port = discovery_mock.default_port
|
||||
|
||||
async def _query(self, *args, **kwargs):
|
||||
if (
|
||||
self.__class__ is protocol_class
|
||||
and self._transport.__class__ is transport_class
|
||||
and self._transport._port == default_port
|
||||
):
|
||||
return discovery_mock.query_data
|
||||
raise KasaException("Unable to execute query")
|
||||
@@ -699,6 +704,7 @@ async def test_discover_try_connect_all(discovery_mock, mocker):
|
||||
if (
|
||||
self.protocol.__class__ is protocol_class
|
||||
and self.protocol._transport.__class__ is transport_class
|
||||
and self.protocol._transport._port == default_port
|
||||
):
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user