-
Notifications
You must be signed in to change notification settings - Fork 66
Add Weather Station device support #460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fankai777
wants to merge
5
commits into
sblibs:master
Choose a base branch
from
fankai777:feat/weather
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+203
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c0e9ef0
feat: add Weather Station device support
fankai777 3422bb6
chore(pre-commit.ci): auto fixes
pre-commit-ci[bot] a1e6f4d
test: improve Weather Station test coverage to 100%
fankai777 8bea55e
fix: add length guard for short service data in weather station parser
fankai777 2a8b8cf
chore(pre-commit.ci): auto fixes
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """Weather Station parser.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from ..helpers import celsius_to_fahrenheit | ||
|
|
||
|
|
||
| def process_weather_station( | ||
| data: bytes | None, mfr_data: bytes | None | ||
| ) -> dict[str, Any]: | ||
| """ | ||
| Process Weather Station advertisement data. | ||
|
|
||
| Manufacturer data layout (mfr_id=2409, after company ID stripped by bleak): | ||
| Byte 0-5: MAC address | ||
| Byte 6: Sequence number | ||
| Byte 7: Battery (bit7=charging, bit6-0=level%) | ||
| Byte 8: Temp alarm(bit7-6), Humidity alarm(bit5-4), Temp decimal(bit3-0) | ||
| Byte 9: Temp sign(bit7: 0=neg,1=pos), Temp integer(bit6-0) | ||
| Byte 10: Fahrenheit flag(bit7), Humidity(bit6-0) | ||
| """ | ||
| temp_data: bytes | None = None | ||
| battery: int | None = None | ||
|
|
||
| if mfr_data and len(mfr_data) >= 11: | ||
| temp_data = mfr_data[8:11] | ||
| battery = mfr_data[7] & 0b01111111 | ||
|
|
||
| if data and len(data) >= 6: | ||
| if not temp_data: | ||
| temp_data = data[3:6] | ||
| if battery is None: | ||
| battery = data[2] & 0b01111111 | ||
|
|
||
| if not temp_data: | ||
| return {} | ||
|
|
||
| _temp_sign = 1 if temp_data[1] & 0b10000000 else -1 | ||
| _temp_c = _temp_sign * ( | ||
| (temp_data[1] & 0b01111111) + ((temp_data[0] & 0b00001111) / 10) | ||
| ) | ||
| _temp_f = celsius_to_fahrenheit(_temp_c) | ||
| _temp_f = (_temp_f * 10) / 10 | ||
| humidity = temp_data[2] & 0b01111111 | ||
|
|
||
| if _temp_c == 0 and humidity == 0 and battery == 0: | ||
| return {} | ||
|
|
||
| return { | ||
| "temp": {"c": _temp_c, "f": _temp_f}, | ||
| "temperature": _temp_c, | ||
| "fahrenheit": bool(temp_data[2] & 0b10000000), | ||
| "humidity": humidity, | ||
| "battery": battery, | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.