Berry 'tasmota.int(v, min, max)' function (#22721)

This commit is contained in:
s-hadinger 2024-12-27 11:33:52 +01:00 committed by GitHub
parent 4d3a780eb1
commit b729886a16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 1943 additions and 1838 deletions

View File

@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
- HASPmota support for `tabview` (#22707)
- Berry bit-shift operators to `int64` (#22709)
- Berry add unicode encoding to string parsing (#22713)
- Berry `tasmota.int(v, min, max)` function
### Breaking Changed

View File

@ -147,6 +147,8 @@ class be_class_tasmota (scope: global, name: Tasmota) {
web_send_decimal, func(l_webSendDecimal)
webcolor, static_func(l_webcolor)
int, static_closure(class_Tasmota_int_closure)
get_power, func(l_getpower)
set_power, func(l_setpower)
get_switch, func(l_getswitch) // deprecated

View File

@ -2,7 +2,6 @@
#- Do not use it -#
class Trigger end # for compilation
class Rule_Matche end # for compilation
tasmota = nil
#@ solidify:Tasmota
@ -717,6 +716,46 @@ class Tasmota
return ret
end
# tasmota.int(v, min, max)
# ensures that v is int, and always between min and max
# if min>max returns min
# if v==nil returns min
static def int(v, min, max)
if (min == nil || max == nil) return int(v) end
min = int(min)
max = int(max)
if (min > max) return min end
if (v == nil) return min end
v = int(v) # v is int (not nil)
if (v < min) return min end
if (v > max) return max end
return v
end
#-
# Unit tests
# behave like normal int
assert(tasmota.int(4) == 4)
assert(tasmota.int(nil) == nil)
assert(tasmota.int(-3) == -3)
assert(tasmota.int(4.5) == 4)
assert(tasmota.int(true) == 1)
assert(tasmota.int(false) == 0)
# normal behavior
assert(tasmota.int(4, 0, 10) == 4)
assert(tasmota.int(0, 0, 10) == 0)
assert(tasmota.int(10, 0, 10) == 10)
assert(tasmota.int(10, 0, 0) == 0)
assert(tasmota.int(10, 10, 10) == 10)
assert(tasmota.int(-4, 0, 10) == 0)
assert(tasmota.int(nil, 0, 10) == 0)
# abnormal
assert(tasmota.int(4, 10, -10) == 10)
-#
# set_light and get_light deprecetaion
def get_light(l)
print('tasmota.get_light() is deprecated, use light.get()')