rgruebel / ha_zigbee2mqtt_networkmap

Custom Component for Homeassistant to show zigbee2mqtt Networkmap
169 stars 43 forks source link

FR: Don't do file I/O in async functions #33

Closed ludeeus closed 2 years ago

ludeeus commented 4 years ago

Instead of copying the files (which does I/O in the async loop), you should set up a custom view that can be used for the panel, see the custom_panel integration on how that can be done (or HACS which implements something similar (https://github.com/hacs/integration/blob/main/custom_components/hacs/operational/setup_actions/frontend.py))

https://github.com/rgruebel/ha_zigbee2mqtt_networkmap/blob/7a8eb0b35683052d18292807b1e277cdd1cf89ab/custom_components/zigbee2mqtt_networkmap/__init__.py#L25

There are also other file writes that should be moved out of the loop and be run with the executor instead (if they are needed)

https://github.com/rgruebel/ha_zigbee2mqtt_networkmap/blob/7a8eb0b35683052d18292807b1e277cdd1cf89ab/custom_components/zigbee2mqtt_networkmap/__init__.py#L53-L56

EDIT: Missed one, this one does I/O on every update it gets, which will halt everything in Home Assistant while it does these operations

https://github.com/rgruebel/ha_zigbee2mqtt_networkmap/blob/7a8eb0b35683052d18292807b1e277cdd1cf89ab/custom_components/zigbee2mqtt_networkmap/__init__.py#L66-L70

working with async: https://developers.home-assistant.io/docs/asyncio_working_with_async

ludeeus commented 4 years ago

Another example of serving the file https://github.com/thomasloven/hass-fontawesome/blob/master/custom_components/fontawesome/custom_component_server.py