forked from home-assistant/core
-
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.
Update DSMR integration to import yaml to ConfigEntry (home-assistant…
…#39473) * Rewrite to import from platform setup * Add config flow for import * Implement reload * Update sensor tests * Add config flow tests * Remove some code * Fix pylint issue * Remove update options code * Add platform import test * Remove infinite while loop * Move async_setup_platform * Check for unload_ok * Remove commented out test code * Implement function to check on host/port already existing Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Implement new method in import * Update tests * Fix test setup platform * Add string * Patch setup_platform * Add block till done to patch block Co-authored-by: Chris Talkington <chris@talkingtontech.com>
- Loading branch information
1 parent
77f5fb7
commit d0120d5
Showing
9 changed files
with
498 additions
and
68 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
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 +1,54 @@ | ||
"""The dsmr component.""" | ||
import asyncio | ||
from asyncio import CancelledError | ||
import logging | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
|
||
from .const import DATA_TASK, DOMAIN, PLATFORMS | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup(hass, config: dict): | ||
"""Set up the DSMR platform.""" | ||
return True | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
"""Set up DSMR from a config entry.""" | ||
hass.data.setdefault(DOMAIN, {}) | ||
hass.data[DOMAIN][entry.entry_id] = {} | ||
|
||
for platform in PLATFORMS: | ||
hass.async_create_task( | ||
hass.config_entries.async_forward_entry_setup(entry, platform) | ||
) | ||
|
||
return True | ||
|
||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
"""Unload a config entry.""" | ||
task = hass.data[DOMAIN][entry.entry_id][DATA_TASK] | ||
|
||
# Cancel the reconnect task | ||
task.cancel() | ||
try: | ||
await task | ||
except CancelledError: | ||
pass | ||
|
||
unload_ok = all( | ||
await asyncio.gather( | ||
*[ | ||
hass.config_entries.async_forward_entry_unload(entry, component) | ||
for component in PLATFORMS | ||
] | ||
) | ||
) | ||
if unload_ok: | ||
hass.data[DOMAIN].pop(entry.entry_id) | ||
|
||
return unload_ok |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""Config flow for DSMR integration.""" | ||
import logging | ||
from typing import Any, Dict, Optional | ||
|
||
from homeassistant import config_entries | ||
from homeassistant.const import CONF_HOST, CONF_PORT | ||
|
||
from .const import DOMAIN # pylint:disable=unused-import | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class DSMRFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): | ||
"""Handle a config flow for DSMR.""" | ||
|
||
VERSION = 1 | ||
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH | ||
|
||
def _abort_if_host_port_configured( | ||
self, | ||
port: str, | ||
host: str = None, | ||
updates: Optional[Dict[Any, Any]] = None, | ||
reload_on_update: bool = True, | ||
): | ||
"""Test if host and port are already configured.""" | ||
for entry in self.hass.config_entries.async_entries(DOMAIN): | ||
if entry.data.get(CONF_HOST) == host and entry.data[CONF_PORT] == port: | ||
if updates is not None: | ||
changed = self.hass.config_entries.async_update_entry( | ||
entry, data={**entry.data, **updates} | ||
) | ||
if ( | ||
changed | ||
and reload_on_update | ||
and entry.state | ||
in ( | ||
config_entries.ENTRY_STATE_LOADED, | ||
config_entries.ENTRY_STATE_SETUP_RETRY, | ||
) | ||
): | ||
self.hass.async_create_task( | ||
self.hass.config_entries.async_reload(entry.entry_id) | ||
) | ||
return self.async_abort(reason="already_configured") | ||
|
||
async def async_step_import(self, import_config=None): | ||
"""Handle the initial step.""" | ||
host = import_config.get(CONF_HOST) | ||
port = import_config[CONF_PORT] | ||
|
||
status = self._abort_if_host_port_configured(port, host, import_config) | ||
if status is not None: | ||
return status | ||
|
||
if host is not None: | ||
name = f"{host}:{port}" | ||
else: | ||
name = port | ||
|
||
return self.async_create_entry(title=name, data=import_config) |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"""Constants for the DSMR integration.""" | ||
|
||
DOMAIN = "dsmr" | ||
|
||
PLATFORMS = ["sensor"] | ||
|
||
CONF_DSMR_VERSION = "dsmr_version" | ||
CONF_RECONNECT_INTERVAL = "reconnect_interval" | ||
CONF_PRECISION = "precision" | ||
|
||
DEFAULT_DSMR_VERSION = "2.2" | ||
DEFAULT_PORT = "/dev/ttyUSB0" | ||
DEFAULT_PRECISION = 3 | ||
DEFAULT_RECONNECT_INTERVAL = 30 | ||
|
||
DATA_TASK = "task" | ||
|
||
ICON_GAS = "mdi:fire" | ||
ICON_POWER = "mdi:flash" | ||
ICON_POWER_FAILURE = "mdi:flash-off" | ||
ICON_SWELL_SAG = "mdi:pulse" |
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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"config": { | ||
"step": {}, | ||
"error": {}, | ||
"abort": { | ||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]" | ||
} | ||
} | ||
} |
Oops, something went wrong.