h4 / lywsd02

MIT License
169 stars 34 forks source link

History Data for last n records #28

Closed 53RT closed 3 years ago

53RT commented 3 years ago

Hi,

I was curious what the other characteristics mean and was partially lucky. Mainly the issue from MiTemperature2 helped me to understand how the Mi Home App reads the daily statistics so fast. I created a bluetooth dump and noticed, that an index is set to total_records - 23 to fetch the 24 most recent history_data which are then displayed.

The function relies on two things. The total number of records the device has recorded (earlier records might be already deleted as it seems that the device can store about 4650 records) and the current number of records stored. Those information can be read with client.num_stored_entries. With the client.history_index property an index in the range [total_records - current_records + 1, total_records] can be set to retrieve a partial amount of the recorded entries.

I added an example in the Readme that shows how to read the ten most recent entries. Note if the client object already exists and client.history_data was already called, setting an index and calling it again will update the ordered_dict that holds the history_data with the most recent entries so it might be, that the data returned is longer than expected as it includes entries from previous calls.

I thought this feature could be interesting so that you don't have to fetch all records. It could be also included as a function to fetch the last n entries.