swbt-python 0.4.0__py3-none-any.whl → 0.5.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
swbt/__init__.py CHANGED
@@ -4,11 +4,14 @@ from swbt.adapter_discovery import AdapterInfo, list_adapters
4
4
  from swbt.diagnostics import DiagnosticsConfig, GamepadStatus
5
5
  from swbt.errors import (
6
6
  AdapterDiscoveryError,
7
+ AdapterIdentityRecoveryRequired,
7
8
  ClosedError,
8
9
  ConnectionFailedError,
9
10
  ConnectionTimeoutError,
10
11
  InvalidInputError,
11
12
  InvalidKeyStoreError,
13
+ InvalidProfileError,
14
+ ProfileControllerMismatchError,
12
15
  SwbtError,
13
16
  TransportOpenError,
14
17
  UnsupportedInputError,
@@ -30,6 +33,7 @@ from swbt.protocol.profiles.base import ControllerColors
30
33
 
31
34
  __all__ = (
32
35
  "AdapterDiscoveryError",
36
+ "AdapterIdentityRecoveryRequired",
33
37
  "AdapterInfo",
34
38
  "Button",
35
39
  "ClosedError",
@@ -47,10 +51,12 @@ __all__ = (
47
51
  "InputState",
48
52
  "InvalidInputError",
49
53
  "InvalidKeyStoreError",
54
+ "InvalidProfileError",
50
55
  "JoyConL",
51
56
  "JoyConR",
52
57
  "PeriodicSwitchGamepad",
53
58
  "ProController",
59
+ "ProfileControllerMismatchError",
54
60
  "Stick",
55
61
  "SwbtError",
56
62
  "SwitchGamepad",
swbt/_testing/gamepad.py CHANGED
@@ -19,7 +19,7 @@ def make_pro_controller(
19
19
  *,
20
20
  transport: HidDeviceTransport,
21
21
  adapter: str | None = None,
22
- key_store_path: str | None = None,
22
+ profile_path: str | None = None,
23
23
  report_period_us: int | None = None,
24
24
  controller_colors: ControllerColors | None = None,
25
25
  diagnostics: DiagnosticsConfig | None = None,
@@ -28,7 +28,7 @@ def make_pro_controller(
28
28
  return ProController._from_config(
29
29
  _SwitchGamepadConfig(
30
30
  adapter=adapter,
31
- key_store_path=key_store_path,
31
+ profile_path=profile_path,
32
32
  report_period_us=report_period_us,
33
33
  controller_colors=controller_colors,
34
34
  ),
@@ -41,7 +41,7 @@ def make_joycon_l(
41
41
  *,
42
42
  transport: HidDeviceTransport,
43
43
  adapter: str | None = None,
44
- key_store_path: str | None = None,
44
+ profile_path: str | None = None,
45
45
  report_period_us: int | None = None,
46
46
  controller_colors: ControllerColors | None = None,
47
47
  diagnostics: DiagnosticsConfig | None = None,
@@ -50,7 +50,7 @@ def make_joycon_l(
50
50
  return JoyConL._from_config(
51
51
  _SwitchGamepadConfig(
52
52
  adapter=adapter,
53
- key_store_path=key_store_path,
53
+ profile_path=profile_path,
54
54
  report_period_us=report_period_us,
55
55
  controller_colors=controller_colors,
56
56
  profile=JoyConLeftProfile(),
@@ -64,7 +64,7 @@ def make_joycon_r(
64
64
  *,
65
65
  transport: HidDeviceTransport,
66
66
  adapter: str | None = None,
67
- key_store_path: str | None = None,
67
+ profile_path: str | None = None,
68
68
  report_period_us: int | None = None,
69
69
  controller_colors: ControllerColors | None = None,
70
70
  diagnostics: DiagnosticsConfig | None = None,
@@ -73,7 +73,7 @@ def make_joycon_r(
73
73
  return JoyConR._from_config(
74
74
  _SwitchGamepadConfig(
75
75
  adapter=adapter,
76
- key_store_path=key_store_path,
76
+ profile_path=profile_path,
77
77
  report_period_us=report_period_us,
78
78
  controller_colors=controller_colors,
79
79
  profile=JoyConRightProfile(),
@@ -87,7 +87,7 @@ def make_direct_pro_controller(
87
87
  *,
88
88
  transport: HidDeviceTransport,
89
89
  adapter: str | None = None,
90
- key_store_path: str | None = None,
90
+ profile_path: str | None = None,
91
91
  controller_colors: ControllerColors | None = None,
92
92
  diagnostics: DiagnosticsConfig | None = None,
93
93
  ) -> DirectProController:
@@ -95,7 +95,7 @@ def make_direct_pro_controller(
95
95
  return DirectProController._from_config(
96
96
  _SwitchGamepadConfig(
97
97
  adapter=adapter,
98
- key_store_path=key_store_path,
98
+ profile_path=profile_path,
99
99
  controller_colors=controller_colors,
100
100
  ),
101
101
  diagnostics=diagnostics,
@@ -107,7 +107,7 @@ def make_direct_joycon_l(
107
107
  *,
108
108
  transport: HidDeviceTransport,
109
109
  adapter: str | None = None,
110
- key_store_path: str | None = None,
110
+ profile_path: str | None = None,
111
111
  controller_colors: ControllerColors | None = None,
112
112
  diagnostics: DiagnosticsConfig | None = None,
113
113
  ) -> DirectJoyConL:
@@ -115,7 +115,7 @@ def make_direct_joycon_l(
115
115
  return DirectJoyConL._from_config(
116
116
  _SwitchGamepadConfig(
117
117
  adapter=adapter,
118
- key_store_path=key_store_path,
118
+ profile_path=profile_path,
119
119
  controller_colors=controller_colors,
120
120
  profile=JoyConLeftProfile(),
121
121
  ),
@@ -128,7 +128,7 @@ def make_direct_joycon_r(
128
128
  *,
129
129
  transport: HidDeviceTransport,
130
130
  adapter: str | None = None,
131
- key_store_path: str | None = None,
131
+ profile_path: str | None = None,
132
132
  controller_colors: ControllerColors | None = None,
133
133
  diagnostics: DiagnosticsConfig | None = None,
134
134
  ) -> DirectJoyConR:
@@ -136,7 +136,7 @@ def make_direct_joycon_r(
136
136
  return DirectJoyConR._from_config(
137
137
  _SwitchGamepadConfig(
138
138
  adapter=adapter,
139
- key_store_path=key_store_path,
139
+ profile_path=profile_path,
140
140
  controller_colors=controller_colors,
141
141
  profile=JoyConRightProfile(),
142
142
  ),
swbt/diagnostics.py CHANGED
@@ -112,7 +112,7 @@ class DiagnosticsRecorder:
112
112
  return diagnostics_event
113
113
 
114
114
  def record_report_tx(self, *, report_id: int, reason: str) -> DiagnosticsEvent:
115
- """Record one sent report and increment its counter."""
115
+ """Record one transport-accepted report and increment its counter."""
116
116
  counter = self._report_counters.get(report_id, 0) + 1
117
117
  self._report_counters[report_id] = counter
118
118
  return self.record_event(
@@ -139,9 +139,7 @@ class DiagnosticsRecorder:
139
139
  self,
140
140
  *,
141
141
  adapter: str,
142
- key_store_exists: bool | None = None,
143
- key_store_path: str | None = None,
144
- key_store_previous_exists: bool | None = None,
142
+ profile_path: str | None = None,
145
143
  ) -> DiagnosticsEvent:
146
144
  """Record environment metadata for one diagnostics run."""
147
145
  fields: dict[str, object] = {
@@ -150,12 +148,8 @@ class DiagnosticsRecorder:
150
148
  "package_version": self._package_version(),
151
149
  "python_version": platform.python_version(),
152
150
  }
153
- if key_store_path is not None:
154
- fields["key_store_path"] = key_store_path
155
- if key_store_exists is not None:
156
- fields["key_store_exists"] = key_store_exists
157
- if key_store_previous_exists is not None:
158
- fields["key_store_previous_exists"] = key_store_previous_exists
151
+ if profile_path is not None:
152
+ fields["profile_path"] = profile_path
159
153
  return self.record_event(
160
154
  "run_metadata",
161
155
  **fields,
swbt/errors.py CHANGED
@@ -63,6 +63,43 @@ class InvalidKeyStoreError(SwbtError):
63
63
  """Raised when a key store has an unsupported or invalid shape."""
64
64
 
65
65
 
66
+ class InvalidProfileError(SwbtError):
67
+ """Raised when a pairing profile is invalid or unsupported."""
68
+
69
+
70
+ class ProfileControllerMismatchError(InvalidProfileError):
71
+ """Raised when a profile belongs to a different concrete controller kind."""
72
+
73
+ def __init__(
74
+ self,
75
+ *,
76
+ expected_controller_kind: str,
77
+ actual_controller_kind: str,
78
+ ) -> None:
79
+ """Initialize expected and actual profile controller kinds."""
80
+ message = (
81
+ "profile controller_kind mismatch: "
82
+ f"expected {expected_controller_kind!r}, got {actual_controller_kind!r}"
83
+ )
84
+ super().__init__(message)
85
+ self.expected_controller_kind = expected_controller_kind
86
+ self.actual_controller_kind = actual_controller_kind
87
+
88
+
89
+ class AdapterIdentityRecoveryRequired(SwbtError): # noqa: N818
90
+ """Raised when a volatile write may have changed the adapter identity."""
91
+
92
+ def __init__(self, *, target_address: str, stage: str) -> None:
93
+ """Initialize the target and failure stage requiring physical recovery."""
94
+ message = (
95
+ "adapter identity preparation became uncertain after write started; "
96
+ "unplug and reconnect the USB dongle before retrying"
97
+ )
98
+ super().__init__(message)
99
+ self.target_address = target_address
100
+ self.stage = stage
101
+
102
+
66
103
  class ProtocolError(SwbtError):
67
104
  """Raised when protocol bytes cannot be parsed or produced."""
68
105
 
swbt/gamepad/_config.py CHANGED
@@ -13,7 +13,7 @@ class _SwitchGamepadConfig:
13
13
 
14
14
  Attributes:
15
15
  adapter: Bumble adapter moniker, such as ``"usb:0"``.
16
- key_store_path: Path used by the default transport to persist pairing keys.
16
+ profile_path: Path to a swbt-owned pairing profile.
17
17
  profile: Fixed controller identity and protocol profile.
18
18
  report_period_us: Periodic input report interval in microseconds.
19
19
  device_name: HID device name advertised to the host.
@@ -21,7 +21,7 @@ class _SwitchGamepadConfig:
21
21
  """
22
22
 
23
23
  adapter: str | None = None
24
- key_store_path: str | None = None
24
+ profile_path: str | None = None
25
25
  profile: ControllerProfile = field(default_factory=default_controller_profile)
26
26
  report_period_us: int | None = None
27
27
  device_name: str | None = None
@@ -60,14 +60,14 @@ class _ControllerSpec:
60
60
  self,
61
61
  *,
62
62
  adapter: str | None,
63
- key_store_path: str | None,
64
63
  report_period_us: int | None,
65
64
  controller_colors: ControllerColors | None,
65
+ profile_path: str | None = None,
66
66
  ) -> _SwitchGamepadConfig:
67
67
  """Create internal construction config from public constructor options."""
68
68
  return _SwitchGamepadConfig(
69
69
  adapter=adapter,
70
- key_store_path=key_store_path,
70
+ profile_path=profile_path,
71
71
  profile=self.profile,
72
72
  report_period_us=report_period_us,
73
73
  controller_colors=controller_colors,
@@ -79,7 +79,7 @@ class _RuntimeConfig:
79
79
  """Normalized internal configuration for ControllerRuntime."""
80
80
 
81
81
  adapter: str | None
82
- key_store_path: str | None
82
+ profile_path: str | None
83
83
  profile: ControllerProfile
84
84
  report_period_us: int
85
85
  device_name: str
@@ -93,7 +93,7 @@ class _RuntimeConfig:
93
93
  raise InvalidInputError(msg)
94
94
  return cls(
95
95
  adapter=config.adapter,
96
- key_store_path=config.key_store_path,
96
+ profile_path=config.profile_path,
97
97
  profile=config.profile,
98
98
  report_period_us=config.report_period_us,
99
99
  device_name=config.device_name,
@@ -58,7 +58,7 @@ class ConnectionWorkflow:
58
58
  diagnostics: DiagnosticsRecorder
59
59
  ensure_open: EnsureOpen
60
60
  get_transport: TransportProvider
61
- key_store_path: str | None
61
+ profile_path: str | None
62
62
  pair: PairWithTimeout
63
63
  set_connection_state: StateSetter
64
64
  transport_was_injected: bool
@@ -71,10 +71,10 @@ class ConnectionWorkflow:
71
71
  """Try active reconnect with exactly one bonded peer."""
72
72
  await self.ensure_open()
73
73
  transport = self._transport()
74
- if self.key_store_path is None and not self.transport_was_injected:
74
+ if self.profile_path is None and not self.transport_was_injected:
75
75
  self.diagnostics.record_event(
76
- "reconnect_key_store_unavailable",
77
- reason="key_store_path_none",
76
+ "reconnect_profile_unavailable",
77
+ reason="profile_path_none",
78
78
  route="active_reconnect",
79
79
  )
80
80
  peers = await transport.list_bonded_peers()
@@ -113,11 +113,19 @@ class ConnectionWorkflow:
113
113
  route="active_reconnect",
114
114
  )
115
115
  try:
116
- await transport.connect_bonded_peer(
117
- peer.address,
118
- connect_timeout=timeout,
119
- )
120
- await self.wait_for_connected(timeout)
116
+
117
+ async def reconnect_and_wait() -> None:
118
+ await transport.connect_bonded_peer(
119
+ peer.address,
120
+ connect_timeout=timeout,
121
+ )
122
+ await self.wait_for_connected(None)
123
+
124
+ if timeout is None:
125
+ await reconnect_and_wait()
126
+ else:
127
+ async with asyncio.timeout(timeout):
128
+ await reconnect_and_wait()
121
129
  except TimeoutError:
122
130
  self.diagnostics.record_event(
123
131
  "active_reconnect_result",
@@ -172,6 +180,8 @@ class ConnectionWorkflow:
172
180
  await self.pair(timeout)
173
181
  except ConnectionTimeoutError:
174
182
  return ConnectionResult(route="pairing", status="timeout")
183
+ except ConnectionFailedError:
184
+ return ConnectionResult(route="pairing", status="failed")
175
185
  return ConnectionResult(route="pairing", status="connected")
176
186
 
177
187
  def _transport(self) -> HidDeviceTransport: