-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
Add config flow to Smarty #127540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add config flow to Smarty #127540
Changes from 2 commits
0146cc4
ae7de4a
e781f95
38f4e75
a87a89b
7b0226d
58a05e8
a2d8a7f
51f39d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Config flow for Smarty integration.""" | ||
|
||
from typing import Any | ||
|
||
from pysmarty2 import Smarty | ||
import voluptuous as vol | ||
|
||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult | ||
from homeassistant.const import CONF_HOST, CONF_NAME | ||
|
||
from .const import DOMAIN | ||
|
||
|
||
class SmartyConfigFlow(ConfigFlow, domain=DOMAIN): | ||
"""Smarty config flow.""" | ||
|
||
def _test_connection(self, host: str) -> str | None: | ||
"""Test the connection to the Smarty API.""" | ||
smarty = Smarty(host=host) | ||
try: | ||
if smarty.update(): | ||
return None | ||
except Exception: # noqa: BLE001 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there nothing more narrow to catch? |
||
return "unknown" | ||
else: | ||
return "cannot_connect" | ||
|
||
async def async_step_user( | ||
self, user_input: dict[str, Any] | None = None | ||
) -> ConfigFlowResult: | ||
"""Handle a flow initialized by the user.""" | ||
errors: dict[str, str] = {} | ||
|
||
if user_input is not None: | ||
self._async_abort_entries_match(user_input) | ||
error = await self.hass.async_add_executor_job( | ||
self._test_connection, user_input[CONF_HOST] | ||
) | ||
if not error: | ||
return self.async_create_entry( | ||
title=user_input[CONF_HOST], data=user_input | ||
) | ||
Comment on lines
+40
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the API not provide any source for a unique ID? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, it's just a modbus device There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And? Is there no register which exposes something like a serial number? |
||
errors["base"] = error | ||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=vol.Schema({vol.Required(CONF_HOST): str}), | ||
errors=errors, | ||
) | ||
|
||
async def async_step_import( | ||
self, import_config: dict[str, Any] | ||
) -> ConfigFlowResult: | ||
"""Handle a flow initialized by import.""" | ||
self._async_abort_entries_match({CONF_HOST: import_config[CONF_HOST]}) | ||
joostlek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
error = await self.hass.async_add_executor_job( | ||
self._test_connection, import_config[CONF_HOST] | ||
) | ||
if not error: | ||
return self.async_create_entry( | ||
title=import_config[CONF_NAME], | ||
data={CONF_HOST: import_config[CONF_HOST]}, | ||
) | ||
return self.async_abort(reason=error) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Constants for the Smarty component.""" | ||
|
||
DOMAIN = "smarty" | ||
|
||
SIGNAL_UPDATE_SMARTY = "smarty_update" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"data": { | ||
"host": "[%key:common::config_flow::data::host%]" | ||
}, | ||
"data_description": { | ||
"host": "The hostname or IP address of the Smarty device" | ||
} | ||
} | ||
}, | ||
"error": { | ||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", | ||
"unknown": "[%key:common::config_flow::error::unknown%]" | ||
}, | ||
"abort": { | ||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]", | ||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", | ||
"unknown": "[%key:common::config_flow::error::unknown%]" | ||
} | ||
}, | ||
"issues": { | ||
"deprecated_yaml_import_issue_unknown": { | ||
"title": "YAML import failed with unknown error", | ||
"description": "Configuring {integration_title} using YAML is being removed but there was an unknown error while importing your existing configuration.\nSetup will not proceed.\n\nVerify that your {integration_title} is operating correctly and restart Home Assistant to attempt the import again.\n\nAlternatively, you may remove the `{domain}` configuration from your configuration.yaml entirely, restart Home Assistant, and add the {integration_title} integration manually." | ||
emontnemery marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"deprecated_yaml_import_issue_auth_error": { | ||
"title": "YAML import failed due to an authentication error", | ||
"description": "Configuring {integration_title} using YAML is being removed but there was an authentication error while importing your existing configuration.\nSetup will not proceed.\n\nVerify that your {integration_title} is operating correctly and restart Home Assistant to attempt the import again.\n\nAlternatively, you may remove the `{domain}` configuration from your configuration.yaml entirely, restart Home Assistant, and add the {integration_title} integration manually." | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -539,6 +539,7 @@ | |
"smart_meter_texas", | ||
"smartthings", | ||
"smarttub", | ||
"smarty", | ||
"smhi", | ||
"smlight", | ||
"sms", | ||
|
Uh oh!
There was an error while loading. Please reload this page.