Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -7,65 +7,67 @@ import voluptuous as vol
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import (
|
||||
CONF_NAME, ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN, STATE_UNAVAILABLE)
|
||||
CONF_NAME,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
STATE_UNKNOWN,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.event import async_track_state_change
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_SOURCE_ID = 'source'
|
||||
ATTR_SOURCE_ID = "source"
|
||||
|
||||
CONF_SOURCE_SENSOR = 'source'
|
||||
CONF_ROUND_DIGITS = 'round'
|
||||
CONF_UNIT_PREFIX = 'unit_prefix'
|
||||
CONF_UNIT_TIME = 'unit_time'
|
||||
CONF_UNIT_OF_MEASUREMENT = 'unit'
|
||||
CONF_METHOD = 'method'
|
||||
CONF_SOURCE_SENSOR = "source"
|
||||
CONF_ROUND_DIGITS = "round"
|
||||
CONF_UNIT_PREFIX = "unit_prefix"
|
||||
CONF_UNIT_TIME = "unit_time"
|
||||
CONF_UNIT_OF_MEASUREMENT = "unit"
|
||||
CONF_METHOD = "method"
|
||||
|
||||
TRAPEZOIDAL_METHOD = 'trapezoidal'
|
||||
LEFT_METHOD = 'left'
|
||||
RIGHT_METHOD = 'right'
|
||||
TRAPEZOIDAL_METHOD = "trapezoidal"
|
||||
LEFT_METHOD = "left"
|
||||
RIGHT_METHOD = "right"
|
||||
INTEGRATION_METHOD = [TRAPEZOIDAL_METHOD, LEFT_METHOD, RIGHT_METHOD]
|
||||
|
||||
# SI Metric prefixes
|
||||
UNIT_PREFIXES = {None: 1,
|
||||
"k": 10**3,
|
||||
"G": 10**6,
|
||||
"T": 10**9}
|
||||
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}
|
||||
UNIT_TIME = {"s": 1, "min": 60, "h": 60 * 60, "d": 24 * 60 * 60}
|
||||
|
||||
ICON = 'mdi:chart-histogram'
|
||||
ICON = "mdi:chart-histogram"
|
||||
|
||||
DEFAULT_ROUND = 3
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Required(CONF_SOURCE_SENSOR): 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_OF_MEASUREMENT): cv.string,
|
||||
vol.Optional(CONF_METHOD, default=TRAPEZOIDAL_METHOD):
|
||||
vol.In(INTEGRATION_METHOD),
|
||||
})
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Required(CONF_SOURCE_SENSOR): 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_OF_MEASUREMENT): cv.string,
|
||||
vol.Optional(CONF_METHOD, default=TRAPEZOIDAL_METHOD): vol.In(
|
||||
INTEGRATION_METHOD
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the integration sensor."""
|
||||
integral = IntegrationSensor(config[CONF_SOURCE_SENSOR],
|
||||
config.get(CONF_NAME),
|
||||
config[CONF_ROUND_DIGITS],
|
||||
config[CONF_UNIT_PREFIX],
|
||||
config[CONF_UNIT_TIME],
|
||||
config.get(CONF_UNIT_OF_MEASUREMENT),
|
||||
config[CONF_METHOD])
|
||||
integral = IntegrationSensor(
|
||||
config[CONF_SOURCE_SENSOR],
|
||||
config.get(CONF_NAME),
|
||||
config[CONF_ROUND_DIGITS],
|
||||
config[CONF_UNIT_PREFIX],
|
||||
config[CONF_UNIT_TIME],
|
||||
config.get(CONF_UNIT_OF_MEASUREMENT),
|
||||
config[CONF_METHOD],
|
||||
)
|
||||
|
||||
async_add_entities([integral])
|
||||
|
||||
@@ -73,22 +75,28 @@ async def async_setup_platform(hass, config, async_add_entities,
|
||||
class IntegrationSensor(RestoreEntity):
|
||||
"""Representation of an integration sensor."""
|
||||
|
||||
def __init__(self, source_entity, name, round_digits, unit_prefix,
|
||||
unit_time, unit_of_measurement, integration_method):
|
||||
def __init__(
|
||||
self,
|
||||
source_entity,
|
||||
name,
|
||||
round_digits,
|
||||
unit_prefix,
|
||||
unit_time,
|
||||
unit_of_measurement,
|
||||
integration_method,
|
||||
):
|
||||
"""Initialize the integration sensor."""
|
||||
self._sensor_source_id = source_entity
|
||||
self._round_digits = round_digits
|
||||
self._state = 0
|
||||
self._method = integration_method
|
||||
|
||||
self._name = name if name is not None\
|
||||
else '{} integral'.format(source_entity)
|
||||
self._name = name if name is not None else "{} integral".format(source_entity)
|
||||
|
||||
if unit_of_measurement is None:
|
||||
self._unit_template = "{}{}{}".format(
|
||||
"" if unit_prefix is None else unit_prefix,
|
||||
"{}",
|
||||
unit_time)
|
||||
"" if unit_prefix is None else unit_prefix, "{}", unit_time
|
||||
)
|
||||
# we postpone the definition of unit_of_measurement to later
|
||||
self._unit_of_measurement = None
|
||||
else:
|
||||
@@ -110,45 +118,52 @@ class IntegrationSensor(RestoreEntity):
|
||||
@callback
|
||||
def calc_integration(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]:
|
||||
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)
|
||||
"" if unit is None else unit
|
||||
)
|
||||
|
||||
try:
|
||||
# integration as the Riemann integral of previous measures.
|
||||
area = 0
|
||||
elapsed_time = (new_state.last_updated
|
||||
- old_state.last_updated).total_seconds()
|
||||
elapsed_time = (
|
||||
new_state.last_updated - old_state.last_updated
|
||||
).total_seconds()
|
||||
|
||||
if self._method == TRAPEZOIDAL_METHOD:
|
||||
area = (Decimal(new_state.state)
|
||||
+ Decimal(old_state.state))*Decimal(elapsed_time)/2
|
||||
area = (
|
||||
(Decimal(new_state.state) + Decimal(old_state.state))
|
||||
* Decimal(elapsed_time)
|
||||
/ 2
|
||||
)
|
||||
elif self._method == LEFT_METHOD:
|
||||
area = Decimal(old_state.state)*Decimal(elapsed_time)
|
||||
area = Decimal(old_state.state) * Decimal(elapsed_time)
|
||||
elif self._method == RIGHT_METHOD:
|
||||
area = Decimal(new_state.state)*Decimal(elapsed_time)
|
||||
area = Decimal(new_state.state) * Decimal(elapsed_time)
|
||||
|
||||
integral = area / (self._unit_prefix * self._unit_time)
|
||||
assert isinstance(integral, Decimal)
|
||||
except ValueError as err:
|
||||
_LOGGER.warning("While calculating integration: %s", err)
|
||||
except DecimalException as err:
|
||||
_LOGGER.warning("Invalid state (%s > %s): %s",
|
||||
old_state.state, new_state.state, err)
|
||||
_LOGGER.warning(
|
||||
"Invalid state (%s > %s): %s", old_state.state, new_state.state, err
|
||||
)
|
||||
except AssertionError as err:
|
||||
_LOGGER.error("Could not calculate integral: %s", err)
|
||||
else:
|
||||
self._state += integral
|
||||
self.async_schedule_update_ha_state()
|
||||
|
||||
async_track_state_change(
|
||||
self.hass, self._sensor_source_id, calc_integration)
|
||||
async_track_state_change(self.hass, self._sensor_source_id, calc_integration)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@@ -173,9 +188,7 @@ class IntegrationSensor(RestoreEntity):
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes of the sensor."""
|
||||
state_attr = {
|
||||
ATTR_SOURCE_ID: self._sensor_source_id,
|
||||
}
|
||||
state_attr = {ATTR_SOURCE_ID: self._sensor_source_id}
|
||||
return state_attr
|
||||
|
||||
@property
|
||||
|
||||
Reference in new issue
Block a user