Files
buttonplus-esphome-generator/src/buttonplus_generator/utils.py
T
2026-05-16 13:58:52 +02:00

77 lines
2.0 KiB
Python

import re
from typing import List
from buttonplus_generator.constants import *
def slugify(input : str) -> str:
s = input.lower()
s = re.sub(r'\s+', '_', s)
s = re.sub(r'[^a-z0-9_]+', '', s)
s = re.sub(r'_+', '_', s)
s = s.strip('_')
if not s:
s = 'buttonplus'
while len(s) > 1 and s[0].isdigit():
s = s[1:]
return s
def array_to_cpp_vector(string_array : List[str]) -> str:
return "{" + ', '.join(f'"{entry}"' for entry in string_array) + "}"
def get_display_id(num_bars : int) -> str:
if(num_bars < 1 or num_bars > 3):
raise ValueError("Can't have a display with this number of bars")
return "base_J" + str(3 - num_bars)
def get_display_address(num_bars : int) -> str:
if(num_bars < 1 or num_bars > 3):
raise ValueError("Can't have a display with this number of bars")
return 0x23 - num_bars
def string_to_click_type(ct:str) -> ClickType:
if ct == "single":
return ClickType.SINGLE
elif ct == "double":
return ClickType.DOUBLE
elif ct == "triple":
return ClickType.TRIPLE
elif ct == "hold":
return ClickType.HOLD
def list_to_click_type(str_list:List[str]) -> List[ClickType]:
ct_list: List[ClickType] = []
for ct in str_list:
ct_list.append(string_to_click_type(ct))
return ct_list
def click_type_color(ct: ClickType) -> Color:
if(ct is ClickType.SINGLE):
return Color("#00FF00")
elif(ct is ClickType.DOUBLE):
return Color("#0000FF")
elif(ct is ClickType.TRIPLE):
return Color("#FF0000")
elif(ct is ClickType.HOLD):
return Color("#FF00FF")
return Color("#FFFFFF")
def click_type_timing(ct: ClickType) -> List[str]:
if(ct is ClickType.SINGLE):
return TIMING_SINGLE_CLICK
elif(ct is ClickType.DOUBLE):
return TIMING_DOUBLE_CLICK
elif(ct is ClickType.TRIPLE):
return TIMING_TRIPLE_CLICK
elif(ct is ClickType.HOLD):
return TIMING_HOLD_CLICK
return TIMING_SINGLE_CLICK