async def test_run_history(hass, recorder_mock): """Test the run history gives the correct run.""" instance = recorder.get_instance(hass) now = dt_util.utcnow() three_days_ago = now - timedelta(days=3) two_days_ago = now - timedelta(days=2) one_day_ago = now - timedelta(days=1) with instance.get_session() as session: session.add(RecorderRuns(start=three_days_ago, created=three_days_ago)) session.add(RecorderRuns(start=two_days_ago, created=two_days_ago)) session.add(RecorderRuns(start=one_day_ago, created=one_day_ago)) session.commit() instance.run_history.load_from_db(session) assert (process_timestamp( instance.run_history.get(three_days_ago + timedelta( microseconds=1)).start) == three_days_ago) assert (process_timestamp( instance.run_history.get(two_days_ago + timedelta( microseconds=1)).start) == two_days_ago) assert (process_timestamp( instance.run_history.get(one_day_ago + timedelta( microseconds=1)).start) == one_day_ago) assert (process_timestamp(instance.run_history.get(now).start) == instance.run_history.recording_start)
def test_entity_ids(self): """Test if entity ids helper method works.""" run = RecorderRuns( start=datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC), end=datetime(2016, 7, 9, 23, 0, 0, tzinfo=dt.UTC), closed_incorrect=False, created=datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC), ) self.session.add(run) self.session.commit() before_run = datetime(2016, 7, 9, 8, 0, 0, tzinfo=dt.UTC) in_run = datetime(2016, 7, 9, 13, 0, 0, tzinfo=dt.UTC) in_run2 = datetime(2016, 7, 9, 15, 0, 0, tzinfo=dt.UTC) in_run3 = datetime(2016, 7, 9, 18, 0, 0, tzinfo=dt.UTC) after_run = datetime(2016, 7, 9, 23, 30, 0, tzinfo=dt.UTC) assert run.to_native() == run assert run.entity_ids() == [] self.session.add(States( entity_id='sensor.temperature', state='20', last_changed=before_run, last_updated=before_run, )) self.session.add(States( entity_id='sensor.sound', state='10', last_changed=after_run, last_updated=after_run, )) self.session.add(States( entity_id='sensor.humidity', state='76', last_changed=in_run, last_updated=in_run, )) self.session.add(States( entity_id='sensor.lux', state='5', last_changed=in_run3, last_updated=in_run3, )) assert sorted(run.entity_ids()) == ['sensor.humidity', 'sensor.lux'] assert run.entity_ids(in_run2) == ['sensor.humidity']
async def _add_test_recorder_runs(hass: HomeAssistantType, instance: recorder.Recorder): """Add a few recorder_runs for testing.""" utcnow = dt_util.utcnow() five_days_ago = utcnow - timedelta(days=5) eleven_days_ago = utcnow - timedelta(days=11) await hass.async_block_till_done() await async_wait_recording_done(hass, instance) with recorder.session_scope(hass=hass) as session: for rec_id in range(6): if rec_id < 2: timestamp = eleven_days_ago elif rec_id < 4: timestamp = five_days_ago else: timestamp = utcnow session.add( RecorderRuns( start=timestamp, created=dt_util.utcnow(), end=timestamp + timedelta(days=1), ))
def _add_test_recorder_runs(hass): """Add a few recorder_runs for testing.""" now = datetime.now() five_days_ago = now - timedelta(days=5) eleven_days_ago = now - timedelta(days=11) hass.block_till_done() hass.data[DATA_INSTANCE].block_till_done() wait_recording_done(hass) with recorder.session_scope(hass=hass) as session: for rec_id in range(6): if rec_id < 2: timestamp = eleven_days_ago elif rec_id < 4: timestamp = five_days_ago else: timestamp = now session.add( RecorderRuns( start=timestamp, created=dt_util.utcnow(), end=timestamp + timedelta(days=1), ))
def test_entity_ids(self): """Test if entity ids helper method works.""" run = RecorderRuns( start=datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC), end=datetime(2016, 7, 9, 23, 0, 0, tzinfo=dt.UTC), closed_incorrect=False, created=datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC), ) self.session.add(run) self.session.commit() before_run = datetime(2016, 7, 9, 8, 0, 0, tzinfo=dt.UTC) in_run = datetime(2016, 7, 9, 13, 0, 0, tzinfo=dt.UTC) in_run2 = datetime(2016, 7, 9, 15, 0, 0, tzinfo=dt.UTC) in_run3 = datetime(2016, 7, 9, 18, 0, 0, tzinfo=dt.UTC) after_run = datetime(2016, 7, 9, 23, 30, 0, tzinfo=dt.UTC) assert run.to_native() == run assert run.entity_ids() == [] self.session.add( States( entity_id="sensor.temperature", state="20", last_changed=before_run, last_updated=before_run, )) self.session.add( States( entity_id="sensor.sound", state="10", last_changed=after_run, last_updated=after_run, )) self.session.add( States( entity_id="sensor.humidity", state="76", last_changed=in_run, last_updated=in_run, )) self.session.add( States( entity_id="sensor.lux", state="5", last_changed=in_run3, last_updated=in_run3, )) assert sorted(run.entity_ids()) == ["sensor.humidity", "sensor.lux"] assert run.entity_ids(in_run2) == ["sensor.humidity"]
def _add_data_in_last_run(hass, entities): """Add test data in the last recorder_run.""" # pylint: disable=protected-access t_now = dt_util.utcnow() - timedelta(minutes=10) t_min_1 = t_now - timedelta(minutes=20) t_min_2 = t_now - timedelta(minutes=30) with recorder.session_scope(hass=hass) as session: session.add(RecorderRuns(start=t_min_2, end=t_now, created=t_min_2)) for entity_id, state in entities.items(): session.add( States(entity_id=entity_id, domain=split_entity_id(entity_id)[0], state=state, attributes='{}', last_changed=t_min_1, last_updated=t_min_1, created=t_min_1))
def test_entity_ids(): """Test if entity ids helper method works.""" engine = create_engine("sqlite://") Base.metadata.create_all(engine) session_factory = sessionmaker(bind=engine) session = scoped_session(session_factory) session.query(Events).delete() session.query(States).delete() session.query(RecorderRuns).delete() run = RecorderRuns( start=datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC), end=datetime(2016, 7, 9, 23, 0, 0, tzinfo=dt.UTC), closed_incorrect=False, created=datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC), ) session.add(run) session.commit() before_run = datetime(2016, 7, 9, 8, 0, 0, tzinfo=dt.UTC) in_run = datetime(2016, 7, 9, 13, 0, 0, tzinfo=dt.UTC) in_run2 = datetime(2016, 7, 9, 15, 0, 0, tzinfo=dt.UTC) in_run3 = datetime(2016, 7, 9, 18, 0, 0, tzinfo=dt.UTC) after_run = datetime(2016, 7, 9, 23, 30, 0, tzinfo=dt.UTC) assert run.to_native() == run assert run.entity_ids() == [] session.add( States( entity_id="sensor.temperature", state="20", last_changed=before_run, last_updated=before_run, )) session.add( States( entity_id="sensor.sound", state="10", last_changed=after_run, last_updated=after_run, )) session.add( States( entity_id="sensor.humidity", state="76", last_changed=in_run, last_updated=in_run, )) session.add( States( entity_id="sensor.lux", state="5", last_changed=in_run3, last_updated=in_run3, )) assert sorted(run.entity_ids()) == ["sensor.humidity", "sensor.lux"] assert run.entity_ids(in_run2) == ["sensor.humidity"]