def test_event_repr(): """Test that Event repr method works.""" assert str(ha.Event("TestEvent")) == "<Event TestEvent[L]>" assert (str( ha.Event("TestEvent", {"beer": "nice"}, ha.EventOrigin.remote)) == "<Event TestEvent[R]: beer=nice>")
def _logbook_filtering(opp, last_changed, last_updated): from openpeerpower.components import logbook entity_id = "test.entity" old_state = {"entity_id": entity_id, "state": "off"} new_state = { "entity_id": entity_id, "state": "on", "last_updated": last_updated, "last_changed": last_changed, } event = core.Event( EVENT_STATE_CHANGED, {"entity_id": entity_id, "old_state": old_state, "new_state": new_state}, ) def yield_events(event): # pylint: disable=protected-access entities_filter = logbook._generate_filter_from_config({}) for _ in range(10 ** 5): if logbook._keep_event(event, entities_filter): yield event start = timer() list(logbook.humanify(None, yield_events(event))) return timer() - start
async def test_event_to_db_model(): """Test we can round trip Event conversion.""" event = ha.Event("state_changed", {"some": "attr"}, ha.EventOrigin.local, dt_util.utcnow()) native = Events.from_event(event).to_native() assert native == event native = Events.from_event(event, event_data="{}").to_native() event.data = {} assert native == event
def test_event_eq(): """Test events.""" now = dt_util.utcnow() data = {"some": "attr"} context = ha.Context() event1, event2 = [ ha.Event("some_type", data, time_fired=now, context=context) for _ in range(2) ] assert event1 == event2
def test_from_event(self): """Test converting event to db state.""" state = op.State("sensor.temperature", "18") event = op.Event( EVENT_STATE_CHANGED, { "entity_id": "sensor.temperature", "old_state": None, "new_state": state }, context=state.context, ) assert state == States.from_event(event).to_native()
def test_from_event_to_db_state(): """Test converting event to db state.""" state = ha.State("sensor.temperature", "18") event = ha.Event( EVENT_STATE_CHANGED, { "entity_id": "sensor.temperature", "old_state": None, "new_state": state }, context=state.context, ) # We don't restore context unless we need it by joining the # events table on the event_id for state_changed events state.context = ha.Context(id=None) assert state == States.from_event(event).to_native()
def test_from_event_to_delete_state(self): """Test converting deleting state event to db state.""" event = op.Event( EVENT_STATE_CHANGED, { "entity_id": "sensor.temperature", "old_state": op.State("sensor.temperature", "18"), "new_state": None, }, ) db_state = States.from_event(event) assert db_state.entity_id == "sensor.temperature" assert db_state.domain == "sensor" assert db_state.state == "" assert db_state.last_changed == event.time_fired assert db_state.last_updated == event.time_fired
def test_event_as_dict(): """Test an Event as dictionary.""" event_type = "some_type" now = dt_util.utcnow() data = {"some": "attr"} event = ha.Event(event_type, data, ha.EventOrigin.local, now) expected = { "event_type": event_type, "data": data, "origin": "LOCAL", "time_fired": now.isoformat(), "context": { "id": event.context.id, "parent_id": None, "user_id": event.context.user_id, }, } assert event.as_dict() == expected # 2nd time to verify cache assert event.as_dict() == expected
def test_from_event(self): """Test converting event to db event.""" event = op.Event("test_event", {"some_data": 15}) assert event == Events.from_event(event).to_native()