MikeTheTux / openhab

0 stars 0 forks source link

dynamic icons for QuantityTypes #56

Closed MikeTheTux closed 4 years ago

MikeTheTux commented 5 years ago

https://community.openhab.org/t/dynamic-icons-to-show-battery-status/3629/6

MikeTheTux commented 5 years ago

for i in seq -98 -60; do sudo ln volume--99.png volume-${i}.png; done for i in seq -58 -20; do sudo ln volume--59.png volume-${i}.png; done for i in seq -19 14; do sudo ln volume-15.png volume-${i}.png; done

MikeTheTux commented 5 years ago

https://community.home-assistant.io/t/xiaomi-aqara-battery-level/36222/10

This voltage is mapped to a new range: [2.8V...3.3V] -> [0...100]: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/xiaomi_aqara.py#L292-L303 85

>>> max_volt = 3300
>>> min_volt = 2800
>>> voltage = 3005
>>> voltage = min(voltage, max_volt)
>>> voltage
3005
>>> voltage = max(voltage, min_volt)
>>> voltage
3005
>>> ((voltage - min_volt) / (max_volt - min_volt))
0.41
>>> percent = ((voltage - min_volt) / (max_volt - min_volt)) * 100
>>> percent
41.0
>>> 
MikeTheTux commented 5 years ago

https://github.com/eclipse/smarthome/blob/master/bundles/ui/org.eclipse.smarthome.ui.icon/src/main/java/org/eclipse/smarthome/ui/icon/AbstractResourceIconProvider.java

    @Override
    public InputStream getIcon(String category, String iconSetId, String state, Format format) {
        String resourceWithoutState = category.toLowerCase() + "." + format.toString().toLowerCase();
        if (state == null) {
            return getResource(iconSetId, resourceWithoutState);
        }

        String iconState;
        if (state.contains(" ")) {
            try {
                String firstPart = state.substring(0, state.indexOf(" "));
                Double.valueOf(firstPart);
                iconState = firstPart;
            } catch (NumberFormatException e) {
                // firstPart is not a number, pass on the full state
                iconState = state;
            }
        } else {
            iconState = state;
        }

        String resourceWithState = category.toLowerCase() + "-" + iconState.toLowerCase() + "."
                + format.toString().toLowerCase();
        if (hasResource(iconSetId, resourceWithState)) {
            return getResource(iconSetId, resourceWithState);
        } else {
            // let's treat all percentage-based categories
            try {
                Double stateAsDouble = Double.valueOf(iconState);
                if (stateAsDouble >= 0 && stateAsDouble <= 100) {
                    for (int i = stateAsDouble.intValue(); i >= 0; i--) {
                        String resourceWithNumberState = category.toLowerCase() + "-" + i + "."
                                + format.toString().toLowerCase();
                        if (hasResource(iconSetId, resourceWithNumberState)) {
                            return getResource(iconSetId, resourceWithNumberState);
                        }
                    }
                }
            } catch (NumberFormatException e) {
                // does not seem to be a number, so ignore it
            }
            logger.debug("Use icon {} as {} is not found", resourceWithoutState, resourceWithState);
            return getResource(iconSetId, resourceWithoutState);
        }
}