mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-01-22 20:57:07 +00:00
Make uses_http a readonly property of device config (#1449)
`uses_http` will no longer be included in `DeviceConfig.to_dict()`
This commit is contained in:
parent
57f6c4138a
commit
be34dbd387
@ -8,7 +8,7 @@ from typing import Any
|
|||||||
|
|
||||||
from .device import Device
|
from .device import Device
|
||||||
from .device_type import DeviceType
|
from .device_type import DeviceType
|
||||||
from .deviceconfig import DeviceConfig, DeviceFamily
|
from .deviceconfig import DeviceConfig, DeviceEncryptionType, DeviceFamily
|
||||||
from .exceptions import KasaException, UnsupportedDeviceError
|
from .exceptions import KasaException, UnsupportedDeviceError
|
||||||
from .iot import (
|
from .iot import (
|
||||||
IotBulb,
|
IotBulb,
|
||||||
@ -176,25 +176,32 @@ def get_device_class_from_family(
|
|||||||
return cls
|
return cls
|
||||||
|
|
||||||
|
|
||||||
def get_protocol(
|
def get_protocol(config: DeviceConfig, *, strict: bool = False) -> BaseProtocol | None:
|
||||||
config: DeviceConfig,
|
"""Return the protocol from the device config.
|
||||||
) -> BaseProtocol | None:
|
|
||||||
"""Return the protocol from the connection name.
|
|
||||||
|
|
||||||
For cameras and vacuums the device family is a simple mapping to
|
For cameras and vacuums the device family is a simple mapping to
|
||||||
the protocol/transport. For other device types the transport varies
|
the protocol/transport. For other device types the transport varies
|
||||||
based on the discovery information.
|
based on the discovery information.
|
||||||
|
|
||||||
|
:param config: Device config to derive protocol
|
||||||
|
:param strict: Require exact match on encrypt type
|
||||||
"""
|
"""
|
||||||
ctype = config.connection_type
|
ctype = config.connection_type
|
||||||
protocol_name = ctype.device_family.value.split(".")[0]
|
protocol_name = ctype.device_family.value.split(".")[0]
|
||||||
|
|
||||||
if ctype.device_family is DeviceFamily.SmartIpCamera:
|
if ctype.device_family is DeviceFamily.SmartIpCamera:
|
||||||
|
if strict and ctype.encryption_type is not DeviceEncryptionType.Aes:
|
||||||
|
return None
|
||||||
return SmartCamProtocol(transport=SslAesTransport(config=config))
|
return SmartCamProtocol(transport=SslAesTransport(config=config))
|
||||||
|
|
||||||
if ctype.device_family is DeviceFamily.IotIpCamera:
|
if ctype.device_family is DeviceFamily.IotIpCamera:
|
||||||
|
if strict and ctype.encryption_type is not DeviceEncryptionType.Xor:
|
||||||
|
return None
|
||||||
return IotProtocol(transport=LinkieTransportV2(config=config))
|
return IotProtocol(transport=LinkieTransportV2(config=config))
|
||||||
|
|
||||||
if ctype.device_family is DeviceFamily.SmartTapoRobovac:
|
if ctype.device_family is DeviceFamily.SmartTapoRobovac:
|
||||||
|
if strict and ctype.encryption_type is not DeviceEncryptionType.Aes:
|
||||||
|
return None
|
||||||
return SmartProtocol(transport=SslTransport(config=config))
|
return SmartProtocol(transport=SslTransport(config=config))
|
||||||
|
|
||||||
protocol_transport_key = (
|
protocol_transport_key = (
|
||||||
|
@ -20,7 +20,7 @@ None
|
|||||||
{'host': '127.0.0.3', 'timeout': 5, 'credentials': {'username': 'user@example.com', \
|
{'host': '127.0.0.3', 'timeout': 5, 'credentials': {'username': 'user@example.com', \
|
||||||
'password': 'great_password'}, 'connection_type'\
|
'password': 'great_password'}, 'connection_type'\
|
||||||
: {'device_family': 'SMART.TAPOBULB', 'encryption_type': 'KLAP', 'login_version': 2, \
|
: {'device_family': 'SMART.TAPOBULB', 'encryption_type': 'KLAP', 'login_version': 2, \
|
||||||
'https': False}, 'uses_http': True}
|
'https': False}}
|
||||||
|
|
||||||
>>> later_device = await Device.connect(config=Device.Config.from_dict(config_dict))
|
>>> later_device = await Device.connect(config=Device.Config.from_dict(config_dict))
|
||||||
>>> print(later_device.alias) # Alias is available as connect() calls update()
|
>>> print(later_device.alias) # Alias is available as connect() calls update()
|
||||||
@ -148,9 +148,12 @@ class DeviceConfig(_DeviceConfigBaseMixin):
|
|||||||
DeviceFamily.IotSmartPlugSwitch, DeviceEncryptionType.Xor
|
DeviceFamily.IotSmartPlugSwitch, DeviceEncryptionType.Xor
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
#: True if the device uses http. Consumers should retrieve rather than set this
|
|
||||||
#: in order to determine whether they should pass a custom http client if desired.
|
@property
|
||||||
uses_http: bool = False
|
def uses_http(self) -> bool:
|
||||||
|
"""True if the device uses http."""
|
||||||
|
ctype = self.connection_type
|
||||||
|
return ctype.encryption_type is not DeviceEncryptionType.Xor or ctype.https
|
||||||
|
|
||||||
#: Set a custom http_client for the device to use.
|
#: Set a custom http_client for the device to use.
|
||||||
http_client: ClientSession | None = field(
|
http_client: ClientSession | None = field(
|
||||||
|
@ -360,7 +360,6 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
|
|||||||
json_func = Discover._get_discovery_json_legacy
|
json_func = Discover._get_discovery_json_legacy
|
||||||
device_func = Discover._get_device_instance_legacy
|
device_func = Discover._get_device_instance_legacy
|
||||||
elif port == Discover.DISCOVERY_PORT_2:
|
elif port == Discover.DISCOVERY_PORT_2:
|
||||||
config.uses_http = True
|
|
||||||
json_func = Discover._get_discovery_json
|
json_func = Discover._get_discovery_json
|
||||||
device_func = Discover._get_device_instance
|
device_func = Discover._get_device_instance
|
||||||
else:
|
else:
|
||||||
@ -634,6 +633,8 @@ class Discover:
|
|||||||
Device.Family.SmartTapoPlug,
|
Device.Family.SmartTapoPlug,
|
||||||
Device.Family.IotSmartPlugSwitch,
|
Device.Family.IotSmartPlugSwitch,
|
||||||
Device.Family.SmartIpCamera,
|
Device.Family.SmartIpCamera,
|
||||||
|
Device.Family.SmartTapoRobovac,
|
||||||
|
Device.Family.IotIpCamera,
|
||||||
}
|
}
|
||||||
candidates: dict[
|
candidates: dict[
|
||||||
tuple[type[BaseProtocol], type[BaseTransport], type[Device]],
|
tuple[type[BaseProtocol], type[BaseTransport], type[Device]],
|
||||||
@ -663,10 +664,9 @@ class Discover:
|
|||||||
port_override=port,
|
port_override=port,
|
||||||
credentials=credentials,
|
credentials=credentials,
|
||||||
http_client=http_client,
|
http_client=http_client,
|
||||||
uses_http=encrypt is not Device.EncryptionType.Xor,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
and (protocol := get_protocol(config))
|
and (protocol := get_protocol(config, strict=True))
|
||||||
and (
|
and (
|
||||||
device_class := get_device_class_from_family(
|
device_class := get_device_class_from_family(
|
||||||
device_family.value, https=https, require_exact=True
|
device_family.value, https=https, require_exact=True
|
||||||
|
@ -5,6 +5,5 @@
|
|||||||
"device_family": "SMART.IPCAMERA",
|
"device_family": "SMART.IPCAMERA",
|
||||||
"encryption_type": "AES",
|
"encryption_type": "AES",
|
||||||
"https": true
|
"https": true
|
||||||
},
|
}
|
||||||
"uses_http": false
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,5 @@
|
|||||||
"encryption_type": "KLAP",
|
"encryption_type": "KLAP",
|
||||||
"https": false,
|
"https": false,
|
||||||
"login_version": 2
|
"login_version": 2
|
||||||
},
|
}
|
||||||
"uses_http": false
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,5 @@
|
|||||||
"device_family": "IOT.SMARTPLUGSWITCH",
|
"device_family": "IOT.SMARTPLUGSWITCH",
|
||||||
"encryption_type": "XOR",
|
"encryption_type": "XOR",
|
||||||
"https": false
|
"https": false
|
||||||
},
|
}
|
||||||
"uses_http": false
|
|
||||||
}
|
}
|
||||||
|
@ -154,12 +154,10 @@ async def test_discover_single(discovery_mock, custom_port, mocker):
|
|||||||
discovery_mock.encrypt_type,
|
discovery_mock.encrypt_type,
|
||||||
discovery_mock.login_version,
|
discovery_mock.login_version,
|
||||||
)
|
)
|
||||||
uses_http = discovery_mock.default_port == 80
|
|
||||||
config = DeviceConfig(
|
config = DeviceConfig(
|
||||||
host=host,
|
host=host,
|
||||||
port_override=custom_port,
|
port_override=custom_port,
|
||||||
connection_type=ct,
|
connection_type=ct,
|
||||||
uses_http=uses_http,
|
|
||||||
credentials=Credentials(),
|
credentials=Credentials(),
|
||||||
)
|
)
|
||||||
assert x.config == config
|
assert x.config == config
|
||||||
|
Loading…
Reference in New Issue
Block a user