-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add error handling and connection closing
- Loading branch information
Aki
committed
Jul 29, 2024
1 parent
38855bf
commit 96f162b
Showing
4 changed files
with
156 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,37 @@ | ||
import asyncio | ||
import logging | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
|
||
from .const import DOMAIN | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
async def async_setup(hass: HomeAssistant, config: dict): | ||
"""Set up the Emonio component.""" | ||
return True | ||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
"""Set up Emonio from a config entry.""" | ||
hass.data.setdefault(DOMAIN, {}) | ||
hass.data[DOMAIN][entry.entry_id] = { | ||
"client": None, # Placeholder for the Modbus client | ||
"entities": [], # Placeholder for the entities | ||
} | ||
await hass.config_entries.async_forward_entry_setups(entry, ["sensor"]) | ||
|
||
return True | ||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
"""Unload a config entry.""" | ||
await hass.config_entries.async_unload_platforms(entry, ["sensor"]) | ||
hass.data[DOMAIN].pop(entry.entry_id) | ||
if entry.entry_id in hass.data[DOMAIN]: | ||
# Close Modbus client connections for all entities | ||
for entity in hass.data[DOMAIN][entry.entry_id]["entities"]: | ||
entity.close_connection() | ||
|
||
return True | ||
# Remove the entry from hass.data | ||
hass.data[DOMAIN].pop(entry.entry_id) | ||
|
||
await hass.config_entries.async_unload_platforms(entry, ["sensor"]) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,5 @@ | |
"requirements": [ | ||
"pymodbus==2.5.3" | ||
], | ||
"version": "0.1.4" | ||
"version": "0.1.5" | ||
} |
Oops, something went wrong.