Consolidate all platforms that have tests (#22109)

* 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.
This commit is contained in:
Penny Wood 2019-03-19 14:07:39 +08:00 committed by Paulus Schoutsen
parent 46ece3603f
commit f195ecca4b
486 changed files with 662 additions and 455 deletions

View file

@ -0,0 +1,316 @@
"""The test for the Trend sensor platform."""
from homeassistant import setup
from tests.common import get_test_home_assistant, assert_setup_component
class TestTrendBinarySensor:
"""Test the Trend sensor."""
hass = None
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_up(self):
"""Test up trend."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id':
"sensor.test_state"
}
}
}
})
self.hass.states.set('sensor.test_state', '1')
self.hass.block_till_done()
self.hass.states.set('sensor.test_state', '2')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'on'
def test_up_using_trendline(self):
"""Test up trend using multiple samples and trendline calculation."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id': "sensor.test_state",
'sample_duration': 300,
'min_gradient': 1,
'max_samples': 25,
}
}
}
})
for val in [1, 0, 2, 3]:
self.hass.states.set('sensor.test_state', val)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'on'
for val in [0, 1, 0, 0]:
self.hass.states.set('sensor.test_state', val)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'off'
def test_down_using_trendline(self):
"""Test down trend using multiple samples and trendline calculation."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id': "sensor.test_state",
'sample_duration': 300,
'min_gradient': 1,
'max_samples': 25,
'invert': 'Yes'
}
}
}
})
for val in [3, 2, 3, 1]:
self.hass.states.set('sensor.test_state', val)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'on'
for val in [4, 2, 4, 4]:
self.hass.states.set('sensor.test_state', val)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'off'
def test_down(self):
"""Test down trend."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id':
"sensor.test_state"
}
}
}
})
self.hass.states.set('sensor.test_state', '2')
self.hass.block_till_done()
self.hass.states.set('sensor.test_state', '1')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'off'
def test_invert_up(self):
"""Test up trend with custom message."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id':
"sensor.test_state",
'invert': "Yes"
}
}
}
})
self.hass.states.set('sensor.test_state', '1')
self.hass.block_till_done()
self.hass.states.set('sensor.test_state', '2')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'off'
def test_invert_down(self):
"""Test down trend with custom message."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id':
"sensor.test_state",
'invert': "Yes"
}
}
}
})
self.hass.states.set('sensor.test_state', '2')
self.hass.block_till_done()
self.hass.states.set('sensor.test_state', '1')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'on'
def test_attribute_up(self):
"""Test attribute up trend."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id':
"sensor.test_state",
'attribute': 'attr'
}
}
}
})
self.hass.states.set('sensor.test_state', 'State', {'attr': '1'})
self.hass.block_till_done()
self.hass.states.set('sensor.test_state', 'State', {'attr': '2'})
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'on'
def test_attribute_down(self):
"""Test attribute down trend."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id':
"sensor.test_state",
'attribute': 'attr'
}
}
}
})
self.hass.states.set('sensor.test_state', 'State', {'attr': '2'})
self.hass.block_till_done()
self.hass.states.set('sensor.test_state', 'State', {'attr': '1'})
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'off'
def test_max_samples(self):
"""Test that sample count is limited correctly."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id': "sensor.test_state",
'max_samples': 3,
'min_gradient': -1,
}
}
}
})
for val in [0, 1, 2, 3, 2, 1]:
self.hass.states.set('sensor.test_state', val)
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'on'
assert state.attributes['sample_count'] == 3
def test_non_numeric(self):
"""Test up trend."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id':
"sensor.test_state"
}
}
}
})
self.hass.states.set('sensor.test_state', 'Non')
self.hass.block_till_done()
self.hass.states.set('sensor.test_state', 'Numeric')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'off'
def test_missing_attribute(self):
"""Test attribute down trend."""
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend',
'sensors': {
'test_trend_sensor': {
'entity_id':
"sensor.test_state",
'attribute': 'missing'
}
}
}
})
self.hass.states.set('sensor.test_state', 'State', {'attr': '2'})
self.hass.block_till_done()
self.hass.states.set('sensor.test_state', 'State', {'attr': '1'})
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'off'
def test_invalid_name_does_not_create(self):
"""Test invalid name."""
with assert_setup_component(0):
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test INVALID sensor': {
'entity_id':
"sensor.test_state"
}
}
}
})
assert self.hass.states.all() == []
def test_invalid_sensor_does_not_create(self):
"""Test invalid sensor."""
with assert_setup_component(0):
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test_trend_sensor': {
'not_entity_id':
"sensor.test_state"
}
}
}
})
assert self.hass.states.all() == []
def test_no_sensors_does_not_create(self):
"""Test no sensors."""
with assert_setup_component(0):
assert setup.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend'
}
})
assert self.hass.states.all() == []