-
-
Notifications
You must be signed in to change notification settings - Fork 19
Add modernized async API client with native response structure and unit parsing #271
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: edenhaus <[email protected]>
Co-authored-by: edenhaus <[email protected]>
Co-authored-by: edenhaus <[email protected]>
Co-authored-by: edenhaus <[email protected]>
edenhaus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot fix requested changes
…versions, fix session handling Co-authored-by: edenhaus <[email protected]>
All requested changes have been addressed in commit abd21fe:
|
edenhaus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Implement requested changes
aioecowitt/api.py
Outdated
| cleaned = (value.replace("%", "").replace("°", "").replace("V", "") | ||
| .replace("hPa", "").replace("inHg", "").replace("mmHg", "") | ||
| .replace("mm", "").replace("in", "").replace("mph", "") | ||
| .replace("m/s", "").replace("km/h", "").replace("W/m2", "")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot remove them completely, we need to add them as a separate field to data as HA needs to know in which unit of measurement the data is provided
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added unit parsing back with comprehensive enum support. The API now parses values like "20.7 C" into separate value and unit fields so Home Assistant knows the measurement units. Implemented dedicated enums for different measurement types (Temperature, Pressure, Distance, Speed, etc.) in commit 1acba58.
|
|
||
| # Parse LDS sensors | ||
| if "ch_lds" in live_data: | ||
| for item in live_data["ch_lds"]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are still flattening the data, which we should not do it....
Please refactor the code to keep them grouped....
An example response from the gateway is:
{
"common_list": [
{
"id": "0x02",
"val": "20.7",
"unit": "C"
},
{
"id": "0x07",
"val": "58%"
},
{
"id": "3",
"val": "20.7",
"unit": "C"
},
{
"id": "5",
"val": "1.03 kPa"
},
{
"id": "0x03",
"val": "12.2",
"unit": "C"
},
{
"id": "0x0B",
"val": "1.6 m/s"
},
{
"id": "0x0C",
"val": "1.7 m/s"
},
{
"id": "0x19",
"val": "3.8 m/s"
},
{
"id": "0x15",
"val": "450.04 W/m2"
},
{
"id": "0x17",
"val": "4"
},
{
"id": "0x0A",
"val": "209"
},
{
"id": "0x6D",
"val": "254"
}
],
"piezoRain": [
{
"id": "srain_piezo",
"val": "0"
},
{
"id": "0x0D",
"val": "0.0 mm"
},
{
"id": "0x0E",
"val": "0.0 mm/Hr"
},
{
"id": "0x7C",
"val": "0.0 mm"
},
{
"id": "0x10",
"val": "0.0 mm"
},
{
"id": "0x11",
"val": "0.0 mm"
},
{
"id": "0x12",
"val": "57.2 mm"
},
{
"id": "0x13",
"val": "835.5 mm",
"battery": "1",
"voltage": "2.28",
"ws90cap_volt": "5.4",
"ws90_ver": "133"
}
],
"wh25": [
{
"intemp": "26.9",
"unit": "C",
"inhumi": "44%",
"abs": "895.0 hPa",
"rel": "895.0 hPa"
}
],
"ch_lds": [
{
"channel": "1",
"name": "",
"unit": "mm",
"battery": "5",
"voltage": "3.28",
"air": "1565 mm",
"depth": "--.-",
"total_height": "0",
"total_heat": "15"
}
],
"debug": [
{
"heap": "97980",
"runtime": "630104",
"usr_interval": "30",
"is_cnip": false
}
]
}As you can see, some properties have a dedicated unit field, and some have the unit as a suffix. We should convert so that all have the unit as a separate field. For the unit, we should use an enum for it. A separate enum per type (e.g. one for temperature, one for wind spee,d and so on)
Also ch_lds is already grouped and we should not flatten it but use this group for our data structure too...
Requesting /get_sensors_info?page=1 currently there exists two pages
Example response looks like:
[{
"img": "wh85",
"type": "49",
"name": "Wind & Rain",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh90",
"type": "48",
"name": "Temp & Humidity & Solar & Wind & Rain",
"version": "133",
"id": "4AD4",
"batt": "1",
"rssi": "-65",
"signal": "4",
"idst": "1"
},{
"img": "wh69",
"type": "0",
"name": "Temp & Humidity & Solar & Wind & Rain",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh68",
"type": "1",
"name": "Solar & Wind",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh40",
"type": "3",
"name": "Rain",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wn20",
"type": "70",
"name": "Rain Mini",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh25",
"type": "4",
"name": "Temp & Humidity & Pressure",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh26",
"type": "5",
"name": "Temp & Humidity",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh80",
"type": "2",
"name": "Temp & Humidity & Solar & Wind",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh57",
"type": "26",
"name": "Lightning",
"id": "13CE9",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh45",
"type": "39",
"name": "PM25 & PM10 & CO2",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh41",
"type": "22",
"name": "PM2.5 CH1",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh41",
"type": "23",
"name": "PM2.5 CH2",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh41",
"type": "24",
"name": "PM2.5 CH3",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh41",
"type": "25",
"name": "PM2.5 CH4",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh55",
"type": "27",
"name": "Leak CH1",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh55",
"type": "28",
"name": "Leak CH2",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh55",
"type": "29",
"name": "Leak CH3",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh55",
"type": "30",
"name": "Leak CH4",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh31",
"type": "6",
"name": "Temp & Humidity CH1",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh31",
"type": "7",
"name": "Temp & Humidity CH2",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh31",
"type": "8",
"name": "Temp & Humidity CH3",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh31",
"type": "9",
"name": "Temp & Humidity CH4",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh31",
"type": "10",
"name": "Temp & Humidity CH5",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh31",
"type": "11",
"name": "Temp & Humidity CH6",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh31",
"type": "12",
"name": "Temp & Humidity CH7",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh31",
"type": "13",
"name": "Temp & Humidity CH8",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh54",
"type": "66",
"name": "Lds CH1",
"id": "2B77",
"batt": "5",
"rssi": "-62",
"signal": "4",
"idst": "1"
},{
"img": "wh54",
"type": "67",
"name": "Lds CH2",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh54",
"type": "68",
"name": "Lds CH3",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh54",
"type": "69",
"name": "Lds CH4",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
},{
"img": "wh51",
"type": "14",
"name": "Soil moisture CH1",
"id": "FFFFFFFF",
"batt": "9",
"rssi": "--",
"signal": "--",
"idst": "1"
}]With this information we know that only one lds devices is currently configured and we can merge this data with readings. So instead of using channel 1 we could use the device id, which should be unique
id = FFFFFFFF means not setup/configured and therefore should be skipped
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely restructured the API to keep data grouped as requested in commit 1acba58. The API now:
- Maintains original grouped structure (wh25, common_list, ch_lds, etc.) instead of flattening
- Parses values and units separately using
_parse_value_and_unit()method - Uses actual device IDs from sensor configuration instead of generic channel numbers
- Filters out unconfigured devices (ID "FFFFFFFF") automatically
- Maps LDS sensor channels to real device IDs from
/get_sensors_info
Example: LDS sensor now uses device ID "2B77" instead of "CH1", and includes both the live reading data and sensor configuration info.
Co-authored-by: edenhaus <[email protected]>
All requested changes implemented in commit 1acba58:
The API now provides the exact structure and unit handling requested for optimal Home Assistant integration. |
This PR migrates and modernizes the API from PPw096/wittiot, adding a comprehensive async HTTP client with structured data models that preserve the native response format optimized for Home Assistant integration.
What's Changed
New API Client
Data Models
The new API returns data structured exactly as the gateway provides, with enhanced parsing:
Key Features
Unit System
Comprehensive enum-based unit system for different measurement types:
Data Structure Groups
Data organized exactly as the gateway provides:
Migration from PPw096/wittiot
This modernizes the original API while preserving its natural structure:
Home Assistant Integration Benefits
Breaking Changes
This is a new API client that supplements the existing EcoWitt protocol listener. The original
EcoWittListenerremains unchanged for backward compatibility.Testing
This implementation provides the exact structure needed for seamless Home Assistant integration with properly parsed units, device identification, and comprehensive sensor support that respects the gateway's native data organization.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.