Skip to content

Commit 7c4d0e5

Browse files
style: fix all ruff check, format, and mypy errors (#2)
* Fix all ruff check errors: line length, import sorting, and type-checking blocks Co-authored-by: deviantintegral <255023+deviantintegral@users.noreply.github.com> * style: apply ruff format to all files Co-authored-by: deviantintegral <255023+deviantintegral@users.noreply.github.com> * style: fix all mypy type checking errors Co-authored-by: deviantintegral <255023+deviantintegral@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: deviantintegral <255023+deviantintegral@users.noreply.github.com>
1 parent b510d12 commit 7c4d0e5

23 files changed

Lines changed: 857 additions & 1671 deletions

‎src/flameconnect/b2c_login.py‎

Lines changed: 27 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,13 @@ def _parse_login_page(html: str, page_url: str) -> dict[str, str]:
6262
# Extract CSRF token from var SETTINGS = {..., "csrf":"...", ...}
6363
csrf_match = re.search(r'"csrf"\s*:\s*"([^"]+)"', html)
6464
if not csrf_match:
65-
raise AuthenticationError(
66-
"Could not find CSRF token in B2C login page"
67-
)
65+
raise AuthenticationError("Could not find CSRF token in B2C login page")
6866
csrf = csrf_match.group(1)
6967

7068
# Extract transId from SETTINGS
7169
tx_match = re.search(r'"transId"\s*:\s*"([^"]+)"', html)
7270
if not tx_match:
73-
raise AuthenticationError(
74-
"Could not find transId in B2C login page"
75-
)
71+
raise AuthenticationError("Could not find transId in B2C login page")
7672
tx = tx_match.group(1)
7773

7874
p = _B2C_POLICY
@@ -84,10 +80,7 @@ def _parse_login_page(html: str, page_url: str) -> dict[str, str]:
8480
qs = f"tx={tx}&p={p}"
8581

8682
post_url = f"{origin}{base}SelfAsserted?{qs}"
87-
confirmed_url = (
88-
f"{origin}{base}"
89-
f"api/CombinedSigninAndSignup/confirmed"
90-
)
83+
confirmed_url = f"{origin}{base}api/CombinedSigninAndSignup/confirmed"
9184

9285
return {
9386
"csrf": csrf,
@@ -109,10 +102,8 @@ def _build_cookie_header(
109102
browsers send them). This function formats cookies in the plain
110103
``name=value; name2=value2`` style that B2C requires.
111104
"""
112-
filtered = cookie_jar.filter_cookies(url)
113-
return "; ".join(
114-
f"{m.key}={m.value}" for m in filtered.values()
115-
)
105+
filtered = cookie_jar.filter_cookies(yarl.URL(url))
106+
return "; ".join(f"{m.key}={m.value}" for m in filtered.values())
116107

117108

118109
def _log_request(
@@ -130,10 +121,7 @@ def _log_request(
130121
if headers:
131122
_LOGGER.debug(">>> headers: %s", headers)
132123
if data:
133-
safe = {
134-
k: ("***" if k == "password" else v)
135-
for k, v in data.items()
136-
}
124+
safe = {k: ("***" if k == "password" else v) for k, v in data.items()}
137125
_LOGGER.debug(">>> body: %s", safe)
138126

139127

@@ -143,7 +131,9 @@ def _log_response(
143131
) -> None:
144132
"""Log an incoming HTTP response at DEBUG level."""
145133
_LOGGER.debug(
146-
"<<< %s %s", resp.status, resp.url,
134+
"<<< %s %s",
135+
resp.status,
136+
resp.url,
147137
)
148138
_LOGGER.debug("<<< headers: %s", dict(resp.headers))
149139
if body is not None:
@@ -153,9 +143,7 @@ def _log_response(
153143
_LOGGER.debug("<<< body: %s", preview)
154144

155145

156-
async def b2c_login_with_credentials(
157-
auth_uri: str, email: str, password: str
158-
) -> str:
146+
async def b2c_login_with_credentials(auth_uri: str, email: str, password: str) -> str:
159147
"""Submit credentials directly to Azure AD B2C and return the redirect URL.
160148
161149
Performs the same HTTP flow a browser would:
@@ -216,12 +204,8 @@ async def b2c_login_with_credentials(
216204
"X-Requested-With": "XMLHttpRequest",
217205
"Referer": auth_uri,
218206
"Origin": origin,
219-
"Accept": (
220-
"application/json, text/javascript, */*; q=0.01"
221-
),
222-
"Content-Type": (
223-
"application/x-www-form-urlencoded; charset=UTF-8"
224-
),
207+
"Accept": ("application/json, text/javascript, */*; q=0.01"),
208+
"Content-Type": ("application/x-www-form-urlencoded; charset=UTF-8"),
225209
}
226210

227211
# Build an unquoted Cookie header — aiohttp's cookie jar
@@ -255,17 +239,11 @@ async def b2c_login_with_credentials(
255239
_log_response(resp, body)
256240
if resp.status != 200:
257241
raise AuthenticationError(
258-
f"Credential submission returned HTTP"
259-
f" {resp.status}"
242+
f"Credential submission returned HTTP {resp.status}"
260243
)
261244
# Check for error in the JSON-like response
262-
if (
263-
'"status":"400"' in body
264-
or '"status": "400"' in body
265-
):
266-
raise AuthenticationError(
267-
"Invalid email or password"
268-
)
245+
if '"status":"400"' in body or '"status": "400"' in body:
246+
raise AuthenticationError("Invalid email or password")
269247
# Merge cookies set by the POST response (e.g.
270248
# updated x-ms-cpim-cache and x-ms-cpim-trans)
271249
# into the cookie header for the confirmed GET.
@@ -276,16 +254,12 @@ async def b2c_login_with_credentials(
276254
if "=" in part:
277255
n, v = part.split("=", 1)
278256
cookies[n] = v
279-
for raw_sc in resp.headers.getall(
280-
"Set-Cookie", []
281-
):
257+
for raw_sc in resp.headers.getall("Set-Cookie", []):
282258
sc_pair = raw_sc.split(";", 1)[0]
283259
if "=" in sc_pair:
284260
n, v = sc_pair.split("=", 1)
285261
cookies[n] = v
286-
cookie_header = "; ".join(
287-
f"{n}={v}" for n, v in cookies.items()
288-
)
262+
cookie_header = "; ".join(f"{n}={v}" for n, v in cookies.items())
289263

290264
# Step 4: GET the confirmed endpoint — follows redirects
291265
# until we hit the custom-scheme redirect
@@ -300,9 +274,7 @@ async def b2c_login_with_credentials(
300274
)
301275

302276
# Follow redirects manually to catch custom-scheme one
303-
next_url: str = (
304-
fields["confirmed_url"] + "?" + confirmed_qs
305-
)
277+
next_url: str = fields["confirmed_url"] + "?" + confirmed_qs
306278
confirmed_headers = {
307279
"Cookie": cookie_header,
308280
}
@@ -316,53 +288,39 @@ async def b2c_login_with_credentials(
316288
resp_body = await resp.text()
317289
_log_response(resp, resp_body)
318290
if resp.status in (301, 302, 303, 307, 308):
319-
location = resp.headers.get(
320-
"Location", ""
321-
)
291+
location = resp.headers.get("Location", "")
322292
if not location:
323293
raise AuthenticationError(
324-
"Redirect without Location"
325-
" header"
294+
"Redirect without Location header"
326295
)
327296
# Custom-scheme redirect (msal{id}://auth)
328-
if (
329-
location.startswith("msal")
330-
and "://auth" in location
331-
):
297+
if location.startswith("msal") and "://auth" in location:
332298
_LOGGER.debug(
333-
"Captured custom-scheme"
334-
" redirect: %s",
299+
"Captured custom-scheme redirect: %s",
335300
location[:120] + "...",
336301
)
337302
return location
338303
# Resolve relative URLs
339304
if not location.startswith("http"):
340-
location = urljoin(
341-
next_url, location
342-
)
305+
location = urljoin(next_url, location)
343306
next_url = location
344307
continue
345308
if resp.status == 200:
346309
redirect_match = re.search(
347-
r'(msal[a-f0-9-]+://auth'
310+
r"(msal[a-f0-9-]+://auth"
348311
r'\?[^\s"\'<]+)',
349312
resp_body,
350313
)
351314
if redirect_match:
352315
return redirect_match.group(1)
353316
raise AuthenticationError(
354-
"Reached 200 response without"
355-
" finding redirect URL"
317+
"Reached 200 response without finding redirect URL"
356318
)
357319
raise AuthenticationError(
358-
"Unexpected HTTP"
359-
f" {resp.status} during"
360-
" redirect chain"
320+
f"Unexpected HTTP {resp.status} during redirect chain"
361321
)
362322

363-
raise AuthenticationError(
364-
"Too many redirects during B2C login"
365-
)
323+
raise AuthenticationError("Too many redirects during B2C login")
366324
except AuthenticationError:
367325
raise
368326
except aiohttp.ClientError as exc:

‎src/flameconnect/cli.py‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,8 @@ def _display_mode(
211211
) -> None:
212212
"""Display Mode parameter."""
213213
unit = temp_unit.unit if temp_unit else TempUnit.CELSIUS
214-
unit_suffix = (
215-
"C" if unit == TempUnit.CELSIUS else "F"
216-
) if temp_unit else ""
217-
display_temp = _convert_temp(
218-
param.target_temperature, unit
219-
)
214+
unit_suffix = ("C" if unit == TempUnit.CELSIUS else "F") if temp_unit else ""
215+
display_temp = _convert_temp(param.target_temperature, unit)
220216
print("\n [321] Mode")
221217
print(f" {'─' * 40}")
222218
mode = _enum_name(_FIRE_MODE_NAMES, param.mode)
@@ -253,12 +249,8 @@ def _display_heat(
253249
) -> None:
254250
"""Display HeatSettings parameter."""
255251
unit = temp_unit.unit if temp_unit else TempUnit.CELSIUS
256-
unit_suffix = (
257-
"C" if unit == TempUnit.CELSIUS else "F"
258-
) if temp_unit else ""
259-
display_temp = _convert_temp(
260-
param.setpoint_temperature, unit
261-
)
252+
unit_suffix = ("C" if unit == TempUnit.CELSIUS else "F") if temp_unit else ""
253+
display_temp = _convert_temp(param.setpoint_temperature, unit)
262254
print("\n [323] Heat Settings")
263255
print(f" {'─' * 40}")
264256
status = _enum_name(_HEAT_STATUS_NAMES, param.heat_status)
@@ -566,9 +558,7 @@ async def _set_brightness(client: FlameConnectClient, fire_id: str, value: str)
566558
print(f"Brightness set to {value}.")
567559

568560

569-
async def _set_pulsating(
570-
client: FlameConnectClient, fire_id: str, value: str
571-
) -> None:
561+
async def _set_pulsating(client: FlameConnectClient, fire_id: str, value: str) -> None:
572562
"""Set pulsating effect on or off."""
573563
if value not in _PULSATING_LOOKUP:
574564
valid = ", ".join(_PULSATING_LOOKUP)
@@ -690,9 +680,7 @@ async def _set_timer(client: FlameConnectClient, fire_id: str, value: str) -> No
690680
print("Timer disabled.")
691681

692682

693-
async def _set_temp_unit(
694-
client: FlameConnectClient, fire_id: str, value: str
695-
) -> None:
683+
async def _set_temp_unit(client: FlameConnectClient, fire_id: str, value: str) -> None:
696684
"""Set the temperature display unit."""
697685
if value not in _TEMP_UNIT_LOOKUP:
698686
valid = ", ".join(_TEMP_UNIT_LOOKUP)
@@ -704,7 +692,6 @@ async def _set_temp_unit(
704692
print(f"Temperature unit set to {value}.")
705693

706694

707-
708695
async def _set_flame_effect(
709696
client: FlameConnectClient, fire_id: str, value: str
710697
) -> None:
@@ -822,6 +809,7 @@ async def _set_ambient_sensor(
822809
await client.write_parameters(fire_id, [new_param])
823810
print(f"Ambient sensor set to {value}.")
824811

812+
825813
async def cmd_tui(*, verbose: bool = False) -> None:
826814
"""Launch the TUI, showing install message if missing."""
827815
try:

‎src/flameconnect/models.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ class RGBWColor:
141141
white: int
142142

143143

144-
145144
NAMED_COLORS: dict[str, RGBWColor] = {
146145
"dark-red": RGBWColor(red=180, green=0, blue=0, white=0),
147146
"light-red": RGBWColor(red=255, green=0, blue=0, white=80),

‎src/flameconnect/protocol.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ def _decode_mode(raw: bytes) -> ModeParam:
9292
_check_length(raw, 6, "Mode")
9393
mode = FireMode(raw[3])
9494
target_temperature = _decode_temperature(raw, 4)
95-
_LOGGER.debug("Decoded Mode: mode=%s target_temperature=%.1f", mode, target_temperature)
95+
_LOGGER.debug(
96+
"Decoded Mode: mode=%s target_temperature=%.1f",
97+
mode,
98+
target_temperature,
99+
)
96100
return ModeParam(mode=mode, target_temperature=target_temperature)
97101

98102

@@ -318,7 +322,8 @@ def _encode_heat_settings(param: HeatParam) -> bytes:
318322
param.setpoint_temperature,
319323
param.boost_duration,
320324
)
321-
wire_boost = max(0, param.boost_duration - 1) # model is 1-indexed, wire is 0-indexed
325+
# model is 1-indexed, wire is 0-indexed
326+
wire_boost = max(0, param.boost_duration - 1)
322327
payload = (
323328
bytes([param.heat_status, param.heat_mode])
324329
+ _encode_temperature(param.setpoint_temperature)

0 commit comments

Comments
 (0)