def init_recorder(self): """Initialize the recorder.""" init_recorder_component(self.hass) self.hass.start() recorder.get_instance().block_till_db_ready() self.hass.block_till_done() recorder.get_instance().block_till_done()
def setup_recorder(config=None): """Set up with params.""" init_recorder_component(hass, config) hass.start() hass.block_till_done() hass.data[DATA_INSTANCE].block_till_done() return hass
def test_initialize_from_database_with_maxage(self): """Test initializing the statistics from the database.""" mock_data = { 'return_time': datetime(2017, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC), } def mock_now(): return mock_data['return_time'] # Testing correct retrieval from recorder, thus we do not # want purging to occur within the class itself. def mock_purge(self): return # Set maximum age to 3 hours. max_age = 3 # Determine what our minimum age should be based on test values. expected_min_age = mock_data['return_time'] + \ timedelta(hours=len(self.values) - max_age) # enable the recorder init_recorder_component(self.hass) with patch('homeassistant.components.sensor.statistics.dt_util.utcnow', new=mock_now), \ patch.object(StatisticsSensor, '_purge_old', mock_purge): # store some values for value in self.values: self.hass.states.set('sensor.test_monitored', value, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}) self.hass.block_till_done() # insert the next value 1 hour later mock_data['return_time'] += timedelta(hours=1) # wait for the recorder to really store the data self.hass.data[recorder.DATA_INSTANCE].block_till_done() # only now create the statistics component, so that it must read # the data from the database assert setup_component(self.hass, 'sensor', { 'sensor': { 'platform': 'statistics', 'name': 'test', 'entity_id': 'sensor.test_monitored', 'sampling_size': 100, 'max_age': {'hours': max_age} } }) self.hass.start() self.hass.block_till_done() # check if the result is as in test_sensor_source() state = self.hass.states.get('sensor.test_mean') assert expected_min_age == state.attributes.get('min_age') # The max_age timestamp should be 1 hour before what we have right # now in mock_data['return_time']. assert mock_data['return_time'] == state.attributes.get('max_age') +\ timedelta(hours=1)
def test_initialize_from_database(self): """Test initializing the statistics from the database.""" # enable the recorder init_recorder_component(self.hass) # store some values for value in self.values: self.hass.states.set('sensor.test_monitored', value, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}) self.hass.block_till_done() # wait for the recorder to really store the data self.hass.data[recorder.DATA_INSTANCE].block_till_done() # only now create the statistics component, so that it must read the # data from the database assert setup_component(self.hass, 'sensor', { 'sensor': { 'platform': 'statistics', 'name': 'test', 'entity_id': 'sensor.test_monitored', 'sampling_size': 100, } }) self.hass.start() self.hass.block_till_done() # check if the result is as in test_sensor_source() state = self.hass.states.get('sensor.test_mean') assert str(self.mean) == state.state
def test_filling_the_cache(): """Test filling the cache from the DB.""" test_entity_id1 = 'input_boolean.b1' test_entity_id2 = 'input_boolean.b2' hass = get_test_home_assistant() hass.state = CoreState.starting init_recorder_component(hass) _add_data_in_last_run(hass, { test_entity_id1: 'on', test_entity_id2: 'off', }) hass.block_till_done() setup_component(hass, input_boolean.DOMAIN, { input_boolean.DOMAIN: { 'b1': None, 'b2': None, }}) hass.start() state = hass.states.get('input_boolean.b1') assert state assert state.state == 'on' state = hass.states.get('input_boolean.b2') assert state assert state.state == 'off' hass.stop()
def test_load_from_db(self): """Test bootstrapping the brightness history from the database. This test can should only be executed if the loading of the history is enabled via plant.ENABLE_LOAD_HISTORY. """ init_recorder_component(self.hass) plant_name = 'wise_plant' for value in [20, 30, 10]: self.hass.states.set(BRIGHTNESS_ENTITY, value, {ATTR_UNIT_OF_MEASUREMENT: 'Lux'}) self.hass.block_till_done() # wait for the recorder to really store the data self.hass.data[recorder.DATA_INSTANCE].block_till_done() assert setup_component(self.hass, plant.DOMAIN, { plant.DOMAIN: { plant_name: GOOD_CONFIG } }) self.hass.block_till_done() state = self.hass.states.get('plant.'+plant_name) self.assertEqual(STATE_UNKNOWN, state.state) max_brightness = state.attributes.get( plant.ATTR_MAX_BRIGHTNESS_HISTORY) self.assertEqual(30, max_brightness)
def test_initialize_from_database(self): """Test initializing the statistics from the database.""" # enable the recorder init_recorder_component(self.hass) # store some values for value in self.values: self.hass.states.set('sensor.test_monitored', value, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}) self.hass.block_till_done() # wait for the recorder to really store the data self.hass.data[recorder.DATA_INSTANCE].block_till_done() # only now create the statistics component, so that it must read the # data from the database assert setup_component( self.hass, 'sensor', { 'sensor': { 'platform': 'statistics', 'name': 'test', 'entity_id': 'sensor.test_monitored', 'sampling_size': 100, } }) # check if the result is as in test_sensor_source() state = self.hass.states.get('sensor.test_mean') self.assertEqual(str(self.mean), state.state)
def test_filling_the_cache(): """Test filling the cache from the DB.""" test_entity_id1 = 'input_boolean.b1' test_entity_id2 = 'input_boolean.b2' hass = get_test_home_assistant() hass.state = CoreState.starting init_recorder_component(hass) _add_data_in_last_run(hass, { test_entity_id1: 'on', test_entity_id2: 'off', }) hass.block_till_done() setup_component(hass, input_boolean.DOMAIN, {input_boolean.DOMAIN: { 'b1': None, 'b2': None, }}) hass.start() state = hass.states.get('input_boolean.b1') assert state assert state.state == 'on' state = hass.states.get('input_boolean.b2') assert state assert state.state == 'off' hass.stop()
def test_load_from_db(self): """Test bootstrapping the brightness history from the database. This test can should only be executed if the loading of the history is enabled via plant.ENABLE_LOAD_HISTORY. """ init_recorder_component(self.hass) plant_name = "wise_plant" for value in [20, 30, 10]: self.hass.states.set(BRIGHTNESS_ENTITY, value, {ATTR_UNIT_OF_MEASUREMENT: "Lux"}) self.hass.block_till_done() # wait for the recorder to really store the data self.hass.data[recorder.DATA_INSTANCE].block_till_done() assert setup_component(self.hass, plant.DOMAIN, {plant.DOMAIN: { plant_name: GOOD_CONFIG }}) self.hass.block_till_done() state = self.hass.states.get("plant." + plant_name) assert STATE_UNKNOWN == state.state max_brightness = state.attributes.get( plant.ATTR_MAX_BRIGHTNESS_HISTORY) assert 30 == max_brightness
def setup_recorder(config=None): """Setup with params.""" init_recorder_component(hass, config) hass.start() hass.block_till_done() recorder.get_instance().block_till_done() return hass
def test_initialize_from_database(self): """Test initializing the statistics from the database.""" # enable the recorder init_recorder_component(self.hass) # store some values for value in self.values: self.hass.states.set( "sensor.test_monitored", value, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS} ) self.hass.block_till_done() # wait for the recorder to really store the data self.hass.data[recorder.DATA_INSTANCE].block_till_done() # only now create the statistics component, so that it must read the # data from the database assert setup_component( self.hass, "sensor", { "sensor": { "platform": "statistics", "name": "test", "entity_id": "sensor.test_monitored", "sampling_size": 100, } }, ) self.hass.start() self.hass.block_till_done() # check if the result is as in test_sensor_source() state = self.hass.states.get("sensor.test_mean") assert str(self.mean) == state.state
async def test_load_from_db(opp): """Test bootstrapping the brightness history from the database. This test can should only be executed if the loading of the history is enabled via plant.ENABLE_LOAD_HISTORY. """ init_recorder_component(opp) plant_name = "wise_plant" for value in [20, 30, 10]: opp.states.async_set( BRIGHTNESS_ENTITY, value, {ATTR_UNIT_OF_MEASUREMENT: "Lux"} ) await opp.async_block_till_done() # wait for the recorder to really store the data opp.data[recorder.DATA_INSTANCE].block_till_done() assert await async_setup_component( opp, plant.DOMAIN, {plant.DOMAIN: {plant_name: GOOD_CONFIG}} ) await opp.async_block_till_done() state = opp.states.get(f"plant.{plant_name}") assert state.state == STATE_UNKNOWN max_brightness = state.attributes.get(plant.ATTR_MAX_BRIGHTNESS_HISTORY) assert max_brightness == 30
def setup_recorder(config=None): """Set up with params.""" init_recorder_component(opp, config) opp.start() opp.block_till_done() opp.data[recorder.DATA_INSTANCE].block_till_done() return opp
def setUp(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() init_recorder_component(self.hass) # Force an in memory DB mock_http_component(self.hass) self.hass.config.components |= set(['frontend', 'recorder', 'api']) assert setup_component(self.hass, logbook.DOMAIN, self.EMPTY_CONFIG) self.hass.start()
def init_recorder(self): """Initialize the recorder.""" init_recorder_component(self.opp) self.opp.start()
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" config = {'purge_keep_days': 4, 'purge_interval': 2} self.hass = get_test_home_assistant() init_recorder_component(self.hass, config) self.hass.start()
def test_initialize_from_database_with_maxage(self): """Test initializing the statistics from the database.""" mock_data = { "return_time": datetime(2017, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC) } def mock_now(): return mock_data["return_time"] # Testing correct retrieval from recorder, thus we do not # want purging to occur within the class itself. def mock_purge(self): return # Set maximum age to 3 hours. max_age = 3 # Determine what our minimum age should be based on test values. expected_min_age = mock_data["return_time"] + timedelta( hours=len(self.values) - max_age ) # enable the recorder init_recorder_component(self.hass) with patch( "homeassistant.components.statistics.sensor.dt_util.utcnow", new=mock_now ), patch.object(StatisticsSensor, "_purge_old", mock_purge): # store some values for value in self.values: self.hass.states.set( "sensor.test_monitored", value, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}, ) self.hass.block_till_done() # insert the next value 1 hour later mock_data["return_time"] += timedelta(hours=1) # wait for the recorder to really store the data self.hass.data[recorder.DATA_INSTANCE].block_till_done() # only now create the statistics component, so that it must read # the data from the database assert setup_component( self.hass, "sensor", { "sensor": { "platform": "statistics", "name": "test", "entity_id": "sensor.test_monitored", "sampling_size": 100, "max_age": {"hours": max_age}, } }, ) self.hass.block_till_done() self.hass.start() self.hass.block_till_done() # check if the result is as in test_sensor_source() state = self.hass.states.get("sensor.test_mean") assert expected_min_age == state.attributes.get("min_age") # The max_age timestamp should be 1 hour before what we have right # now in mock_data['return_time']. assert mock_data["return_time"] == state.attributes.get("max_age") + timedelta( hours=1 )
def setUp(self): # pylint: disable=invalid-name """Set up things to be run when tests are started.""" self.hass = get_test_home_assistant() init_recorder_component(self.hass) self.hass.start()
def init_recorder(self): """Initialize the recorder.""" init_recorder_component(self.hass) self.hass.start() self.wait_recording_done()
def test_initialize_from_database_with_maxage(self): """Test initializing the statistics from the database.""" mock_data = { 'return_time': datetime(2017, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC), } def mock_now(): return mock_data['return_time'] # Testing correct retrieval from recorder, thus we do not # want purging to occur within the class itself. def mock_purge(self): return # Set maximum age to 3 hours. max_age = 3 # Determine what our minimum age should be based on test values. expected_min_age = mock_data['return_time'] + \ timedelta(hours=len(self.values) - max_age) # enable the recorder init_recorder_component(self.hass) with patch('homeassistant.components.sensor.statistics.dt_util.utcnow', new=mock_now), \ patch.object(StatisticsSensor, '_purge_old', mock_purge): # store some values for value in self.values: self.hass.states.set('sensor.test_monitored', value, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}) self.hass.block_till_done() # insert the next value 1 hour later mock_data['return_time'] += timedelta(hours=1) # wait for the recorder to really store the data self.hass.data[recorder.DATA_INSTANCE].block_till_done() # only now create the statistics component, so that it must read # the data from the database assert setup_component( self.hass, 'sensor', { 'sensor': { 'platform': 'statistics', 'name': 'test', 'entity_id': 'sensor.test_monitored', 'sampling_size': 100, 'max_age': { 'hours': max_age } } }) self.hass.block_till_done() self.hass.start() self.hass.block_till_done() # check if the result is as in test_sensor_source() state = self.hass.states.get('sensor.test_mean') assert expected_min_age == state.attributes.get('min_age') # The max_age timestamp should be 1 hour before what we have right # now in mock_data['return_time']. assert mock_data['return_time'] == state.attributes.get('max_age') +\ timedelta(hours=1)
def test_initialize_from_database_with_maxage(self): """Test initializing the statistics from the database.""" now = dt_util.utcnow() mock_data = { "return_time": datetime(now.year + 1, 8, 2, 12, 23, 42, tzinfo=dt_util.UTC) } def mock_now(): return mock_data["return_time"] # Testing correct retrieval from recorder, thus we do not # want purging to occur within the class itself. def mock_purge(self): return # enable the recorder init_recorder_component(self.hass) self.hass.block_till_done() self.hass.data[recorder.DATA_INSTANCE].block_till_done() with patch("homeassistant.components.statistics.sensor.dt_util.utcnow", new=mock_now), patch.object(StatisticsSensor, "_purge_old", mock_purge): # store some values for value in self.values: self.hass.states.set( "sensor.test_monitored", value, {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}, ) self.hass.block_till_done() # insert the next value 1 hour later mock_data["return_time"] += timedelta(hours=1) # wait for the recorder to really store the data wait_recording_done(self.hass) # only now create the statistics component, so that it must read # the data from the database assert setup_component( self.hass, "sensor", { "sensor": [ { "platform": "statistics", "name": "test", "entity_id": "sensor.test_monitored", "sampling_size": 100, "state_characteristic": "datetime_newest", "max_age": { "hours": 3 }, }, ] }, ) self.hass.block_till_done() self.hass.block_till_done() self.hass.start() self.hass.block_till_done() # check if the result is as in test_sensor_source() state = self.hass.states.get("sensor.test") assert state.attributes.get("age_coverage_ratio") == round(2 / 3, 2) # The max_age timestamp should be 1 hour before what we have right # now in mock_data['return_time']. assert mock_data["return_time"] == datetime.strptime( state.state, "%Y-%m-%d %H:%M:%S%z") + timedelta(hours=1)
def setUp(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() init_recorder_component(self.hass) # Force an in memory DB assert setup_component(self.hass, logbook.DOMAIN, self.EMPTY_CONFIG) self.hass.start()
def setUp(self): """Set up things to be run when tests are started.""" self.hass = get_test_home_assistant() init_recorder_component(self.hass) # Force an in memory DB assert setup_component(self.hass, logbook.DOMAIN, self.EMPTY_CONFIG) self.hass.start()
def init_recorder(self): """Initialize the recorder.""" init_recorder_component(self.hass) self.hass.start() wait_recording_done(self.hass)
def setUp(self): # pylint: disable=invalid-name """Set up things to be run when tests are started.""" self.opp = get_test_open_peer_power() init_recorder_component(self.opp) self.opp.start()