Skip to content

Commit

Permalink
number rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
pilotak committed Jun 3, 2020
1 parent d58f021 commit b03ee2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Configuration variables:
- **friendly_name** (*Optional*): Name to use in the Frontend *(will be the same for all entities specified at the moment)*.
- **icon** (*Optional*): Icon to use in the Frontend.
- **unit_of_measurement** (*Optional*): Defines the units of measurement of the sensor, if any.
- **round_to** (*Optional*): round numbers to 'x' decimals, if zero it will become whole number. Skip this field if you extracting a string or you want to leave the value as it is.
- **time_format** (*Optional*): **`strftime`** type string to beautify time attribute output. Applicable only when attribute `last_changed` or `last_triggered` is selected. Cheatsheet for strftime formatting [here](http://strftime.ninja/).

## Install via [HACS](https://github.com/custom-components/hacs)
Expand Down
19 changes: 15 additions & 4 deletions custom_components/attributes/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@

CONF_ATTRIBUTE = "attribute"
CONF_TIME_FORMAT = "time_format"
CONF_ROUND_TO = "round_to"

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(ATTR_ICON): cv.string,
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_TIME_FORMAT): cv.string,
vol.Optional(CONF_ROUND_TO): cv.positive_int,
vol.Required(CONF_ATTRIBUTE): cv.string,
vol.Required(CONF_ENTITIES): cv.entity_ids
})
Expand All @@ -44,6 +46,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
for device in config[CONF_ENTITIES]:
attr = config.get(CONF_ATTRIBUTE)
time_format = str(config.get(CONF_TIME_FORMAT))
round_to = config.get(CONF_ROUND_TO, None)

if (attr == "last_triggered" or
attr == "last_changed") and time_format:
Expand All @@ -60,10 +63,18 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
{{% else %}} {2} {{% endif %}}").format(
device, attr, STATE_UNKNOWN)
else:
state_template = ("{{% if states('{0}') != '{2}' %}}\
{{{{ states.{0}.attributes['{1}'] }}}}\
{{% else %}} {2} {{% endif %}}").format(
device, attr, STATE_UNKNOWN)
state_template = "{{% if states('{0}') != '{2}' %}}"

if round_to is None:
state_template += "{{{{ states.{0}.attributes['{1}'] }}}}"
elif round_to > 0:
state_template += "{{{{ (states.{0}.attributes['{1}'] | float) | round({3}) }}}}"
else:
state_template += "{{{{ states.{0}.attributes['{1}'] | int }}}}"

state_template += "{{% else %}} {2} {{% endif %}}"
state_template = state_template.format(
device, attr, STATE_UNKNOWN, round_to)

_LOGGER.info("Adding attribute: %s of entity: %s", attr, device)
_LOGGER.debug("Applying template: %s", state_template)
Expand Down

0 comments on commit b03ee2b

Please sign in to comment.