I have created a sensor for HACS to read temp, wind, rain etc from MobileAlerts sensors - to do this it needs to read & scrape a HTML page which has all of the sensors. I have created a SensorPage class to read the page and cache data for all sensors, then there is one object for each sensor to hold the relevant info - this object is passed the ‘SensorPage’ object at startup which it holds to read its data.
The SensorPage object uses aiohttp session.post() to get the data however the page can take up to 10s to return.
In the meantime all of the other sensors report data unavailable as there is no data yet read for the page.
What is the recommended solution to flag to each sensor that the data is available so that they can update their state (register a callback), or should the sensor wait for another call to state (or available) and to then try and get the data if it doesn’t yet have it? Is the call to available made first to see if state should be called? I can’t find a great deal of documentation on what gets called & when.
Thanks
If you are creating custom web page that you are scraping, then maybe the simplest way is to make it return data in JSON format.
That would allow you to use JSON parser to get data.
Then in JSON structure, return only valid values. If there is no reliable data for specific sensor then simply do not include that sensor data in JSON.
When parsing JOSN, check for each sensor if its data exist in JSON and act accordingly.
It’s not my page so I cannot change it, therefore rest of response it not relevant… - I have a number of sensors so don’t want to scrape the page each time for each sensor - my question was how to deal with async updating across multiple sensors.
For any others I hope this helps - the DataUpdateCoordinator / CoordinatorEntity provides this,
see Fetching Data | Home Assistant Developer Docs
problem was not knowing what to search on!