Skip to content

Commit e6b1f04

Browse files
authored
Allow to manually introduce a serial port (#3)
1 parent 132d8d4 commit e6b1f04

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

custom_components/pylontech_serial/config_flow.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ async def async_step_user(self, user_input=None, usb_device=None):
2727
errors = {}
2828

2929
if user_input is not None:
30+
if user_input[CONF_SERIAL_PORT] == "Enter Manually":
31+
self.user_input = user_input
32+
return await self.async_step_manual_path()
3033
return self.async_create_entry(title="Pylontech Battery", data=user_input)
3134

3235
ports = await self.hass.async_add_executor_job(serial.tools.list_ports.comports)
@@ -36,7 +39,8 @@ async def async_step_user(self, user_input=None, usb_device=None):
3639

3740
if usb_device and usb_device not in list_of_ports:
3841
list_of_ports[usb_device] = usb_device
39-
42+
43+
list_of_ports["Enter Manually"] = "Enter Manually"
4044
default_port = usb_device if usb_device else vol.UNDEFINED
4145

4246
schema = vol.Schema({
@@ -50,6 +54,19 @@ async def async_step_user(self, user_input=None, usb_device=None):
5054
step_id="user", data_schema=schema, errors=errors
5155
)
5256

57+
async def async_step_manual_path(self, user_input=None):
58+
"""Handle manual serial port entry."""
59+
if user_input is not None:
60+
self.user_input[CONF_SERIAL_PORT] = user_input[CONF_SERIAL_PORT]
61+
return self.async_create_entry(title="Pylontech Battery", data=self.user_input)
62+
63+
return self.async_show_form(
64+
step_id="manual_path",
65+
data_schema=vol.Schema({
66+
vol.Required(CONF_SERIAL_PORT): str
67+
})
68+
)
69+
5370
@staticmethod
5471
@callback
5572
def async_get_options_flow(config_entry):
@@ -72,7 +89,10 @@ async def async_step_user(self, user_input=None):
7289
current_cap = self.config_entry.options.get(CONF_BATTERY_CAPACITY, self.config_entry.data.get(CONF_BATTERY_CAPACITY, DEFAULT_BATTERY_CAPACITY))
7390

7491
if user_input is not None:
75-
return self.async_create_entry(title="", data=user_input)
92+
if user_input[CONF_SERIAL_PORT] == "Enter Manually":
93+
self.user_input = user_input
94+
return await self.async_step_manual_path()
95+
return self.async_create_entry(title="", data=user_input)
7696

7797
ports = await self.hass.async_add_executor_job(serial.tools.list_ports.comports)
7898
list_of_ports = {}
@@ -83,6 +103,8 @@ async def async_step_user(self, user_input=None):
83103
if current_port is not None and current_port not in list_of_ports:
84104
list_of_ports[current_port] = current_port
85105

106+
list_of_ports["Enter Manually"] = "Enter Manually"
107+
86108
default_port = current_port if current_port is not None else vol.UNDEFINED
87109

88110
schema = vol.Schema({
@@ -93,3 +115,16 @@ async def async_step_user(self, user_input=None):
93115
})
94116

95117
return self.async_show_form(step_id="user", data_schema=schema, errors=errors)
118+
119+
async def async_step_manual_path(self, user_input=None):
120+
"""Handle manual serial port entry."""
121+
if user_input is not None:
122+
self.user_input[CONF_SERIAL_PORT] = user_input[CONF_SERIAL_PORT]
123+
return self.async_create_entry(title="", data=self.user_input)
124+
125+
return self.async_show_form(
126+
step_id="manual_path",
127+
data_schema=vol.Schema({
128+
vol.Required(CONF_SERIAL_PORT, default=self.user_input.get(CONF_SERIAL_PORT, "")): str
129+
})
130+
)

custom_components/pylontech_serial/translations/ca.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
"unknown": "Error inesperat"
99
},
1010
"step": {
11+
"manual_path": {
12+
"description": "Introdueix la ruta al port sèrie manualment.",
13+
"title": "Port Sèrie Manual",
14+
"data": {
15+
"serial_port": "Port Sèrie (ex. /dev/ttyUSB0)"
16+
}
17+
},
1118
"user": {
1219
"data": {
1320
"baud_rate": "Taxa de Baud",
@@ -26,6 +33,13 @@
2633
},
2734
"options": {
2835
"step": {
36+
"manual_path": {
37+
"description": "Introdueix la ruta al port sèrie manualment.",
38+
"title": "Port Sèrie Manual",
39+
"data": {
40+
"serial_port": "Port Sèrie (ex. /dev/ttyUSB0)"
41+
}
42+
},
2943
"user": {
3044
"data": {
3145
"baud_rate": "Taxa de Baud",

custom_components/pylontech_serial/translations/en.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
"unknown": "Unexpected error"
99
},
1010
"step": {
11+
"manual_path": {
12+
"description": "Enter the path to your serial port manually.",
13+
"title": "Manual Serial Port",
14+
"data": {
15+
"serial_port": "Serial Port (e.g., /dev/ttyUSB0)"
16+
}
17+
},
1118
"user": {
1219
"data": {
1320
"baud_rate": "Baud Rate",
@@ -26,6 +33,13 @@
2633
},
2734
"options": {
2835
"step": {
36+
"manual_path": {
37+
"description": "Enter the path to your serial port manually.",
38+
"title": "Manual Serial Port",
39+
"data": {
40+
"serial_port": "Serial Port (e.g., /dev/ttyUSB0)"
41+
}
42+
},
2943
"user": {
3044
"data": {
3145
"baud_rate": "Baud Rate",

0 commit comments

Comments
 (0)