Add Compensation Integration (#41675)
* Add Compensation Integration Adds the Compensation Integration * Add Requirements add missing requirements to compensation integration * Fix for tests Fix files after tests * Fix isort ran isort * Handle ADR-0007 Change the configuration to deal with ADR-0007 * fix flake8 Fix flake8 * Added Error Trapping Catch errors. Raise Rank Warnings but continue. Fixed bad imports * fix flake8 & pylint * fix isort.... again * fix tests & comments fix tests and comments * fix flake8 * remove discovery message * Fixed Review changes * Fixed review requests. * Added test to test get more coverage. * Roll back numpy requirement Roll back numpy requirement to match other integrations. * Fix flake8 * Fix requested changes Removed some necessary comments. Changed a test case to be more readable. * Fix doc strings and continue * Fixed a few test case doc strings * Removed a continue/else * Remove periods from logger Removed periods from _LOGGER errors. * Fixes changed name to unqiue_id. implemented suggested changes. * Add name and fix unique_id * removed conf name and auto construct it
This commit is contained in:
parent
23fae255ff
commit
545fe7a7be
9 changed files with 537 additions and 0 deletions
228
tests/components/compensation/test_sensor.py
Normal file
228
tests/components/compensation/test_sensor.py
Normal file
|
@ -0,0 +1,228 @@
|
|||
"""The tests for the integration sensor platform."""
|
||||
|
||||
from homeassistant.components.compensation.const import CONF_PRECISION, DOMAIN
|
||||
from homeassistant.components.compensation.sensor import ATTR_COEFFICIENTS
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
EVENT_STATE_CHANGED,
|
||||
STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
async def test_linear_state(hass):
|
||||
"""Test compensation sensor state."""
|
||||
config = {
|
||||
"compensation": {
|
||||
"test": {
|
||||
"source": "sensor.uncompensated",
|
||||
"data_points": [
|
||||
[1.0, 2.0],
|
||||
[2.0, 3.0],
|
||||
],
|
||||
"precision": 2,
|
||||
"unit_of_measurement": "a",
|
||||
}
|
||||
}
|
||||
}
|
||||
expected_entity_id = "sensor.compensation_sensor_uncompensated"
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
assert await async_setup_component(hass, SENSOR_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
entity_id = config[DOMAIN]["test"]["source"]
|
||||
hass.states.async_set(entity_id, 4, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(expected_entity_id)
|
||||
assert state is not None
|
||||
|
||||
assert round(float(state.state), config[DOMAIN]["test"][CONF_PRECISION]) == 5.0
|
||||
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "a"
|
||||
|
||||
coefs = [round(v, 1) for v in state.attributes.get(ATTR_COEFFICIENTS)]
|
||||
assert coefs == [1.0, 1.0]
|
||||
|
||||
hass.states.async_set(entity_id, "foo", {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(expected_entity_id)
|
||||
assert state is not None
|
||||
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
|
||||
async def test_linear_state_from_attribute(hass):
|
||||
"""Test compensation sensor state that pulls from attribute."""
|
||||
config = {
|
||||
"compensation": {
|
||||
"test": {
|
||||
"source": "sensor.uncompensated",
|
||||
"attribute": "value",
|
||||
"data_points": [
|
||||
[1.0, 2.0],
|
||||
[2.0, 3.0],
|
||||
],
|
||||
"precision": 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
expected_entity_id = "sensor.compensation_sensor_uncompensated_value"
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
assert await async_setup_component(hass, SENSOR_DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
|
||||
entity_id = config[DOMAIN]["test"]["source"]
|
||||
hass.states.async_set(entity_id, 3, {"value": 4})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(expected_entity_id)
|
||||
assert state is not None
|
||||
|
||||
assert round(float(state.state), config[DOMAIN]["test"][CONF_PRECISION]) == 5.0
|
||||
|
||||
coefs = [round(v, 1) for v in state.attributes.get(ATTR_COEFFICIENTS)]
|
||||
assert coefs == [1.0, 1.0]
|
||||
|
||||
hass.states.async_set(entity_id, 3, {"value": "bar"})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(expected_entity_id)
|
||||
assert state is not None
|
||||
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
|
||||
async def test_quadratic_state(hass):
|
||||
"""Test 3 degree polynominial compensation sensor."""
|
||||
config = {
|
||||
"compensation": {
|
||||
"test": {
|
||||
"source": "sensor.temperature",
|
||||
"data_points": [
|
||||
[50, 3.3],
|
||||
[50, 2.8],
|
||||
[50, 2.9],
|
||||
[70, 2.3],
|
||||
[70, 2.6],
|
||||
[70, 2.1],
|
||||
[80, 2.5],
|
||||
[80, 2.9],
|
||||
[80, 2.4],
|
||||
[90, 3.0],
|
||||
[90, 3.1],
|
||||
[90, 2.8],
|
||||
[100, 3.3],
|
||||
[100, 3.5],
|
||||
[100, 3.0],
|
||||
],
|
||||
"degree": 2,
|
||||
"precision": 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_id = config[DOMAIN]["test"]["source"]
|
||||
hass.states.async_set(entity_id, 43.2, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.compensation_sensor_temperature")
|
||||
|
||||
assert state is not None
|
||||
|
||||
assert round(float(state.state), config[DOMAIN]["test"][CONF_PRECISION]) == 3.327
|
||||
|
||||
|
||||
async def test_numpy_errors(hass, caplog):
|
||||
"""Tests bad polyfits."""
|
||||
config = {
|
||||
"compensation": {
|
||||
"test": {
|
||||
"source": "sensor.uncompensated",
|
||||
"data_points": [
|
||||
[1.0, 1.0],
|
||||
[1.0, 1.0],
|
||||
],
|
||||
},
|
||||
"test2": {
|
||||
"source": "sensor.uncompensated2",
|
||||
"data_points": [
|
||||
[0.0, 1.0],
|
||||
[0.0, 1.0],
|
||||
],
|
||||
},
|
||||
}
|
||||
}
|
||||
await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "polyfit may be poorly conditioned" in caplog.text
|
||||
|
||||
assert "invalid value encountered in true_divide" in caplog.text
|
||||
|
||||
|
||||
async def test_datapoints_greater_than_degree(hass, caplog):
|
||||
"""Tests 3 bad data points."""
|
||||
config = {
|
||||
"compensation": {
|
||||
"test": {
|
||||
"source": "sensor.uncompensated",
|
||||
"data_points": [
|
||||
[1.0, 2.0],
|
||||
[2.0, 3.0],
|
||||
],
|
||||
"degree": 2,
|
||||
},
|
||||
}
|
||||
}
|
||||
await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "data_points must have at least 3 data_points" in caplog.text
|
||||
|
||||
|
||||
async def test_new_state_is_none(hass):
|
||||
"""Tests catch for empty new states."""
|
||||
config = {
|
||||
"compensation": {
|
||||
"test": {
|
||||
"source": "sensor.uncompensated",
|
||||
"data_points": [
|
||||
[1.0, 2.0],
|
||||
[2.0, 3.0],
|
||||
],
|
||||
"precision": 2,
|
||||
"unit_of_measurement": "a",
|
||||
}
|
||||
}
|
||||
}
|
||||
expected_entity_id = "sensor.compensation_sensor_uncompensated"
|
||||
|
||||
await async_setup_component(hass, DOMAIN, config)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
last_changed = hass.states.get(expected_entity_id).last_changed
|
||||
|
||||
hass.bus.async_fire(
|
||||
EVENT_STATE_CHANGED, event_data={"entity_id": "sensor.uncompensated"}
|
||||
)
|
||||
|
||||
assert last_changed == hass.states.get(expected_entity_id).last_changed
|
Loading…
Add table
Add a link
Reference in a new issue