Home Assistant Unofficial Reference 2024.12.1
homeassistant.util.percentage Namespace Reference

Functions

int _T (list[_T] ordered_list, _T item)
 
_T _T (list[_T] ordered_list, int percentage)
 
float percentage_to_ranged_value (tuple[float, float] low_high_range, float percentage)
 
int ranged_value_to_percentage (tuple[float, float] low_high_range, float value)
 

Detailed Description

Percentage util functions.

Function Documentation

◆ _T() [1/2]

int homeassistant.util.percentage._T ( list[_T]  ordered_list,
_T  item 
)
private
Determine the percentage of an item in an ordered list.

When using this utility for fan speeds, do not include "off"

Given the list: ["low", "medium", "high", "very_high"], this
function will return the following when the item is passed
in:

    low: 25
    medium: 50
    high: 75
    very_high: 100

Definition at line 13 of file percentage.py.

◆ _T() [2/2]

_T homeassistant.util.percentage._T ( list[_T]  ordered_list,
int  percentage 
)
private
Find the item that most closely matches the percentage in an ordered list.

When using this utility for fan speeds, do not include "off"

Given the list: ["low", "medium", "high", "very_high"], this
function will return the following when when the item is passed
in:

    1-25: low
    26-50: medium
    51-75: high
    76-100: very_high

Definition at line 36 of file percentage.py.

◆ percentage_to_ranged_value()

float homeassistant.util.percentage.percentage_to_ranged_value ( tuple[float, float]  low_high_range,
float   percentage 
)
Given a range of low and high values convert a percentage to a single value.

When using this utility for fan speeds, do not include 0 if it is off

Given a low value of 1 and a high value of 255 this function
will return:

(1,255), 100: 255
(1,255), 50: 127.5
(1,255), 4: 10.2

Definition at line 79 of file percentage.py.

◆ ranged_value_to_percentage()

int homeassistant.util.percentage.ranged_value_to_percentage ( tuple[float, float]  low_high_range,
float   value 
)
Given a range of low and high values convert a single value to a percentage.

When using this utility for fan speeds, do not include 0 if it is off

Given a low value of 1 and a high value of 255 this function
will return:

(1,255), 255: 100
(1,255), 127: 50
(1,255), 10: 4

Definition at line 62 of file percentage.py.