Skip to content

Commit

Permalink
Update config flow with extra step so user can customize broker endpo…
Browse files Browse the repository at this point in the history
…int.
  • Loading branch information
koenhendriks committed Jan 2, 2024
1 parent f04c4e9 commit dd4b894
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
28 changes: 23 additions & 5 deletions custom_components/button_plus/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -42,22 +44,39 @@ 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,
"mqtt_user": broker_username
}
)

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 = {}
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion custom_components/button_plus/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}
Expand Down

0 comments on commit dd4b894

Please sign in to comment.