Posting stock-unit data to a flow sensor at a coarser resolution records values several times too high
When I post sensor data in a stock unit (like kWh or m³) to a sensor with a flow unit (like kW or m³/h), and the posted values have a coarser resolution than the sensor, the stored values are too high by the upsampling factor.
To reproduce
A sensor with unit m³/h and an event resolution of 10 minutes (like the "some gas sensor" in the API test fixtures). Post one value over one hour:
POST /api/v3_0/sensors/<id>/data
{
"type": "PostSensorDataRequest",
"values": -11.28,
"start": "2021-06-07T00:00:00+02:00",
"duration": "PT1H",
"horizon": "PT0H",
"unit": "m³"
}
Expected: six beliefs of -11.28 m³/h (11.28 m³ over one hour is 11.28 m³/h).
Actual: six beliefs of -67.68 m³/h, six times too high. Posting the same hour as three 20-minute values of -11.28 m³ gives the same -67.68 m³/h, where -33.84 m³/h is expected. Posting six 10-minute values (no upsampling needed) is fine.
The same goes for e.g. 4 kWh over PT1H posted to a kW sensor with a 15-minute resolution, which is stored as 16 kW instead of 4 kW.
Cause
PostSensorDataSchema.post_load_sequence calls possibly_upsample_values before possibly_convert_units. The values are first repeated to the sensor's resolution, and then converted with event_resolution=duration / len(values), which by then is the sensor's (finer) resolution, so each repeated stock value is treated as if it was the amount for that shorter interval.
Converting first (at the resolution the data was posted in) and upsampling afterwards gives the right result. I have a fix with a regression test and can open a PR.