Skip to content

Commit ebef19d

Browse files
committed
* Fixed incorrect types when initializing serial ModBus client
+ Added predefined values for serial ModBus configuration flow
1 parent 8c068d3 commit ebef19d

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

custom_components/systemair/config_flow.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
DOMAIN,
4040
LOGGER,
4141
MODEL_SPECS,
42+
SERIAL_BAUDRATES,
43+
SERIAL_BYTESIZES,
44+
SERIAL_PARITIES,
45+
SERIAL_STOPBITS,
4246
)
4347

4448

@@ -72,12 +76,26 @@ async def _validate_modbus_tcp_connection(self, user_input: dict) -> None:
7276

7377
async def _validate_serial_connection(self, user_input: dict) -> None:
7478
"""Validate the connection to the unit via Modbus Serial."""
79+
baudrate = int(user_input[CONF_BAUDRATE])
80+
81+
bytesize = user_input[CONF_BYTESIZE]
82+
if bytesize in SERIAL_BYTESIZES:
83+
bytesize = SERIAL_BYTESIZES[bytesize]
84+
85+
parity = user_input[CONF_PARITY]
86+
if parity in SERIAL_PARITIES:
87+
parity = SERIAL_PARITIES[parity]
88+
89+
stopbits = user_input[CONF_STOPBITS]
90+
if stopbits in SERIAL_STOPBITS:
91+
stopbits = SERIAL_STOPBITS[stopbits]
92+
7593
client = SystemairSerialClient(
7694
port=user_input[CONF_SERIAL_PORT],
77-
baudrate=user_input[CONF_BAUDRATE],
78-
bytesize=user_input[CONF_BYTESIZE],
79-
parity=user_input[CONF_PARITY],
80-
stopbits=user_input[CONF_STOPBITS],
95+
baudrate=baudrate,
96+
bytesize=bytesize,
97+
parity=parity,
98+
stopbits=stopbits,
8199
slave_id=user_input[CONF_SLAVE_ID],
82100
)
83101
try:
@@ -191,9 +209,17 @@ async def async_step_modbus_serial(self, user_input: dict | None = None) -> conf
191209

192210
user_input[CONF_API_TYPE] = API_TYPE_MODBUS_SERIAL
193211

194-
# Convert string values to integers for bytesize and stopbits
195-
user_input[CONF_BYTESIZE] = int(user_input[CONF_BYTESIZE])
196-
user_input[CONF_STOPBITS] = int(user_input[CONF_STOPBITS])
212+
# Convert display values to serial library constants
213+
user_input[CONF_BAUDRATE] = int(user_input[CONF_BAUDRATE])
214+
215+
if user_input[CONF_BYTESIZE] in SERIAL_BYTESIZES:
216+
user_input[CONF_BYTESIZE] = SERIAL_BYTESIZES[user_input[CONF_BYTESIZE]]
217+
218+
if user_input[CONF_PARITY] in SERIAL_PARITIES:
219+
user_input[CONF_PARITY] = SERIAL_PARITIES[user_input[CONF_PARITY]]
220+
221+
if user_input[CONF_STOPBITS] in SERIAL_STOPBITS:
222+
user_input[CONF_STOPBITS] = SERIAL_STOPBITS[user_input[CONF_STOPBITS]]
197223

198224
return self.async_create_entry(
199225
title=user_input.get(CONF_MODEL, f"Serial {user_input[CONF_SERIAL_PORT]}"),
@@ -205,26 +231,27 @@ async def async_step_modbus_serial(self, user_input: dict | None = None) -> conf
205231
data_schema=vol.Schema(
206232
{
207233
vol.Required(CONF_SERIAL_PORT, default=DEFAULT_SERIAL_PORT): selector.TextSelector(),
208-
vol.Required(CONF_BAUDRATE, default=DEFAULT_BAUDRATE): selector.NumberSelector(
209-
selector.NumberSelectorConfig(
210-
mode=selector.NumberSelectorMode.BOX,
234+
vol.Required(CONF_BAUDRATE, default=str(DEFAULT_BAUDRATE)): selector.SelectSelector(
235+
selector.SelectSelectorConfig(
236+
options=[str(b) for b in SERIAL_BAUDRATES],
237+
mode=selector.SelectSelectorMode.DROPDOWN,
211238
)
212239
),
213240
vol.Required(CONF_BYTESIZE, default=DEFAULT_BYTESIZE): selector.SelectSelector(
214241
selector.SelectSelectorConfig(
215-
options=["7", "8"],
242+
options=list(SERIAL_BYTESIZES.keys()),
216243
mode=selector.SelectSelectorMode.DROPDOWN,
217244
)
218245
),
219246
vol.Required(CONF_PARITY, default=DEFAULT_PARITY): selector.SelectSelector(
220247
selector.SelectSelectorConfig(
221-
options=["N", "E", "O"],
248+
options=list(SERIAL_PARITIES.keys()),
222249
mode=selector.SelectSelectorMode.DROPDOWN,
223250
)
224251
),
225252
vol.Required(CONF_STOPBITS, default=DEFAULT_STOPBITS): selector.SelectSelector(
226253
selector.SelectSelectorConfig(
227-
options=["1", "2"],
254+
options=list(SERIAL_STOPBITS.keys()),
228255
mode=selector.SelectSelectorMode.DROPDOWN,
229256
)
230257
),

custom_components/systemair/const.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,27 @@
2727
CONF_STOPBITS = "stopbits"
2828

2929
DEFAULT_SERIAL_PORT = "/dev/ttyUSB0"
30-
DEFAULT_BAUDRATE = 19200
31-
DEFAULT_BYTESIZE = 8
32-
DEFAULT_STOPBITS = 1
33-
DEFAULT_PARITY = "N"
30+
DEFAULT_BAUDRATE = 115200
31+
DEFAULT_BYTESIZE = "8 bits"
32+
DEFAULT_STOPBITS = "1"
33+
DEFAULT_PARITY = "None"
34+
35+
# Serial port options
36+
SERIAL_BAUDRATES = [9600, 19200, 38400, 57600, 115200]
37+
SERIAL_BYTESIZES = {
38+
"7 bits": 7,
39+
"8 bits": 8,
40+
}
41+
SERIAL_PARITIES = {
42+
"None": "N",
43+
"Even": "E",
44+
"Odd": "O",
45+
}
46+
SERIAL_STOPBITS = {
47+
"1": 1,
48+
"1.5": 1.5,
49+
"2": 2,
50+
}
3451

3552
# --- Power Specs for different models ---
3653
MODEL_SPECS = {

custom_components/systemair/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
"aiohttp",
1616
"async-timeout"
1717
],
18-
"version": "1.0.8"
18+
"version": "1.0.9"
1919
}

0 commit comments

Comments
 (0)