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 ),
0 commit comments