Fix plants calling async methods from sync context (#8200)

This commit is contained in:
Paulus Schoutsen 2017-06-25 10:07:28 -07:00 committed by GitHub
parent a082ffca1d
commit 1cfed4f015

View file

@ -1,23 +1,18 @@
"""Unit tests for platform/plant.py.""" """Unit tests for platform/plant.py."""
import asyncio
import unittest
from tests.common import get_test_home_assistant
import homeassistant.components.plant as plant import homeassistant.components.plant as plant
class TestPlant(unittest.TestCase): GOOD_DATA = {
"""test the processing of data."""
GOOD_DATA = {
'moisture': 50, 'moisture': 50,
'battery': 90, 'battery': 90,
'temperature': 23.4, 'temperature': 23.4,
'conductivity': 777, 'conductivity': 777,
'brightness': 987, 'brightness': 987,
} }
GOOD_CONFIG = { GOOD_CONFIG = {
'sensors': { 'sensors': {
'moisture': 'sensor.mqtt_plant_moisture', 'moisture': 'sensor.mqtt_plant_moisture',
'battery': 'sensor.mqtt_plant_battery', 'battery': 'sensor.mqtt_plant_battery',
@ -30,44 +25,39 @@ class TestPlant(unittest.TestCase):
'min_battery': 17, 'min_battery': 17,
'min_conductivity': 500, 'min_conductivity': 500,
'min_temperature': 15, 'min_temperature': 15,
} }
class _MockState(object):
class _MockState(object):
def __init__(self, state=None): def __init__(self, state=None):
self.state = state self.state = state
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
def tearDown(self): @asyncio.coroutine
"""Stop everything that was started.""" def test_valid_data(hass):
self.hass.stop()
def test_valid_data(self):
"""Test processing valid data.""" """Test processing valid data."""
self.sensor = plant.Plant('my plant', self.GOOD_CONFIG) sensor = plant.Plant('my plant', GOOD_CONFIG)
self.sensor.hass = self.hass sensor.hass = hass
for reading, value in self.GOOD_DATA.items(): for reading, value in GOOD_DATA.items():
self.sensor.state_changed( sensor.state_changed(
self.GOOD_CONFIG['sensors'][reading], None, GOOD_CONFIG['sensors'][reading], None,
TestPlant._MockState(value)) _MockState(value))
self.assertEqual(self.sensor.state, 'ok') assert sensor.state == 'ok'
attrib = self.sensor.state_attributes attrib = sensor.state_attributes
for reading, value in self.GOOD_DATA.items(): for reading, value in GOOD_DATA.items():
# battery level has a different name in # battery level has a different name in
# the JSON format than in hass # the JSON format than in hass
self.assertEqual(attrib[reading], value) assert attrib[reading] == value
def test_low_battery(self):
@asyncio.coroutine
def test_low_battery(hass):
"""Test processing with low battery data and limit set.""" """Test processing with low battery data and limit set."""
self.sensor = plant.Plant(self.hass, self.GOOD_CONFIG) sensor = plant.Plant(hass, GOOD_CONFIG)
self.sensor.hass = self.hass sensor.hass = hass
self.assertEqual(self.sensor.state_attributes['problem'], 'none') assert sensor.state_attributes['problem'] == 'none'
self.sensor.state_changed('sensor.mqtt_plant_battery', sensor.state_changed('sensor.mqtt_plant_battery',
TestPlant._MockState(45), _MockState(45), _MockState(10))
TestPlant._MockState(10)) assert sensor.state == 'problem'
self.assertEqual(self.sensor.state, 'problem') assert sensor.state_attributes['problem'] == 'battery low'
self.assertEqual(self.sensor.state_attributes['problem'],
'battery low')