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:
1 parent
46ece3603f
commit
f195ecca4b
486 files changed
+662
-455
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Tests for command_line component."""
|
||||
@@ -0,0 +1,63 @@
|
||||
"""The tests for the Command line Binary sensor platform."""
|
||||
import unittest
|
||||
|
||||
from homeassistant.const import (STATE_ON, STATE_OFF)
|
||||
from homeassistant.components.command_line import binary_sensor as command_line
|
||||
from homeassistant.helpers import template
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
class TestCommandSensorBinarySensor(unittest.TestCase):
|
||||
"""Test the Command line Binary sensor."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_setup(self):
|
||||
"""Test sensor setup."""
|
||||
config = {'name': 'Test',
|
||||
'command': 'echo 1',
|
||||
'payload_on': '1',
|
||||
'payload_off': '0',
|
||||
'command_timeout': 15
|
||||
}
|
||||
|
||||
devices = []
|
||||
|
||||
def add_dev_callback(devs, update):
|
||||
"""Add callback to add devices."""
|
||||
for dev in devs:
|
||||
devices.append(dev)
|
||||
|
||||
command_line.setup_platform(self.hass, config, add_dev_callback)
|
||||
|
||||
assert 1 == len(devices)
|
||||
entity = devices[0]
|
||||
entity.update()
|
||||
assert 'Test' == entity.name
|
||||
assert STATE_ON == entity.state
|
||||
|
||||
def test_template(self):
|
||||
"""Test setting the state with a template."""
|
||||
data = command_line.CommandSensorData(self.hass, 'echo 10', 15)
|
||||
|
||||
entity = command_line.CommandBinarySensor(
|
||||
self.hass, data, 'test', None, '1.0', '0',
|
||||
template.Template('{{ value | multiply(0.1) }}', self.hass))
|
||||
entity.update()
|
||||
assert STATE_ON == entity.state
|
||||
|
||||
def test_sensor_off(self):
|
||||
"""Test setting the state with a template."""
|
||||
data = command_line.CommandSensorData(self.hass, 'echo 0', 15)
|
||||
|
||||
entity = command_line.CommandBinarySensor(
|
||||
self.hass, data, 'test', None, '1', '0', None)
|
||||
entity.update()
|
||||
assert STATE_OFF == entity.state
|
||||
@@ -0,0 +1,75 @@
|
||||
"""The tests the cover command line platform."""
|
||||
import os
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.cover import DOMAIN
|
||||
import homeassistant.components.command_line.cover as cmd_rs
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, SERVICE_CLOSE_COVER, SERVICE_OPEN_COVER,
|
||||
SERVICE_STOP_COVER)
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def rs(hass):
|
||||
"""Return CommandCover instance."""
|
||||
return cmd_rs.CommandCover(hass, 'foo', 'command_open', 'command_close',
|
||||
'command_stop', 'command_state', None)
|
||||
|
||||
|
||||
def test_should_poll_new(rs):
|
||||
"""Test the setting of polling."""
|
||||
assert rs.should_poll is True
|
||||
rs._command_state = None
|
||||
assert rs.should_poll is False
|
||||
|
||||
|
||||
def test_query_state_value(rs):
|
||||
"""Test with state value."""
|
||||
with mock.patch('subprocess.check_output') as mock_run:
|
||||
mock_run.return_value = b' foo bar '
|
||||
result = rs._query_state_value('runme')
|
||||
assert 'foo bar' == result
|
||||
assert mock_run.call_count == 1
|
||||
assert mock_run.call_args == mock.call('runme', shell=True)
|
||||
|
||||
|
||||
async def test_state_value(hass):
|
||||
"""Test with state value."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'cover_status')
|
||||
test_cover = {
|
||||
'command_state': 'cat {}'.format(path),
|
||||
'command_open': 'echo 1 > {}'.format(path),
|
||||
'command_close': 'echo 1 > {}'.format(path),
|
||||
'command_stop': 'echo 0 > {}'.format(path),
|
||||
'value_template': '{{ value }}'
|
||||
}
|
||||
assert await async_setup_component(hass, DOMAIN, {
|
||||
'cover': {
|
||||
'platform': 'command_line',
|
||||
'covers': {
|
||||
'test': test_cover
|
||||
}
|
||||
}
|
||||
}) is True
|
||||
|
||||
assert 'unknown' == hass.states.get('cover.test').state
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_OPEN_COVER,
|
||||
{ATTR_ENTITY_ID: 'cover.test'}, blocking=True)
|
||||
assert 'open' == hass.states.get('cover.test').state
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_CLOSE_COVER,
|
||||
{ATTR_ENTITY_ID: 'cover.test'}, blocking=True)
|
||||
assert 'open' == hass.states.get('cover.test').state
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_STOP_COVER,
|
||||
{ATTR_ENTITY_ID: 'cover.test'}, blocking=True)
|
||||
assert 'closed' == hass.states.get('cover.test').state
|
||||
@@ -0,0 +1,177 @@
|
||||
"""The tests for the Command line sensor platform."""
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.helpers.template import Template
|
||||
from homeassistant.components.command_line import sensor as command_line
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
class TestCommandSensorSensor(unittest.TestCase):
|
||||
"""Test the Command line sensor."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def update_side_effect(self, data):
|
||||
"""Side effect function for mocking CommandSensorData.update()."""
|
||||
self.commandline.data = data
|
||||
|
||||
def test_setup(self):
|
||||
"""Test sensor setup."""
|
||||
config = {'name': 'Test',
|
||||
'unit_of_measurement': 'in',
|
||||
'command': 'echo 5',
|
||||
'command_timeout': 15
|
||||
}
|
||||
devices = []
|
||||
|
||||
def add_dev_callback(devs, update):
|
||||
"""Add callback to add devices."""
|
||||
for dev in devs:
|
||||
devices.append(dev)
|
||||
|
||||
command_line.setup_platform(self.hass, config, add_dev_callback)
|
||||
|
||||
assert 1 == len(devices)
|
||||
entity = devices[0]
|
||||
entity.update()
|
||||
assert 'Test' == entity.name
|
||||
assert 'in' == entity.unit_of_measurement
|
||||
assert '5' == entity.state
|
||||
|
||||
def test_template(self):
|
||||
"""Test command sensor with template."""
|
||||
data = command_line.CommandSensorData(self.hass, 'echo 50', 15)
|
||||
|
||||
entity = command_line.CommandSensor(
|
||||
self.hass, data, 'test', 'in',
|
||||
Template('{{ value | multiply(0.1) }}', self.hass), [])
|
||||
|
||||
entity.update()
|
||||
assert 5 == float(entity.state)
|
||||
|
||||
def test_template_render(self):
|
||||
"""Ensure command with templates get rendered properly."""
|
||||
self.hass.states.set('sensor.test_state', 'Works')
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
'echo {{ states.sensor.test_state.state }}', 15
|
||||
)
|
||||
data.update()
|
||||
|
||||
assert "Works" == data.value
|
||||
|
||||
def test_bad_command(self):
|
||||
"""Test bad command."""
|
||||
data = command_line.CommandSensorData(self.hass, 'asdfasdf', 15)
|
||||
data.update()
|
||||
|
||||
assert data.value is None
|
||||
|
||||
def test_update_with_json_attrs(self):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
('echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
|
||||
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'),
|
||||
15
|
||||
)
|
||||
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key',
|
||||
'another_key',
|
||||
'key_three'])
|
||||
self.sensor.update()
|
||||
assert 'some_json_value' == \
|
||||
self.sensor.device_state_attributes['key']
|
||||
assert 'another_json_value' == \
|
||||
self.sensor.device_state_attributes['another_key']
|
||||
assert 'value_three' == \
|
||||
self.sensor.device_state_attributes['key_three']
|
||||
|
||||
@patch('homeassistant.components.command_line.sensor._LOGGER')
|
||||
def test_update_with_json_attrs_no_data(self, mock_logger):
|
||||
"""Test attributes when no JSON result fetched."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
'echo ', 15
|
||||
)
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key'])
|
||||
self.sensor.update()
|
||||
assert {} == self.sensor.device_state_attributes
|
||||
assert mock_logger.warning.called
|
||||
|
||||
@patch('homeassistant.components.command_line.sensor._LOGGER')
|
||||
def test_update_with_json_attrs_not_dict(self, mock_logger):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
'echo [1, 2, 3]', 15
|
||||
)
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key'])
|
||||
self.sensor.update()
|
||||
assert {} == self.sensor.device_state_attributes
|
||||
assert mock_logger.warning.called
|
||||
|
||||
@patch('homeassistant.components.command_line.sensor._LOGGER')
|
||||
def test_update_with_json_attrs_bad_JSON(self, mock_logger):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
'echo This is text rather than JSON data.', 15
|
||||
)
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key'])
|
||||
self.sensor.update()
|
||||
assert {} == self.sensor.device_state_attributes
|
||||
assert mock_logger.warning.called
|
||||
|
||||
def test_update_with_missing_json_attrs(self):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
('echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
|
||||
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'),
|
||||
15
|
||||
)
|
||||
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key',
|
||||
'another_key',
|
||||
'key_three',
|
||||
'special_key'])
|
||||
self.sensor.update()
|
||||
assert 'some_json_value' == \
|
||||
self.sensor.device_state_attributes['key']
|
||||
assert 'another_json_value' == \
|
||||
self.sensor.device_state_attributes['another_key']
|
||||
assert 'value_three' == \
|
||||
self.sensor.device_state_attributes['key_three']
|
||||
assert not ('special_key' in self.sensor.device_state_attributes)
|
||||
|
||||
def test_update_with_unnecessary_json_attrs(self):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
data = command_line.CommandSensorData(
|
||||
self.hass,
|
||||
('echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
|
||||
\\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'),
|
||||
15
|
||||
)
|
||||
|
||||
self.sensor = command_line.CommandSensor(self.hass, data, 'test',
|
||||
None, None, ['key',
|
||||
'another_key'])
|
||||
self.sensor.update()
|
||||
assert 'some_json_value' == \
|
||||
self.sensor.device_state_attributes['key']
|
||||
assert 'another_json_value' == \
|
||||
self.sensor.device_state_attributes['another_key']
|
||||
assert not ('key_three' in self.sensor.device_state_attributes)
|
||||
@@ -0,0 +1,200 @@
|
||||
"""The tests for the Command line switch platform."""
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.const import STATE_ON, STATE_OFF
|
||||
import homeassistant.components.switch as switch
|
||||
import homeassistant.components.command_line.switch as command_line
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.components.switch import common
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
class TestCommandSwitch(unittest.TestCase):
|
||||
"""Test the command switch."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_state_none(self):
|
||||
"""Test with none state."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'switch_status')
|
||||
test_switch = {
|
||||
'command_on': 'echo 1 > {}'.format(path),
|
||||
'command_off': 'echo 0 > {}'.format(path),
|
||||
}
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'command_line',
|
||||
'switches': {
|
||||
'test': test_switch
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
common.turn_on(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
common.turn_off(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
def test_state_value(self):
|
||||
"""Test with state value."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'switch_status')
|
||||
test_switch = {
|
||||
'command_state': 'cat {}'.format(path),
|
||||
'command_on': 'echo 1 > {}'.format(path),
|
||||
'command_off': 'echo 0 > {}'.format(path),
|
||||
'value_template': '{{ value=="1" }}'
|
||||
}
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'command_line',
|
||||
'switches': {
|
||||
'test': test_switch
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
common.turn_on(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
common.turn_off(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
def test_state_json_value(self):
|
||||
"""Test with state JSON value."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'switch_status')
|
||||
oncmd = json.dumps({'status': 'ok'})
|
||||
offcmd = json.dumps({'status': 'nope'})
|
||||
test_switch = {
|
||||
'command_state': 'cat {}'.format(path),
|
||||
'command_on': 'echo \'{}\' > {}'.format(oncmd, path),
|
||||
'command_off': 'echo \'{}\' > {}'.format(offcmd, path),
|
||||
'value_template': '{{ value_json.status=="ok" }}'
|
||||
}
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'command_line',
|
||||
'switches': {
|
||||
'test': test_switch
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
common.turn_on(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
common.turn_off(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
def test_state_code(self):
|
||||
"""Test with state code."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'switch_status')
|
||||
test_switch = {
|
||||
'command_state': 'cat {}'.format(path),
|
||||
'command_on': 'echo 1 > {}'.format(path),
|
||||
'command_off': 'echo 0 > {}'.format(path),
|
||||
}
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'command_line',
|
||||
'switches': {
|
||||
'test': test_switch
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
common.turn_on(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
common.turn_off(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
def test_assumed_state_should_be_true_if_command_state_is_none(self):
|
||||
"""Test with state value."""
|
||||
# args: hass, device_name, friendly_name, command_on, command_off,
|
||||
# command_state, value_template
|
||||
init_args = [
|
||||
self.hass,
|
||||
"test_device_name",
|
||||
"Test friendly name!",
|
||||
"echo 'on command'",
|
||||
"echo 'off command'",
|
||||
None,
|
||||
None,
|
||||
]
|
||||
|
||||
no_state_device = command_line.CommandSwitch(*init_args)
|
||||
assert no_state_device.assumed_state
|
||||
|
||||
# Set state command
|
||||
init_args[-2] = 'cat {}'
|
||||
|
||||
state_device = command_line.CommandSwitch(*init_args)
|
||||
assert not state_device.assumed_state
|
||||
|
||||
def test_entity_id_set_correctly(self):
|
||||
"""Test that entity_id is set correctly from object_id."""
|
||||
init_args = [
|
||||
self.hass,
|
||||
"test_device_name",
|
||||
"Test friendly name!",
|
||||
"echo 'on command'",
|
||||
"echo 'off command'",
|
||||
False,
|
||||
None,
|
||||
]
|
||||
|
||||
test_switch = command_line.CommandSwitch(*init_args)
|
||||
assert test_switch.entity_id == 'switch.test_device_name'
|
||||
assert test_switch.name == 'Test friendly name!'
|
||||
Reference in new issue
Block a user