1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-26 14:08:21 +00:00

Add integration method to sensor.integration (#21050)

* add integration method and respective new methods

* ack @ottowinter tip

* align const name with value
This commit is contained in:
Diogo Gomes
2019-02-14 04:26:27 +00:00
committed by Paulus Schoutsen
parent 81d2ec9618
commit f1f3074612
2 changed files with 127 additions and 6 deletions

View File

@@ -39,6 +39,110 @@ async def test_state(hass):
assert state.attributes.get('unit_of_measurement') == 'kWh'
async def test_trapezoidal(hass):
"""Test integration sensor state."""
config = {
'sensor': {
'platform': 'integration',
'name': 'integration',
'source': 'sensor.power',
'unit': 'kWh',
'round': 2,
}
}
assert await async_setup_component(hass, 'sensor', config)
entity_id = config['sensor']['source']
hass.states.async_set(entity_id, 0, {})
await hass.async_block_till_done()
# Testing a power sensor with non-monotonic intervals and values
for time, value in [(20, 10), (30, 30), (40, 5), (50, 0)]:
now = dt_util.utcnow() + timedelta(minutes=time)
with patch('homeassistant.util.dt.utcnow',
return_value=now):
hass.states.async_set(entity_id, value, {}, force_update=True)
await hass.async_block_till_done()
state = hass.states.get('sensor.integration')
assert state is not None
assert round(float(state.state), config['sensor']['round']) == 8.33
assert state.attributes.get('unit_of_measurement') == 'kWh'
async def test_left(hass):
"""Test integration sensor state with left reimann method."""
config = {
'sensor': {
'platform': 'integration',
'name': 'integration',
'method': 'left',
'source': 'sensor.power',
'unit': 'kWh',
'round': 2,
}
}
assert await async_setup_component(hass, 'sensor', config)
entity_id = config['sensor']['source']
hass.states.async_set(entity_id, 0, {})
await hass.async_block_till_done()
# Testing a power sensor with non-monotonic intervals and values
for time, value in [(20, 10), (30, 30), (40, 5), (50, 0)]:
now = dt_util.utcnow() + timedelta(minutes=time)
with patch('homeassistant.util.dt.utcnow',
return_value=now):
hass.states.async_set(entity_id, value, {}, force_update=True)
await hass.async_block_till_done()
state = hass.states.get('sensor.integration')
assert state is not None
assert round(float(state.state), config['sensor']['round']) == 7.5
assert state.attributes.get('unit_of_measurement') == 'kWh'
async def test_right(hass):
"""Test integration sensor state with left reimann method."""
config = {
'sensor': {
'platform': 'integration',
'name': 'integration',
'method': 'right',
'source': 'sensor.power',
'unit': 'kWh',
'round': 2,
}
}
assert await async_setup_component(hass, 'sensor', config)
entity_id = config['sensor']['source']
hass.states.async_set(entity_id, 0, {})
await hass.async_block_till_done()
# Testing a power sensor with non-monotonic intervals and values
for time, value in [(20, 10), (30, 30), (40, 5), (50, 0)]:
now = dt_util.utcnow() + timedelta(minutes=time)
with patch('homeassistant.util.dt.utcnow',
return_value=now):
hass.states.async_set(entity_id, value, {}, force_update=True)
await hass.async_block_till_done()
state = hass.states.get('sensor.integration')
assert state is not None
assert round(float(state.state), config['sensor']['round']) == 9.17
assert state.attributes.get('unit_of_measurement') == 'kWh'
async def test_prefix(hass):
"""Test integration sensor state using a power source."""
config = {