This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+93 -79
View File
@@ -41,93 +41,104 @@ class TestRestBinarySensorSetup(unittest.TestCase):
def test_setup_missing_config(self):
"""Test setup with configuration missing required entries."""
with assert_setup_component(0):
assert setup_component(self.hass, binary_sensor.DOMAIN, {
'binary_sensor': {'platform': 'rest'}})
assert setup_component(
self.hass, binary_sensor.DOMAIN, {"binary_sensor": {"platform": "rest"}}
)
def test_setup_missing_schema(self):
"""Test setup with resource missing schema."""
with pytest.raises(MissingSchema):
rest.setup_platform(self.hass, {
'platform': 'rest',
'resource': 'localhost',
'method': 'GET'
}, None)
rest.setup_platform(
self.hass,
{"platform": "rest", "resource": "localhost", "method": "GET"},
None,
)
@patch('requests.Session.send',
side_effect=requests.exceptions.ConnectionError())
@patch("requests.Session.send", side_effect=requests.exceptions.ConnectionError())
def test_setup_failed_connect(self, mock_req):
"""Test setup when connection error occurs."""
with raises(PlatformNotReady):
rest.setup_platform(self.hass, {
'platform': 'rest',
'resource': 'http://localhost',
}, self.add_devices, None)
rest.setup_platform(
self.hass,
{"platform": "rest", "resource": "http://localhost"},
self.add_devices,
None,
)
assert len(self.DEVICES) == 0
@patch('requests.Session.send', side_effect=Timeout())
@patch("requests.Session.send", side_effect=Timeout())
def test_setup_timeout(self, mock_req):
"""Test setup when connection timeout occurs."""
with raises(PlatformNotReady):
rest.setup_platform(self.hass, {
'platform': 'rest',
'resource': 'http://localhost',
}, self.add_devices, None)
rest.setup_platform(
self.hass,
{"platform": "rest", "resource": "http://localhost"},
self.add_devices,
None,
)
assert len(self.DEVICES) == 0
@requests_mock.Mocker()
def test_setup_minimum(self, mock_req):
"""Test setup with minimum configuration."""
mock_req.get('http://localhost', status_code=200)
with assert_setup_component(1, 'binary_sensor'):
assert setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'rest',
'resource': 'http://localhost'
}
})
mock_req.get("http://localhost", status_code=200)
with assert_setup_component(1, "binary_sensor"):
assert setup_component(
self.hass,
"binary_sensor",
{"binary_sensor": {"platform": "rest", "resource": "http://localhost"}},
)
assert 1 == mock_req.call_count
@requests_mock.Mocker()
def test_setup_get(self, mock_req):
"""Test setup with valid configuration."""
mock_req.get('http://localhost', status_code=200)
with assert_setup_component(1, 'binary_sensor'):
assert setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'rest',
'resource': 'http://localhost',
'method': 'GET',
'value_template': '{{ value_json.key }}',
'name': 'foo',
'verify_ssl': 'true',
'authentication': 'basic',
'username': 'my username',
'password': 'my password',
'headers': {'Accept': 'application/json'}
}
})
mock_req.get("http://localhost", status_code=200)
with assert_setup_component(1, "binary_sensor"):
assert setup_component(
self.hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "rest",
"resource": "http://localhost",
"method": "GET",
"value_template": "{{ value_json.key }}",
"name": "foo",
"verify_ssl": "true",
"authentication": "basic",
"username": "my username",
"password": "my password",
"headers": {"Accept": "application/json"},
}
},
)
assert 1 == mock_req.call_count
@requests_mock.Mocker()
def test_setup_post(self, mock_req):
"""Test setup with valid configuration."""
mock_req.post('http://localhost', status_code=200)
with assert_setup_component(1, 'binary_sensor'):
assert setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'rest',
'resource': 'http://localhost',
'method': 'POST',
'value_template': '{{ value_json.key }}',
'payload': '{ "device": "toaster"}',
'name': 'foo',
'verify_ssl': 'true',
'authentication': 'basic',
'username': 'my username',
'password': 'my password',
'headers': {'Accept': 'application/json'}
}
})
mock_req.post("http://localhost", status_code=200)
with assert_setup_component(1, "binary_sensor"):
assert setup_component(
self.hass,
"binary_sensor",
{
"binary_sensor": {
"platform": "rest",
"resource": "http://localhost",
"method": "POST",
"value_template": "{{ value_json.key }}",
"payload": '{ "device": "toaster"}',
"name": "foo",
"verify_ssl": "true",
"authentication": "basic",
"username": "my username",
"password": "my password",
"headers": {"Accept": "application/json"},
}
},
)
assert 1 == mock_req.call_count
@@ -137,18 +148,17 @@ class TestRestBinarySensor(unittest.TestCase):
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.rest = Mock('RestData')
self.rest.update = Mock('RestData.update',
side_effect=self.update_side_effect(
'{ "key": false }'))
self.name = 'foo'
self.device_class = 'light'
self.value_template = \
template.Template('{{ value_json.key }}', self.hass)
self.rest = Mock("RestData")
self.rest.update = Mock(
"RestData.update", side_effect=self.update_side_effect('{ "key": false }')
)
self.name = "foo"
self.device_class = "light"
self.value_template = template.Template("{{ value_json.key }}", self.hass)
self.binary_sensor = rest.RestBinarySensor(
self.hass, self.rest, self.name, self.device_class,
self.value_template)
self.hass, self.rest, self.name, self.device_class, self.value_template
)
def tearDown(self):
"""Stop everything that was started."""
@@ -174,33 +184,37 @@ class TestRestBinarySensor(unittest.TestCase):
def test_update_when_value_is_none(self):
"""Test state gets updated to unknown when sensor returns no data."""
self.rest.update = Mock(
'RestData.update',
side_effect=self.update_side_effect(None))
"RestData.update", side_effect=self.update_side_effect(None)
)
self.binary_sensor.update()
assert not self.binary_sensor.available
def test_update_when_value_changed(self):
"""Test state gets updated when sensor returns a new status."""
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect(
'{ "key": true }'))
self.rest.update = Mock(
"rest.RestData.update",
side_effect=self.update_side_effect('{ "key": true }'),
)
self.binary_sensor.update()
assert STATE_ON == self.binary_sensor.state
assert self.binary_sensor.available
def test_update_when_failed_request(self):
"""Test state gets updated when sensor returns a new status."""
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect(None))
self.rest.update = Mock(
"rest.RestData.update", side_effect=self.update_side_effect(None)
)
self.binary_sensor.update()
assert not self.binary_sensor.available
def test_update_with_no_template(self):
"""Test update when there is no value template."""
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect('true'))
self.rest.update = Mock(
"rest.RestData.update", side_effect=self.update_side_effect("true")
)
self.binary_sensor = rest.RestBinarySensor(
self.hass, self.rest, self.name, self.device_class, None)
self.hass, self.rest, self.name, self.device_class, None
)
self.binary_sensor.update()
assert STATE_ON == self.binary_sensor.state
assert self.binary_sensor.available
+196 -135
View File
@@ -31,95 +31,104 @@ class TestRestSensorSetup(unittest.TestCase):
def test_setup_missing_config(self):
"""Test setup with configuration missing required entries."""
with assert_setup_component(0):
assert setup_component(self.hass, sensor.DOMAIN, {
'sensor': {'platform': 'rest'}})
assert setup_component(
self.hass, sensor.DOMAIN, {"sensor": {"platform": "rest"}}
)
def test_setup_missing_schema(self):
"""Test setup with resource missing schema."""
with pytest.raises(MissingSchema):
rest.setup_platform(self.hass, {
'platform': 'rest',
'resource': 'localhost',
'method': 'GET'
}, None)
rest.setup_platform(
self.hass,
{"platform": "rest", "resource": "localhost", "method": "GET"},
None,
)
@patch('requests.Session.send',
side_effect=requests.exceptions.ConnectionError())
@patch("requests.Session.send", side_effect=requests.exceptions.ConnectionError())
def test_setup_failed_connect(self, mock_req):
"""Test setup when connection error occurs."""
with raises(PlatformNotReady):
rest.setup_platform(self.hass, {
'platform': 'rest',
'resource': 'http://localhost',
}, lambda devices, update=True: None)
rest.setup_platform(
self.hass,
{"platform": "rest", "resource": "http://localhost"},
lambda devices, update=True: None,
)
@patch('requests.Session.send', side_effect=Timeout())
@patch("requests.Session.send", side_effect=Timeout())
def test_setup_timeout(self, mock_req):
"""Test setup when connection timeout occurs."""
with raises(PlatformNotReady):
rest.setup_platform(self.hass, {
'platform': 'rest',
'resource': 'http://localhost',
}, lambda devices, update=True: None)
rest.setup_platform(
self.hass,
{"platform": "rest", "resource": "http://localhost"},
lambda devices, update=True: None,
)
@requests_mock.Mocker()
def test_setup_minimum(self, mock_req):
"""Test setup with minimum configuration."""
mock_req.get('http://localhost', status_code=200)
with assert_setup_component(1, 'sensor'):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'rest',
'resource': 'http://localhost'
}
})
mock_req.get("http://localhost", status_code=200)
with assert_setup_component(1, "sensor"):
assert setup_component(
self.hass,
"sensor",
{"sensor": {"platform": "rest", "resource": "http://localhost"}},
)
assert 2 == mock_req.call_count
@requests_mock.Mocker()
def test_setup_get(self, mock_req):
"""Test setup with valid configuration."""
mock_req.get('http://localhost', status_code=200)
with assert_setup_component(1, 'sensor'):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'rest',
'resource': 'http://localhost',
'method': 'GET',
'value_template': '{{ value_json.key }}',
'name': 'foo',
'unit_of_measurement': 'MB',
'verify_ssl': 'true',
'timeout': 30,
'authentication': 'basic',
'username': 'my username',
'password': 'my password',
'headers': {'Accept': 'application/json'}
}
})
mock_req.get("http://localhost", status_code=200)
with assert_setup_component(1, "sensor"):
assert setup_component(
self.hass,
"sensor",
{
"sensor": {
"platform": "rest",
"resource": "http://localhost",
"method": "GET",
"value_template": "{{ value_json.key }}",
"name": "foo",
"unit_of_measurement": "MB",
"verify_ssl": "true",
"timeout": 30,
"authentication": "basic",
"username": "my username",
"password": "my password",
"headers": {"Accept": "application/json"},
}
},
)
assert 2 == mock_req.call_count
@requests_mock.Mocker()
def test_setup_post(self, mock_req):
"""Test setup with valid configuration."""
mock_req.post('http://localhost', status_code=200)
with assert_setup_component(1, 'sensor'):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'rest',
'resource': 'http://localhost',
'method': 'POST',
'value_template': '{{ value_json.key }}',
'payload': '{ "device": "toaster"}',
'name': 'foo',
'unit_of_measurement': 'MB',
'verify_ssl': 'true',
'timeout': 30,
'authentication': 'basic',
'username': 'my username',
'password': 'my password',
'headers': {'Accept': 'application/json'}
}
})
mock_req.post("http://localhost", status_code=200)
with assert_setup_component(1, "sensor"):
assert setup_component(
self.hass,
"sensor",
{
"sensor": {
"platform": "rest",
"resource": "http://localhost",
"method": "POST",
"value_template": "{{ value_json.key }}",
"payload": '{ "device": "toaster"}',
"name": "foo",
"unit_of_measurement": "MB",
"verify_ssl": "true",
"timeout": 30,
"authentication": "basic",
"username": "my username",
"password": "my password",
"headers": {"Accept": "application/json"},
}
},
)
assert 2 == mock_req.call_count
@@ -129,21 +138,30 @@ class TestRestSensor(unittest.TestCase):
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.initial_state = 'initial_state'
self.rest = Mock('rest.RestData')
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect(
'{ "key": "' + self.initial_state + '" }'))
self.name = 'foo'
self.unit_of_measurement = 'MB'
self.initial_state = "initial_state"
self.rest = Mock("rest.RestData")
self.rest.update = Mock(
"rest.RestData.update",
side_effect=self.update_side_effect(
'{ "key": "' + self.initial_state + '" }'
),
)
self.name = "foo"
self.unit_of_measurement = "MB"
self.device_class = None
self.value_template = template('{{ value_json.key }}')
self.value_template = template("{{ value_json.key }}")
self.value_template.hass = self.hass
self.force_update = False
self.sensor = rest.RestSensor(
self.hass, self.rest, self.name, self.unit_of_measurement,
self.device_class, self.value_template, [], self.force_update
self.hass,
self.rest,
self.name,
self.unit_of_measurement,
self.device_class,
self.value_template,
[],
self.force_update,
)
def tearDown(self):
@@ -174,83 +192,118 @@ class TestRestSensor(unittest.TestCase):
def test_update_when_value_is_none(self):
"""Test state gets updated to unknown when sensor returns no data."""
self.rest.update = Mock(
'rest.RestData.update', side_effect=self.update_side_effect(None))
"rest.RestData.update", side_effect=self.update_side_effect(None)
)
self.sensor.update()
assert self.sensor.state is None
assert not self.sensor.available
def test_update_when_value_changed(self):
"""Test state gets updated when sensor returns a new status."""
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect(
'{ "key": "updated_state" }'))
self.rest.update = Mock(
"rest.RestData.update",
side_effect=self.update_side_effect('{ "key": "updated_state" }'),
)
self.sensor.update()
assert 'updated_state' == self.sensor.state
assert "updated_state" == self.sensor.state
assert self.sensor.available
def test_update_with_no_template(self):
"""Test update when there is no value template."""
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect(
'plain_state'))
self.sensor = rest.RestSensor(self.hass, self.rest, self.name,
self.unit_of_measurement,
self.device_class, None, [],
self.force_update)
self.rest.update = Mock(
"rest.RestData.update", side_effect=self.update_side_effect("plain_state")
)
self.sensor = rest.RestSensor(
self.hass,
self.rest,
self.name,
self.unit_of_measurement,
self.device_class,
None,
[],
self.force_update,
)
self.sensor.update()
assert 'plain_state' == self.sensor.state
assert "plain_state" == self.sensor.state
assert self.sensor.available
def test_update_with_json_attrs(self):
"""Test attributes get extracted from a JSON result."""
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect(
'{ "key": "some_json_value" }'))
self.sensor = rest.RestSensor(self.hass, self.rest, self.name,
self.unit_of_measurement,
self.device_class, None, ['key'],
self.force_update)
self.rest.update = Mock(
"rest.RestData.update",
side_effect=self.update_side_effect('{ "key": "some_json_value" }'),
)
self.sensor = rest.RestSensor(
self.hass,
self.rest,
self.name,
self.unit_of_measurement,
self.device_class,
None,
["key"],
self.force_update,
)
self.sensor.update()
assert 'some_json_value' == \
self.sensor.device_state_attributes['key']
assert "some_json_value" == self.sensor.device_state_attributes["key"]
@patch('homeassistant.components.rest.sensor._LOGGER')
@patch("homeassistant.components.rest.sensor._LOGGER")
def test_update_with_json_attrs_no_data(self, mock_logger):
"""Test attributes when no JSON result fetched."""
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect(None))
self.sensor = rest.RestSensor(self.hass, self.rest, self.name,
self.unit_of_measurement,
self.device_class, None, ['key'],
self.force_update)
self.rest.update = Mock(
"rest.RestData.update", side_effect=self.update_side_effect(None)
)
self.sensor = rest.RestSensor(
self.hass,
self.rest,
self.name,
self.unit_of_measurement,
self.device_class,
None,
["key"],
self.force_update,
)
self.sensor.update()
assert {} == self.sensor.device_state_attributes
assert mock_logger.warning.called
@patch('homeassistant.components.rest.sensor._LOGGER')
@patch("homeassistant.components.rest.sensor._LOGGER")
def test_update_with_json_attrs_not_dict(self, mock_logger):
"""Test attributes get extracted from a JSON result."""
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect(
'["list", "of", "things"]'))
self.sensor = rest.RestSensor(self.hass, self.rest, self.name,
self.unit_of_measurement,
self.device_class, None, ['key'],
self.force_update)
self.rest.update = Mock(
"rest.RestData.update",
side_effect=self.update_side_effect('["list", "of", "things"]'),
)
self.sensor = rest.RestSensor(
self.hass,
self.rest,
self.name,
self.unit_of_measurement,
self.device_class,
None,
["key"],
self.force_update,
)
self.sensor.update()
assert {} == self.sensor.device_state_attributes
assert mock_logger.warning.called
@patch('homeassistant.components.rest.sensor._LOGGER')
@patch("homeassistant.components.rest.sensor._LOGGER")
def test_update_with_json_attrs_bad_JSON(self, mock_logger):
"""Test attributes get extracted from a JSON result."""
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect(
'This is text rather than JSON data.'))
self.sensor = rest.RestSensor(self.hass, self.rest, self.name,
self.unit_of_measurement,
self.device_class, None, ['key'],
self.force_update)
self.rest.update = Mock(
"rest.RestData.update",
side_effect=self.update_side_effect("This is text rather than JSON data."),
)
self.sensor = rest.RestSensor(
self.hass,
self.rest,
self.name,
self.unit_of_measurement,
self.device_class,
None,
["key"],
self.force_update,
)
self.sensor.update()
assert {} == self.sensor.device_state_attributes
assert mock_logger.warning.called
@@ -258,20 +311,28 @@ class TestRestSensor(unittest.TestCase):
def test_update_with_json_attrs_and_template(self):
"""Test attributes get extracted from a JSON result."""
self.rest.update = Mock('rest.RestData.update',
side_effect=self.update_side_effect(
'{ "key": "json_state_updated_value" }'))
self.sensor = rest.RestSensor(self.hass, self.rest, self.name,
self.unit_of_measurement,
self.device_class,
self.value_template, ['key'],
self.force_update)
self.rest.update = Mock(
"rest.RestData.update",
side_effect=self.update_side_effect(
'{ "key": "json_state_updated_value" }'
),
)
self.sensor = rest.RestSensor(
self.hass,
self.rest,
self.name,
self.unit_of_measurement,
self.device_class,
self.value_template,
["key"],
self.force_update,
)
self.sensor.update()
assert 'json_state_updated_value' == self.sensor.state
assert 'json_state_updated_value' == \
self.sensor.device_state_attributes['key'], \
self.force_update
assert "json_state_updated_value" == self.sensor.state
assert (
"json_state_updated_value" == self.sensor.device_state_attributes["key"]
), self.force_update
class TestRestData(unittest.TestCase):
@@ -284,17 +345,17 @@ class TestRestData(unittest.TestCase):
self.verify_ssl = True
self.timeout = 10
self.rest = rest.RestData(
self.method, self.resource, None, None, None, self.verify_ssl,
self.timeout)
self.method, self.resource, None, None, None, self.verify_ssl, self.timeout
)
@requests_mock.Mocker()
def test_update(self, mock_req):
"""Test update."""
mock_req.get('http://localhost', text='test data')
mock_req.get("http://localhost", text="test data")
self.rest.update()
assert 'test data' == self.rest.data
assert "test data" == self.rest.data
@patch('requests.Session', side_effect=RequestException)
@patch("requests.Session", side_effect=RequestException)
def test_update_request_exception(self, mock_req):
"""Test update when a request exception occurs."""
self.rest.update()
+71 -78
View File
@@ -24,71 +24,69 @@ class TestRestSwitchSetup:
def test_setup_missing_config(self):
"""Test setup with configuration missing required entries."""
assert not run_coroutine_threadsafe(
rest.async_setup_platform(self.hass, {
'platform': 'rest'
}, None),
self.hass.loop
rest.async_setup_platform(self.hass, {"platform": "rest"}, None),
self.hass.loop,
).result()
def test_setup_missing_schema(self):
"""Test setup with resource missing schema."""
assert not run_coroutine_threadsafe(
rest.async_setup_platform(self.hass, {
'platform': 'rest',
'resource': 'localhost'
}, None),
self.hass.loop
rest.async_setup_platform(
self.hass, {"platform": "rest", "resource": "localhost"}, None
),
self.hass.loop,
).result()
def test_setup_failed_connect(self, aioclient_mock):
"""Test setup when connection error occurs."""
aioclient_mock.get('http://localhost', exc=aiohttp.ClientError)
aioclient_mock.get("http://localhost", exc=aiohttp.ClientError)
assert not run_coroutine_threadsafe(
rest.async_setup_platform(self.hass, {
'platform': 'rest',
'resource': 'http://localhost',
}, None),
self.hass.loop
rest.async_setup_platform(
self.hass, {"platform": "rest", "resource": "http://localhost"}, None
),
self.hass.loop,
).result()
def test_setup_timeout(self, aioclient_mock):
"""Test setup when connection timeout occurs."""
aioclient_mock.get('http://localhost', exc=asyncio.TimeoutError())
aioclient_mock.get("http://localhost", exc=asyncio.TimeoutError())
assert not run_coroutine_threadsafe(
rest.async_setup_platform(self.hass, {
'platform': 'rest',
'resource': 'http://localhost',
}, None),
self.hass.loop
rest.async_setup_platform(
self.hass, {"platform": "rest", "resource": "http://localhost"}, None
),
self.hass.loop,
).result()
def test_setup_minimum(self, aioclient_mock):
"""Test setup with minimum configuration."""
aioclient_mock.get('http://localhost', status=200)
with assert_setup_component(1, 'switch'):
assert setup_component(self.hass, 'switch', {
'switch': {
'platform': 'rest',
'resource': 'http://localhost'
}
})
aioclient_mock.get("http://localhost", status=200)
with assert_setup_component(1, "switch"):
assert setup_component(
self.hass,
"switch",
{"switch": {"platform": "rest", "resource": "http://localhost"}},
)
assert aioclient_mock.call_count == 1
def test_setup(self, aioclient_mock):
"""Test setup with valid configuration."""
aioclient_mock.get('http://localhost', status=200)
assert setup_component(self.hass, 'switch', {
'switch': {
'platform': 'rest',
'name': 'foo',
'resource': 'http://localhost',
'headers': {'Content-type': 'application/json'},
'body_on': 'custom on text',
'body_off': 'custom off text',
}
})
aioclient_mock.get("http://localhost", status=200)
assert setup_component(
self.hass,
"switch",
{
"switch": {
"platform": "rest",
"name": "foo",
"resource": "http://localhost",
"headers": {"Content-type": "application/json"},
"body_on": "custom on text",
"body_off": "custom off text",
}
},
)
assert aioclient_mock.call_count == 1
assert_setup_component(1, 'switch')
assert_setup_component(1, "switch")
class TestRestSwitch:
@@ -97,16 +95,25 @@ class TestRestSwitch:
def setup_method(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.name = 'foo'
self.method = 'post'
self.resource = 'http://localhost/'
self.headers = {'Content-type': 'application/json'}
self.name = "foo"
self.method = "post"
self.resource = "http://localhost/"
self.headers = {"Content-type": "application/json"}
self.auth = None
self.body_on = Template('on', self.hass)
self.body_off = Template('off', self.hass)
self.body_on = Template("on", self.hass)
self.body_off = Template("off", self.hass)
self.switch = rest.RestSwitch(
self.name, self.resource, self.method, self.headers, self.auth,
self.body_on, self.body_off, None, 10, True)
self.name,
self.resource,
self.method,
self.headers,
self.auth,
self.body_on,
self.body_off,
None,
10,
True,
)
self.switch.hass = self.hass
def teardown_method(self):
@@ -124,87 +131,73 @@ class TestRestSwitch:
def test_turn_on_success(self, aioclient_mock):
"""Test turn_on."""
aioclient_mock.post(self.resource, status=200)
run_coroutine_threadsafe(
self.switch.async_turn_on(), self.hass.loop).result()
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
assert self.body_on.template == \
aioclient_mock.mock_calls[-1][2].decode()
assert self.body_on.template == aioclient_mock.mock_calls[-1][2].decode()
assert self.switch.is_on
def test_turn_on_status_not_ok(self, aioclient_mock):
"""Test turn_on when error status returned."""
aioclient_mock.post(self.resource, status=500)
run_coroutine_threadsafe(
self.switch.async_turn_on(), self.hass.loop).result()
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
assert self.body_on.template == \
aioclient_mock.mock_calls[-1][2].decode()
assert self.body_on.template == aioclient_mock.mock_calls[-1][2].decode()
assert self.switch.is_on is None
def test_turn_on_timeout(self, aioclient_mock):
"""Test turn_on when timeout occurs."""
aioclient_mock.post(self.resource, status=500)
run_coroutine_threadsafe(
self.switch.async_turn_on(), self.hass.loop).result()
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
assert self.switch.is_on is None
def test_turn_off_success(self, aioclient_mock):
"""Test turn_off."""
aioclient_mock.post(self.resource, status=200)
run_coroutine_threadsafe(
self.switch.async_turn_off(), self.hass.loop).result()
run_coroutine_threadsafe(self.switch.async_turn_off(), self.hass.loop).result()
assert self.body_off.template == \
aioclient_mock.mock_calls[-1][2].decode()
assert self.body_off.template == aioclient_mock.mock_calls[-1][2].decode()
assert not self.switch.is_on
def test_turn_off_status_not_ok(self, aioclient_mock):
"""Test turn_off when error status returned."""
aioclient_mock.post(self.resource, status=500)
run_coroutine_threadsafe(
self.switch.async_turn_off(), self.hass.loop).result()
run_coroutine_threadsafe(self.switch.async_turn_off(), self.hass.loop).result()
assert self.body_off.template == \
aioclient_mock.mock_calls[-1][2].decode()
assert self.body_off.template == aioclient_mock.mock_calls[-1][2].decode()
assert self.switch.is_on is None
def test_turn_off_timeout(self, aioclient_mock):
"""Test turn_off when timeout occurs."""
aioclient_mock.post(self.resource, exc=asyncio.TimeoutError())
run_coroutine_threadsafe(
self.switch.async_turn_on(), self.hass.loop).result()
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
assert self.switch.is_on is None
def test_update_when_on(self, aioclient_mock):
"""Test update when switch is on."""
aioclient_mock.get(self.resource, text=self.body_on.template)
run_coroutine_threadsafe(
self.switch.async_update(), self.hass.loop).result()
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
assert self.switch.is_on
def test_update_when_off(self, aioclient_mock):
"""Test update when switch is off."""
aioclient_mock.get(self.resource, text=self.body_off.template)
run_coroutine_threadsafe(
self.switch.async_update(), self.hass.loop).result()
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
assert not self.switch.is_on
def test_update_when_unknown(self, aioclient_mock):
"""Test update when unknown status returned."""
aioclient_mock.get(self.resource, text='unknown status')
run_coroutine_threadsafe(
self.switch.async_update(), self.hass.loop).result()
aioclient_mock.get(self.resource, text="unknown status")
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
assert self.switch.is_on is None
def test_update_timeout(self, aioclient_mock):
"""Test update when timeout occurs."""
aioclient_mock.get(self.resource, exc=asyncio.TimeoutError())
run_coroutine_threadsafe(
self.switch.async_update(), self.hass.loop).result()
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
assert self.switch.is_on is None