Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b97e1c9
create derivation component
afaucogney Sep 5, 2019
b27c9e0
Update test
afaucogney Sep 5, 2019
075a72d
add some functionnal point test
afaucogney Sep 5, 2019
9cfd018
Change derivation to derivative
afaucogney Sep 9, 2019
efdfef6
Continue migration from derivation to derivative
afaucogney Sep 9, 2019
a1e6519
Add codeowners info
afaucogney Sep 9, 2019
f55c5e6
fix tests typo and values
afaucogney Sep 9, 2019
f7f1ac5
Improve code from reviewer
afaucogney Sep 17, 2019
8e690d0
Merge pull request #1 from home-assistant/dev
afaucogney Sep 23, 2019
72131fe
create derivation component
afaucogney Sep 5, 2019
65519cb
Update test
afaucogney Sep 5, 2019
ae685f0
add some functionnal point test
afaucogney Sep 5, 2019
d600a83
Change derivation to derivative
afaucogney Sep 9, 2019
656c719
Continue migration from derivation to derivative
afaucogney Sep 9, 2019
d6f34e4
Add codeowners info
afaucogney Sep 9, 2019
eb775e5
fix tests typo and values
afaucogney Sep 9, 2019
aa49251
Improve code from reviewer
afaucogney Sep 17, 2019
a46bbad
Merge remote-tracking branch 'origin/feature/derivative' into feature…
afaucogney Sep 23, 2019
140236d
Update homeassistant/components/derivative/sensor.py
afaucogney Sep 24, 2019
f33b88f
Fix review
afaucogney Nov 28, 2019
680426e
precise state test
afaucogney Dec 18, 2019
4cc647c
Merge branch 'feature/derivative' of github.com:afaucogney/home-assis…
afaucogney Jan 4, 2020
4cbcd98
un comment original tests and remove error tests
afaucogney Jan 4, 2020
c9c1003
Merge branch 'feature/derivative' of https://github.com/afaucogney/ho…
afaucogney Jan 4, 2020
79e1b29
Fix isort issue
afaucogney Jan 4, 2020
e354611
Fix review
afaucogney Jan 12, 2020
6f6632c
Rollback import conf_unit, just defined localy
afaucogney Jan 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ homeassistant/components/darksky/* @fabaff
homeassistant/components/deconz/* @kane610
homeassistant/components/delijn/* @bollewolle
homeassistant/components/demo/* @home-assistant/core
homeassistant/components/derivative/* @afaucogney
homeassistant/components/device_automation/* @home-assistant/core
homeassistant/components/digital_ocean/* @fabaff
homeassistant/components/discogs/* @thibmaek
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/derivative/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""The derivative component."""
10 changes: 10 additions & 0 deletions homeassistant/components/derivative/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"domain": "derivative",
"name": "Derivative",
"documentation": "https://www.home-assistant.io/integrations/derivative",
"requirements": [],
"dependencies": [],
"codeowners": [
"@afaucogney"
]
}
177 changes: 177 additions & 0 deletions homeassistant/components/derivative/sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
"""Numeric derivative of data coming from a source sensor over time."""
from decimal import Decimal, DecimalException
import logging

import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
CONF_NAME,
CONF_SOURCE,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import async_track_state_change
from homeassistant.helpers.restore_state import RestoreEntity

# mypy: allow-untyped-defs, no-check-untyped-defs

_LOGGER = logging.getLogger(__name__)

ATTR_SOURCE_ID = "source"

CONF_ROUND_DIGITS = "round"
CONF_UNIT_PREFIX = "unit_prefix"
CONF_UNIT_TIME = "unit_time"
CONF_UNIT = "unit"

# SI Metric prefixes
UNIT_PREFIXES = {None: 1, "k": 10 ** 3, "G": 10 ** 6, "T": 10 ** 9}

# SI Time prefixes
UNIT_TIME = {"s": 1, "min": 60, "h": 60 * 60, "d": 24 * 60 * 60}

ICON = "mdi:chart-line"

DEFAULT_ROUND = 3

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_SOURCE): cv.entity_id,
vol.Optional(CONF_ROUND_DIGITS, default=DEFAULT_ROUND): vol.Coerce(int),
vol.Optional(CONF_UNIT_PREFIX, default=None): vol.In(UNIT_PREFIXES),
vol.Optional(CONF_UNIT_TIME, default="h"): vol.In(UNIT_TIME),
vol.Optional(CONF_UNIT): cv.string,
}
)


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the derivative sensor."""
derivative = DerivativeSensor(
config[CONF_SOURCE],
config.get(CONF_NAME),
config[CONF_ROUND_DIGITS],
config[CONF_UNIT_PREFIX],
config[CONF_UNIT_TIME],
config.get(CONF_UNIT),
)

async_add_entities([derivative])


class DerivativeSensor(RestoreEntity):
"""Representation of an derivative sensor."""

def __init__(
self,
source_entity,
name,
round_digits,
unit_prefix,
unit_time,
unit_of_measurement,
):
"""Initialize the derivative sensor."""
self._sensor_source_id = source_entity
self._round_digits = round_digits
self._state = 0

self._name = name if name is not None else f"{source_entity} derivative"

if unit_of_measurement is None:
final_unit_prefix = "" if unit_prefix is None else unit_prefix
self._unit_template = f"{final_unit_prefix}{{}}/{unit_time}"
# we postpone the definition of unit_of_measurement to later
self._unit_of_measurement = None
else:
self._unit_of_measurement = unit_of_measurement

self._unit_prefix = UNIT_PREFIXES[unit_prefix]
self._unit_time = UNIT_TIME[unit_time]

async def async_added_to_hass(self):
"""Handle entity which will be added."""
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state is not None:
try:
self._state = Decimal(state.state)
except SyntaxError as err:
_LOGGER.warning("Could not restore last state: %s", err)

@callback
def calc_derivative(entity, old_state, new_state):
"""Handle the sensor state changes."""
if (
old_state is None
or old_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE]
or new_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE]
):
return

if self._unit_of_measurement is None:
unit = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
self._unit_of_measurement = self._unit_template.format(
"" if unit is None else unit
)

try:
# derivative of previous measures.
gradient = 0
elapsed_time = (
new_state.last_updated - old_state.last_updated
).total_seconds()
gradient = Decimal(new_state.state) - Decimal(old_state.state)
derivative = gradient / (
Decimal(elapsed_time) * (self._unit_prefix * self._unit_time)
)
assert isinstance(derivative, Decimal)
except ValueError as err:
_LOGGER.warning("While calculating derivative: %s", err)
except DecimalException as err:
_LOGGER.warning(
"Invalid state (%s > %s): %s", old_state.state, new_state.state, err
)
except AssertionError as err:
_LOGGER.error("Could not calculate derivative: %s", err)
else:
self._state = derivative
self.async_schedule_update_ha_state()

async_track_state_change(self.hass, self._sensor_source_id, calc_derivative)

@property
def name(self):
"""Return the name of the sensor."""
return self._name

@property
def state(self):
"""Return the state of the sensor."""
return round(self._state, self._round_digits)

@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit_of_measurement

@property
def should_poll(self):
"""No polling needed."""
return False

@property
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
state_attr = {ATTR_SOURCE_ID: self._sensor_source_id}
return state_attr

@property
def icon(self):
"""Return the icon to use in the frontend."""
return ICON
1 change: 1 addition & 0 deletions tests/components/derivative/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for the derivative component."""
Loading