Skip to content

Commit

Permalink
connected usb ports auto generated
Browse files Browse the repository at this point in the history
  • Loading branch information
flip555 committed Feb 24, 2024
1 parent 52d5e24 commit 005c3f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
21 changes: 14 additions & 7 deletions custom_components/bms_connector/config_flows/bms/seplos.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
from homeassistant import config_entries
import voluptuous as vol
from ...const import DOMAIN
from serial.tools import list_ports

class SeplosConfigFlowMethods:
async def async_step_seplos_bms_v2_device(self, user_input=None):
# Query available USB ports
ports = [port.device for port in list_ports.comports()]
default_port = ports[0] if ports else "/dev/ttyUSB0" # Fallback default

if user_input is not None:
# Store user input and create the configuration entry
self.user_input.update(user_input)
#self.user_input["name_prefix"] = f"Seplos BMS V2 - {self.user_input['usb_port']} - "
self.user_input["sensor_update_frequency"] = 5
title = f"Seplos BMS V2 - {self.user_input['usb_port']}"
title = f"{self.user_input['name_prefix']} {self.user_input['usb_port']}"
return self.async_create_entry(title=title, data=self.user_input)

# Include dynamic listing of USB ports in the schema
data_schema = vol.Schema({
vol.Required("usb_port", default="/dev/ttyUSB0"): str,
vol.Required("name_prefix", default="Seplos BMS HA - "): str,
vol.Required("battery_pack_count", default=1): vol.All(vol.Coerce(int), vol.Range(min=1, max=16)),
vol.Required("usb_port", default=default_port): vol.In(ports),
vol.Required("name_prefix", default="Seplos BMS HA - "): str,
vol.Required("battery_pack_count", default=1): vol.All(vol.Coerce(int), vol.Range(min=1, max=16)),
})

return self.async_show_form(
Expand All @@ -34,14 +39,16 @@ async def async_step_seplos_options_bms_v2(self, user_input=None):

return self.async_create_entry(title="", data=user_input)
battery_pack_count = self.config_entry.data.get("battery_pack_count", 1)
usb_port = self.config_entry.data.get("usb_port", "")
#usb_port = self.config_entry.data.get("usb_port", "")
ports = [port.device for port in list_ports.comports()]
usb_port = self.config_entry.data.get("usb_port", ports[0] if ports else "/dev/ttyUSB0") # Fallback or current setting
name_prefix = self.config_entry.data.get("name_prefix", f"Seplos BMS HA - ")
sensor_update_frequency = self.config_entry.data.get("sensor_update_frequency", 5)

return self.async_show_form(
step_id="seplos_options_bms_v2",
data_schema=vol.Schema({
vol.Required("usb_port", default=usb_port): str,
vol.Required("usb_port", default=usb_port): vol.In(ports),
vol.Required("name_prefix", default=name_prefix): str,
vol.Required("battery_pack_count", default=battery_pack_count): vol.All(vol.Coerce(int), vol.Range(min=1, max=16)),
vol.Required("sensor_update_frequency", default=sensor_update_frequency): int,
Expand Down
5 changes: 3 additions & 2 deletions custom_components/bms_connector/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
"description": "Please select your region"
},
"seplos_bms_v2_device": {
"description": "Please enter the connection port and select the number of battery packs are attached to your BMS. For example, use '/dev/ttyUSB0' for USB serial connections and select 1 for a single battery.",
"description": "Please select the connection port and select the number of battery packs are attached to your BMS.",
"data": {
"usb_port": "USB Serial Port",
"battery_address": "Battery Address",
"name_prefix": "Please enter your desired sensor prefix",
"battery_pack_count": "Number of Battery Packs",
"sensor_update_frequency": "Update Frequency (s)"
}
Expand Down Expand Up @@ -78,7 +79,7 @@
"data": {
"usb_port": "USB Serial Port",
"battery_address": "Battery Address",
"name_prefix": "Entity Name Prefix",
"name_prefix": "Entity Name Prefix **CHANGING THIS WILL RENAME ENTITIES**",
"battery_pack_count": "Number of Battery Packs",
"sensor_update_frequency": "Update Frequency (s)"
}
Expand Down

0 comments on commit 005c3f3

Please sign in to comment.