Hi!,
I’m building a custom integration based on the examples given in:
The integration should take a message from the given mqtt topic, parse the JSON to get the key-value pairs, and write ‘value’ in the state of the entity which is named ‘key’. The mqtt sensor is an ESP based hub I made that collects temperature data from many sensors, around 30 per device - consequently I don’t want to define each sensor in the config.yaml file, rather just define my custom sensor integration that will create the entities as needed.
So far, I just started but ran into some problems. ( Admittedly I’m in way over my head, very first custom integration, so go easy on me )
What is the proper way of creating an entity from inside a custom integration?
hass.states.set(
'sens_name', value,
{
'state_class': 'measurement',
'unit_of_measurement': '°C',
'device_class': 'temperature',
'friendly_name': 'friendly name',
'icon': 'mdi:thermometer',
})
I’m currently using this, but have a slight feeling that this is the thing that causes my HA core to crash sometimes with the following error: ( the error changes actually, but it’s always something involving async- await functions) - the exception is thrown when I try to load the HA webpage, but not every time, sometimes it works, other times the webpage hangs on the splash screen/ loading data screen
2022-12-07 23:59:27.735 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [2722591216] Error handling message: Unknown error (unknown_error) from 192.168.1.108 (Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:107.0) Gecko/20100101 Firefox/107.0)
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 27, in _handle_async_response
await func(hass, connection, msg)
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 356, in handle_get_services
descriptions = await async_get_all_descriptions(hass)
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 491, in async_get_all_descriptions
domain_yaml = loaded[domain]
KeyError: 'humidifier'
I modified the mqtt_basic_sync example, which seem to be the only working mqtt example I could find. Is my problem related to my integration not running asynchronously?
Can you recommend me some example custom integrations using mqtt, or one that creates entities based on sensor tags or something that explains how the yaml parameters work ( checked the dev docs but still not sure, maybe that’s another question ).
How should I go about creating entities from the integration? maybe even dinamically when the mqtt msg is read, not in the constructor.