class TestRandomSensor(DatabaseTest): """ Tests basic sensor functionality with the simplest generated sensor. """ def setup(self): self.sensor = RandomValuesSensor(sensor_name="random") self.async_wait_time = 0.1 super().setup() def _test_random_value(self, values): eq_(len(values), 2) for val in values: ok_(isinstance(val, Sensor.value_type), "Type of values should be SensorDataValues, got {}".format( type(val))) ok_(abs(val.value) < 100, "Val should be between -100, 100, got {}".format(val)) def test_read_sensor(self): self._test_random_value(self.sensor.read()) def test_update_sensor_reads_sensor(self): self.sensor.update() sleep(self.async_wait_time) records = self.sensor.data eq_(len(records), 1) self._test_random_value(records[0].values) def test_update_sensor_adds_objects_to_db(self): eq_(len(self.sensor.data), 0) self.sensor.update() sleep(self.async_wait_time) records = self.session.query(Sensor). \ filter(Sensor.id == self.sensor.id).first().data eq_(len(records), 1) self._test_random_value(records[0].values) def test_current_value(self): self.sensor.update() sleep(self.async_wait_time) data = self.session.query(Sensor). \ filter(Sensor.id == self.sensor.id).first().data[0] curr_val = self.sensor.current_value for val in data.values: ok_(curr_val[val.name] == val.value, "curr_value {} ({}) should equal {}".format( val.name, curr_val[val.name], val.value)) def test_last_update(self): start = datetime.now() self.sensor.update() sleep(self.async_wait_time) ok_(self.sensor.last_update > start)
def setup(self): self.sensor = RandomValuesSensor(sensor_name="random") self.async_wait_time = 0.1 super().setup()