From 1cfed4f01560136ef44e0dc6c417f07b540a8851 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 25 Jun 2017 10:07:28 -0700 Subject: [PATCH] Fix plants calling async methods from sync context (#8200) --- tests/components/test_plant.py | 110 +++++++++++++++------------------ 1 file changed, 50 insertions(+), 60 deletions(-) diff --git a/tests/components/test_plant.py b/tests/components/test_plant.py index 57339f28941..70641afd6b4 100644 --- a/tests/components/test_plant.py +++ b/tests/components/test_plant.py @@ -1,73 +1,63 @@ """Unit tests for platform/plant.py.""" +import asyncio -import unittest - -from tests.common import get_test_home_assistant import homeassistant.components.plant as plant -class TestPlant(unittest.TestCase): - """test the processing of data.""" +GOOD_DATA = { + 'moisture': 50, + 'battery': 90, + 'temperature': 23.4, + 'conductivity': 777, + 'brightness': 987, +} - GOOD_DATA = { - 'moisture': 50, - 'battery': 90, - 'temperature': 23.4, - 'conductivity': 777, - 'brightness': 987, - } +GOOD_CONFIG = { + 'sensors': { + 'moisture': 'sensor.mqtt_plant_moisture', + 'battery': 'sensor.mqtt_plant_battery', + 'temperature': 'sensor.mqtt_plant_temperature', + 'conductivity': 'sensor.mqtt_plant_conductivity', + 'brightness': 'sensor.mqtt_plant_brightness', + }, + 'min_moisture': 20, + 'max_moisture': 60, + 'min_battery': 17, + 'min_conductivity': 500, + 'min_temperature': 15, +} - GOOD_CONFIG = { - 'sensors': { - 'moisture': 'sensor.mqtt_plant_moisture', - 'battery': 'sensor.mqtt_plant_battery', - 'temperature': 'sensor.mqtt_plant_temperature', - 'conductivity': 'sensor.mqtt_plant_conductivity', - 'brightness': 'sensor.mqtt_plant_brightness', - }, - 'min_moisture': 20, - 'max_moisture': 60, - 'min_battery': 17, - 'min_conductivity': 500, - 'min_temperature': 15, - } - class _MockState(object): +class _MockState(object): - def __init__(self, state=None): - self.state = state + def __init__(self, state=None): + self.state = state - def setUp(self): - """Setup 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() +@asyncio.coroutine +def test_valid_data(hass): + """Test processing valid data.""" + sensor = plant.Plant('my plant', GOOD_CONFIG) + sensor.hass = hass + for reading, value in GOOD_DATA.items(): + sensor.state_changed( + GOOD_CONFIG['sensors'][reading], None, + _MockState(value)) + assert sensor.state == 'ok' + attrib = sensor.state_attributes + for reading, value in GOOD_DATA.items(): + # battery level has a different name in + # the JSON format than in hass + assert attrib[reading] == value - def test_valid_data(self): - """Test processing valid data.""" - self.sensor = plant.Plant('my plant', self.GOOD_CONFIG) - self.sensor.hass = self.hass - for reading, value in self.GOOD_DATA.items(): - self.sensor.state_changed( - self.GOOD_CONFIG['sensors'][reading], None, - TestPlant._MockState(value)) - self.assertEqual(self.sensor.state, 'ok') - attrib = self.sensor.state_attributes - for reading, value in self.GOOD_DATA.items(): - # battery level has a different name in - # the JSON format than in hass - self.assertEqual(attrib[reading], value) - def test_low_battery(self): - """Test processing with low battery data and limit set.""" - self.sensor = plant.Plant(self.hass, self.GOOD_CONFIG) - self.sensor.hass = self.hass - self.assertEqual(self.sensor.state_attributes['problem'], 'none') - self.sensor.state_changed('sensor.mqtt_plant_battery', - TestPlant._MockState(45), - TestPlant._MockState(10)) - self.assertEqual(self.sensor.state, 'problem') - self.assertEqual(self.sensor.state_attributes['problem'], - 'battery low') +@asyncio.coroutine +def test_low_battery(hass): + """Test processing with low battery data and limit set.""" + sensor = plant.Plant(hass, GOOD_CONFIG) + sensor.hass = hass + assert sensor.state_attributes['problem'] == 'none' + sensor.state_changed('sensor.mqtt_plant_battery', + _MockState(45), _MockState(10)) + assert sensor.state == 'problem' + assert sensor.state_attributes['problem'] == 'battery low'