@@ -57,7 +57,7 @@ impl fmt::Display for Homie5ValueConversionError {
57
57
write ! ( f, "Integer '{}' is out of allowed range: {}" , value, range)
58
58
}
59
59
Homie5ValueConversionError :: FloatOutOfRange ( value, range) => {
60
- write ! ( f, "Flaot '{}' is out of allowed range: {}" , value, range)
60
+ write ! ( f, "Float '{}' is out of allowed range: {}" , value, range)
61
61
}
62
62
Homie5ValueConversionError :: InvalidDateTimeFormat ( value) => {
63
63
write ! ( f, "'{}' is not a valid date/time value" , value)
@@ -188,7 +188,7 @@ impl Display for HomieColorValue {
188
188
impl FromStr for HomieColorValue {
189
189
type Err = Homie5ValueConversionError ;
190
190
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
191
- let mut tokens = str :: split ( s , ',' ) ;
191
+ let mut tokens = s . split ( ',' ) ;
192
192
match tokens. next ( ) {
193
193
Some ( "rgb" ) => {
194
194
if let ( Some ( Ok ( r) ) , Some ( Ok ( g) ) , Some ( Ok ( b) ) ) = (
@@ -620,12 +620,13 @@ impl HomieValue {
620
620
let HomiePropertyFormat :: FloatRange ( range) = & property_desc. format else {
621
621
return Ok ( value) ;
622
622
} ;
623
- // Use the minimum, max, or current value as base (in that priority order)
623
+
624
+ // Use min as base, if not present, use max, otherwise use the current value
624
625
let base = range. min . or ( range. max ) . unwrap_or ( value) ;
625
626
626
- // Calculate the rounded value based on the step
627
+ // Adjust rounding logic: floor((x + 0.5)) to always round up
627
628
let rounded = match range. step {
628
- Some ( s) if s > 0.0 => ( ( value - base) / s) . round ( ) * s + base,
629
+ Some ( s) if s > 0.0 => ( ( value - base) / s + 0.5 ) . floor ( ) * s + base,
629
630
_ => value,
630
631
} ;
631
632
@@ -642,12 +643,12 @@ impl HomieValue {
642
643
return Ok ( value) ;
643
644
} ;
644
645
645
- // Use the minimum or maximum as the base, or use the current value
646
+ // Use min as base, if not present, use max, otherwise use the current value
646
647
let base = range. min . or ( range. max ) . unwrap_or ( value) ;
647
648
648
- // Calculate the rounded value based on the step
649
+ // Adjust rounding logic: floor((x + 0.5)) to always round up
649
650
let rounded = match range. step {
650
- Some ( s) if s > 0 => ( ( value - base) as f64 / s as f64 ) . round ( ) as i64 * s + base,
651
+ Some ( s) if s > 0 => ( ( value - base) as f64 / s as f64 + 0.5 ) . floor ( ) as i64 * s + base,
651
652
_ => value,
652
653
} ;
653
654
0 commit comments