Here is the background:
I am extending a custom component from HACS. It includes 2 sensors which pull in data from 2 cloud sources; both have details about the expected precipitation but are ‘unparsed’ and used for a frontend card.
The extension i’ve made combines both sources. From my combined data, i want to expose 4 new sensors.
In my ‘combination’ logic sensor, i’ve subscribed to state changes;
async_track_state_change(hass, [ "sensor.neerslag_buienalarm_regen_data", "sensor.neerslag_buienradar_regen_data" ], self.parse_rain_source_data)
This means that whenever either of the sources update, my method self.parse_rain_source_data
executes.
In the meantime, i have also registered my new sensors, one is called “sensor.is_it_raining” which I want to update from my callback method;
@callback
def parse_rain_source_data(self, entity_id, old_state, new_state):
However, whenever i try to call from within that callback
self.hass.states.set("sensor.is_it_raining", "yes")
Hass bails with an error:
File "/config/custom_components/neerslag/sensor.py", line 421, in update_sensor_state_and_attributes
self.hass.states.set(entity_id, new_state, inputAttributesObject, True)
File "/usr/src/homeassistant/homeassistant/core.py", line 1286, in set
run_callback_threadsafe(
File "/usr/src/homeassistant/homeassistant/util/async_.py", line 51, in run_callback_threadsafe
raise RuntimeError("Cannot be called from within the event loop")
RuntimeError: Cannot be called from within the event loop
And i have no clue how to solve this Any pointers so that i can update the sensor states in this callback?