Conversation
- Replace blocking time.sleep() with asyncio.sleep() in HADiscoveryPublisher.connect() to avoid stalling the event loop during MQTT handshake - Make connect_mqtt() and HADiscoveryPublisher.connect() async; consolidate MQTT connection into the single asyncio.run() call in main() - Raise ValueError on invalid PKCS7 padding in _decrypt() instead of silently returning corrupt data - Guard publish_state() and publish_availability() with _connected check to surface failures instead of dropping data silently - Add is_connected guard in _trigger_update() before write_gatt_char() - Replace hard-coded asyncio.sleep(2) after trigger_update() with an event-based wait (clear then await with timeout) to avoid stale signals - Validate required sensor keys (mac_address, device_id, local_key) at config load time so missing keys fail fast at startup Nightshift-Task: bug-finder Nightshift-Ref: https://github.com/marcus/nightshift
Clear _response_event after _trigger_update() returns and before the follow-up wait, so a DP_WRITE ack that resolved the internal wait in _trigger_update() does not cause the outer wait_for() to return immediately with a stale signal instead of properly waiting for the DP_REPORT notification. Nightshift-Task: bug-finder Nightshift-Ref: https://github.com/marcus/nightshift
Use rjust instead of ljust when zero-padding VALUE-type DPs to 4 bytes before big-endian unpacking. ljust appends zeros to the right (LSB side), corrupting temperature and moisture readings for payloads shorter than 4 bytes. rjust correctly left-pads (MSB side) to preserve the value. Nightshift-Task: bug-finder Nightshift-Ref: https://github.com/marcus/nightshift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
time.sleep()withasyncio.sleep()inHADiscoveryPublisher.connect()— the old code stalled the event loop for up to 5 seconds during every MQTT connection attempt. Madeconnect()andconnect_mqtt()async and consolidated into a singleasyncio.run()inmain()._decrypt()— invalid PKCS7 padding (padding_len == 0or> 16) previously returned garbage bytes; now raisesValueErrorso callers can handle the error properly.publish_state()andpublish_availability()with_connectedcheck — previously called_client.publish()when disconnected, silently losing data.is_connectedguard in_trigger_update()beforewrite_gatt_char()— prevents anAttributeError/BleakErrorif the device drops connection between the initial query and the trigger.asyncio.sleep(2)after_trigger_update()inread_sensors()with an event-based wait (_response_event.clear()thenwait_for(..., timeout=5.0)) — avoids stale signals and gives a proper timeout/log on failure.mac_address,device_id,local_key) inload_config()— missing keys now cause an early, descriptive error at startup instead of aKeyErrorduring live polling.Test plan
_decrypt()with padding_len > 16 — expectValueError--onceagainst a real SGS01 and confirm MQTT messages appear in HApublish_statelogs error instead of crashinglocal_keyand confirm startup exits with a clear errorpython3 -m ruff check pi-fallback/🤖 Generated with Claude Code