* Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
208 lines
6.6 KiB
Python
208 lines
6.6 KiB
Python
"""The tests for the integration sensor platform."""
|
|
from datetime import timedelta
|
|
from unittest.mock import patch
|
|
|
|
from homeassistant.setup import async_setup_component
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
|
async def test_state(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, 1, {})
|
|
await hass.async_block_till_done()
|
|
|
|
now = dt_util.utcnow() + timedelta(seconds=3600)
|
|
with patch('homeassistant.util.dt.utcnow',
|
|
return_value=now):
|
|
hass.states.async_set(entity_id, 1, {}, force_update=True)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get('sensor.integration')
|
|
assert state is not None
|
|
|
|
# Testing a power sensor at 1 KiloWatts for 1hour = 1kWh
|
|
assert round(float(state.state), config['sensor']['round']) == 1.0
|
|
|
|
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 = {
|
|
'sensor': {
|
|
'platform': 'integration',
|
|
'name': 'integration',
|
|
'source': 'sensor.power',
|
|
'round': 2,
|
|
'unit_prefix': 'k'
|
|
}
|
|
}
|
|
|
|
assert await async_setup_component(hass, 'sensor', config)
|
|
|
|
entity_id = config['sensor']['source']
|
|
hass.states.async_set(entity_id, 1000, {'unit_of_measurement': 'W'})
|
|
await hass.async_block_till_done()
|
|
|
|
now = dt_util.utcnow() + timedelta(seconds=3600)
|
|
with patch('homeassistant.util.dt.utcnow',
|
|
return_value=now):
|
|
hass.states.async_set(entity_id, 1000, {'unit_of_measurement': 'W'},
|
|
force_update=True)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get('sensor.integration')
|
|
assert state is not None
|
|
|
|
# Testing a power sensor at 1000 Watts for 1hour = 1kWh
|
|
assert round(float(state.state), config['sensor']['round']) == 1.0
|
|
assert state.attributes.get('unit_of_measurement') == 'kWh'
|
|
|
|
|
|
async def test_suffix(hass):
|
|
"""Test integration sensor state using a network counter source."""
|
|
config = {
|
|
'sensor': {
|
|
'platform': 'integration',
|
|
'name': 'integration',
|
|
'source': 'sensor.bytes_per_second',
|
|
'round': 2,
|
|
'unit_prefix': 'k',
|
|
'unit_time': 's'
|
|
}
|
|
}
|
|
|
|
assert await async_setup_component(hass, 'sensor', config)
|
|
|
|
entity_id = config['sensor']['source']
|
|
hass.states.async_set(entity_id, 1000, {})
|
|
await hass.async_block_till_done()
|
|
|
|
now = dt_util.utcnow() + timedelta(seconds=10)
|
|
with patch('homeassistant.util.dt.utcnow',
|
|
return_value=now):
|
|
hass.states.async_set(entity_id, 1000, {}, force_update=True)
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get('sensor.integration')
|
|
assert state is not None
|
|
|
|
# Testing a network speed sensor at 1000 bytes/s over 10s = 10kbytes
|
|
assert round(float(state.state), config['sensor']['round']) == 10.0
|