diff --git a/custom_components/button_plus/config_flow.py b/custom_components/button_plus/config_flow.py index e65d2a0..34c1bf7 100644 --- a/custom_components/button_plus/config_flow.py +++ b/custom_components/button_plus/config_flow.py @@ -9,7 +9,7 @@ import voluptuous as vol from homeassistant import config_entries, exceptions -from homeassistant.const import CONF_IP_ADDRESS, CONF_EMAIL, CONF_PASSWORD +from homeassistant.const import CONF_IP_ADDRESS, CONF_EMAIL, CONF_PASSWORD, CONF_HOST from homeassistant.helpers import aiohttp_client from .button_plus_api.api_client import ApiClient from .button_plus_api.local_api_client import LocalApiClient @@ -26,12 +26,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): def __init__(self): self.mqtt_entry = None + self.broker_endpoint = None VERSION = 1 CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH async def async_step_user(self, user_input=None): """Handle the initial Button+ setup, showing the 2 options and checking the MQTT integration.""" + errors = {} mqtt_entries = self.hass.config_entries.async_entries(domain="mqtt") if len(mqtt_entries) < 1: @@ -42,15 +44,23 @@ async def async_step_user(self, user_input=None): description_placeholders={ "mqtt_integration_link": mqtt_url }) + mqtt_entry = mqtt_entries[0] broker = self.get_mqtt_endpoint(mqtt_entry.data.get("broker")) broker_port = mqtt_entry.data.get("port") broker_username = mqtt_entry.data.get("username", "(No authentication)") self.mqtt_entry = mqtt_entry - return self.async_show_menu( + if user_input is not None: + self.broker_endpoint = user_input.get("broker", broker) + return await self.async_step_choose_entry() + + return self.async_show_form( step_id="user", - menu_options=["fetch_website", "manual"], + data_schema=vol.Schema({ + vol.Required("broker", default=broker): str + }), + errors=errors, description_placeholders={ "mqtt_broker": broker, "mqtt_broker_port": broker_port, @@ -58,6 +68,15 @@ async def async_step_user(self, user_input=None): } ) + async def async_step_choose_entry(self, user_input=None): + errors = {} + # if user_input is not None: + return self.async_show_menu( + step_id="choose_entry", + menu_options=["fetch_website", "manual"], + description_placeholders={} + ) + async def async_step_manual(self, user_input=None): """ Handle setting up button plus from manual IP.""" errors = {} @@ -198,14 +217,13 @@ def validate_ip(self, ip) -> bool: def add_broker_to_config(self, device_config: DeviceConfiguration) -> DeviceConfiguration: mqtt_entry = self.mqtt_entry - broker_endpoint = self.get_mqtt_endpoint(mqtt_entry.data.get("broker")) broker_port = mqtt_entry.data.get("port") broker_username = mqtt_entry.data.get("username", "") broker_password = mqtt_entry.data.get("password", "") broker = MqttBroker( broker_id=f"ha-button-plus", - url=f"mqtt://{broker_endpoint}/", + url=f"mqtt://{self.broker_endpoint}/", port=broker_port, ws_port=9001, username=broker_username, diff --git a/custom_components/button_plus/translations/en.json b/custom_components/button_plus/translations/en.json index a516ee5..dcd39af 100644 --- a/custom_components/button_plus/translations/en.json +++ b/custom_components/button_plus/translations/en.json @@ -30,11 +30,20 @@ "description": "Manually enter Button+ device IP address" }, "user": { + "data": { + "broker": "Address to reach the broker." + }, + "data_description": { + "broker": "Leave this unchanged if you're unsure what this is." + }, + "description": "The MQTT broker configured in Home Assistant will be used:\n - Broker: {mqtt_broker}\n - Port:{mqtt_broker_port}\n - Authentication: {mqtt_user}\n\n This broker configuration will be added to each device with the name of this integration; 'ha-button-plus' and can be found in the Button+ interface.\n\n The buttons of the Button+ will be configured to send clicks on a topic in this broker and the integration will sync these with the home assistant button entities. \n\n You can override the MQTT broker endpoint if you want here." + }, + "choose_entry": { "menu_options": { "fetch_website": "Fetch all devices from Button.plus website", "manual": "Manually enter single device by local IP" }, - "description": "The MQTT broker configured in Home Assistant will be used:\n - Broker: {mqtt_broker}\n - Port:{mqtt_broker_port}\n - Authentication: {mqtt_user}\n\n This broker configuration will be added to each device with the name of this integration; 'ha-button-plus' and can be found in the Button+ interface.\n\n The buttons of the Button+ will be configured to send clicks on a topic in this broker and the integration will sync these with the home assistant button entities. \n\n To continue, pick the desired option to setup your Button+ devices." + "description": "To continue, pick the desired option to setup your Button+ devices." } } }