def test_state_changed_attr_sends_message(self, mock_utcnow, mock_pub): """"Test the sending of a new message if attribute changed.""" e_id = 'fake.entity' base_topic = 'pub' # Add the statestream component for publishing state updates assert self.add_statestream(base_topic=base_topic, publish_attributes=True) self.hass.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mock_pub.reset_mock() test_attributes = {"testing": "YES"} # Set a state of an entity mock_state_change_event(self.hass, State(e_id, 'off', attributes=test_attributes)) self.hass.block_till_done() # Make sure 'on' was published to pub/fake/entity/state calls = [ call.async_publish(self.hass, 'pub/fake/entity/state', 'off', 1, True), call.async_publish(self.hass, 'pub/fake/entity/testing', 'YES', 1, True) ] mock_pub.assert_has_calls(calls, any_order=True) assert mock_pub.called
def test_get_states(self): """ Test getting states at a specific point in time. """ self.init_recorder() states = [] # Create 10 states for 5 different entities # After the first 5, sleep a second and save the time # history.get_states takes the latest states BEFORE point X for i in range(10): state = ha.State("test.point_in_time_{}".format(i % 5), "State {}".format(i), {"attribute_test": i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() recorder._INSTANCE.block_till_done() if i < 5: states.append(state) if i == 4: time.sleep(1) point = dt_util.utcnow() self.assertEqual(states, sorted(history.get_states(point), key=lambda state: state.entity_id)) # Test get_state here because we have a DB setup self.assertEqual(states[0], history.get_state(point, states[0].entity_id))
def _setup_get_states(hass): """Set up for testing get_states.""" states = [] now = dt_util.utcnow() with patch("homeassistant.components.recorder.dt_util.utcnow", return_value=now): for i in range(5): state = ha.State( f"test.point_in_time_{i % 5}", f"State {i}", {"attribute_test": i}, ) mock_state_change_event(hass, state) states.append(state) wait_recording_done(hass) future = now + timedelta(seconds=1) with patch("homeassistant.components.recorder.dt_util.utcnow", return_value=future): for i in range(5): state = ha.State( f"test.point_in_time_{i % 5}", f"State {i}", {"attribute_test": i}, ) mock_state_change_event(hass, state) wait_recording_done(hass) return now, future, states
def test_state_changed_attr_sends_message(self, mock_utcnow, mock_pub): """Test the sending of a new message if attribute changed.""" e_id = "fake.entity" base_topic = "pub" # Add the statestream component for publishing state updates assert self.add_statestream(base_topic=base_topic, publish_attributes=True) self.hass.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mock_pub.reset_mock() test_attributes = {"testing": "YES", "list": ["a", "b", "c"], "bool": False} # Set a state of an entity mock_state_change_event( self.hass, State(e_id, "off", attributes=test_attributes) ) self.hass.block_till_done() # Make sure 'on' was published to pub/fake/entity/state calls = [ call.async_publish(self.hass, "pub/fake/entity/state", "off", 1, True), call.async_publish(self.hass, "pub/fake/entity/testing", '"YES"', 1, True), call.async_publish( self.hass, "pub/fake/entity/list", '["a", "b", "c"]', 1, True ), call.async_publish(self.hass, "pub/fake/entity/bool", "false", 1, True), ] mock_pub.assert_has_calls(calls, any_order=True) assert mock_pub.called
def test_get_states(self): """ Test getting states at a specific point in time. """ self.init_recorder() states = [] for i in range(5): state = ha.State('test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() states.append(state) recorder._INSTANCE.block_till_done() point = dt_util.utcnow() + timedelta(seconds=1) with patch('homeassistant.util.dt.utcnow', return_value=point): for i in range(5): state = ha.State('test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() # Get states returns everything before POINT self.assertEqual( states, sorted(history.get_states(point), key=lambda state: state.entity_id)) # Test get_state here because we have a DB setup self.assertEqual(states[0], history.get_state(point, states[0].entity_id))
def test_splunk_entityfilter_with_glob_filter(self, mock_requests): """Test event listener.""" # pylint: disable=no-member self._setup_with_filter({"exclude_entity_globs": ["*.skip_*"]}) testdata = [ { "entity_id": "other_domain.other_entity", "filter_expected": False }, { "entity_id": "other_domain.excluded_entity", "filter_expected": True }, { "entity_id": "excluded_domain.other_entity", "filter_expected": True }, { "entity_id": "test.skip_me", "filter_expected": True }, ] for test in testdata: mock_state_change_event(self.hass, State(test["entity_id"], "on")) self.hass.block_till_done() if test["filter_expected"]: assert not splunk.post_request.called else: assert splunk.post_request.called splunk.post_request.reset_mock()
def test_state_changed_event_exclude_entity(self, mock_utcnow, mock_pub): """"Test that filtering on excluded entity works as expected.""" base_topic = 'pub' incl = {} excl = { 'entities': ['fake.entity2'] } # Add the statestream component for publishing state updates # Set the filter to allow fake.* items assert self.add_statestream(base_topic=base_topic, publish_include=incl, publish_exclude=excl) self.hass.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mock_pub.reset_mock() # Set a state of an entity mock_state_change_event(self.hass, State('fake.entity', 'on')) self.hass.block_till_done() # Make sure 'on' was published to pub/fake/entity/state mock_pub.assert_called_with(self.hass, 'pub/fake/entity/state', 'on', 1, True) assert mock_pub.called mock_pub.reset_mock() # Set a state of an entity that shouldn't be included mock_state_change_event(self.hass, State('fake.entity2', 'on')) self.hass.block_till_done() assert not mock_pub.called
def test_wrong_ignored_event_sends_over_stream(self, mock_pub): """Test the ignoring of sending events if defined.""" assert self.add_eventstream(pub_topic='bar', ignore_event=['statee_changed']) self.hass.block_till_done() e_id = 'entity.test_id' event = {} event['event_type'] = EVENT_STATE_CHANGED new_state = { "state": "on", "entity_id": e_id, "attributes": {}, } event['event_data'] = {"new_state": new_state, "entity_id": e_id} # Reset the mock because it will have already gotten calls for the # mqtt_eventstream state change on initialization, etc. mock_pub.reset_mock() # Set a state of an entity mock_state_change_event(self.hass, State(e_id, 'on')) self.hass.block_till_done() assert mock_pub.called
def test_state_changed_event_sends_message_and_timestamp( self, mock_utcnow, mock_pub): """"Test the sending of a message and timestamps if event changed.""" e_id = 'another.entity' base_topic = 'pub' # Add the statestream component for publishing state updates assert self.add_statestream(base_topic=base_topic, publish_attributes=None, publish_timestamps=True) self.hass.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mock_pub.reset_mock() # Set a state of an entity mock_state_change_event(self.hass, State(e_id, 'on')) self.hass.block_till_done() # Make sure 'on' was published to pub/fake/entity/state calls = [ call.async_publish(self.hass, 'pub/another/entity/state', 'on', 1, True), call.async_publish(self.hass, 'pub/another/entity/last_changed', ANY, 1, True), call.async_publish(self.hass, 'pub/another/entity/last_updated', ANY, 1, True), ] mock_pub.assert_has_calls(calls, any_order=True) assert mock_pub.called
def test_state_changed_event_include_domain_exclude_entity( self, mock_utcnow, mock_pub): """Test filtering with included domain and excluded entity.""" base_topic = "pub" incl = {"domains": ["fake"]} excl = {"entities": ["fake.entity2"]} # Add the statestream component for publishing state updates # Set the filter to allow fake.* items assert self.add_statestream(base_topic=base_topic, publish_include=incl, publish_exclude=excl) self.hass.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mock_pub.reset_mock() # Set a state of an entity mock_state_change_event(self.hass, State("fake.entity", "on")) self.hass.block_till_done() # Make sure 'on' was published to pub/fake/entity/state mock_pub.assert_called_with(self.hass, "pub/fake/entity/state", "on", 1, True) assert mock_pub.called mock_pub.reset_mock() # Set a state of an entity that shouldn't be included mock_state_change_event(self.hass, State("fake.entity2", "on")) self.hass.block_till_done() assert not mock_pub.called
def test_state_changed_event_sends_message_and_timestamp( self, mock_utcnow, mock_pub): """Test the sending of a message and timestamps if event changed.""" e_id = "another.entity" base_topic = "pub" # Add the statestream component for publishing state updates assert self.add_statestream(base_topic=base_topic, publish_attributes=None, publish_timestamps=True) self.hass.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mock_pub.reset_mock() # Set a state of an entity mock_state_change_event(self.hass, State(e_id, "on")) self.hass.block_till_done() # Make sure 'on' was published to pub/fake/entity/state calls = [ call.async_publish(self.hass, "pub/another/entity/state", "on", 1, True), call.async_publish(self.hass, "pub/another/entity/last_changed", ANY, 1, True), call.async_publish(self.hass, "pub/another/entity/last_updated", ANY, 1, True), ] mock_pub.assert_has_calls(calls, any_order=True) assert mock_pub.called
def test_state_changed_event_exclude_entity(self, mock_utcnow, mock_pub): """Test that filtering on excluded entity works as expected.""" base_topic = 'pub' incl = {} excl = { 'entities': ['fake.entity2'] } # Add the statestream component for publishing state updates # Set the filter to allow fake.* items assert self.add_statestream(base_topic=base_topic, publish_include=incl, publish_exclude=excl) self.hass.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mock_pub.reset_mock() # Set a state of an entity mock_state_change_event(self.hass, State('fake.entity', 'on')) self.hass.block_till_done() # Make sure 'on' was published to pub/fake/entity/state mock_pub.assert_called_with(self.hass, 'pub/fake/entity/state', 'on', 1, True) assert mock_pub.called mock_pub.reset_mock() # Set a state of an entity that shouldn't be included mock_state_change_event(self.hass, State('fake.entity2', 'on')) self.hass.block_till_done() assert not mock_pub.called
def test_get_states(self): """ Test getting states at a specific point in time. """ self.init_recorder() states = [] # Create 10 states for 5 different entities # After the first 5, sleep a second and save the time # history.get_states takes the latest states BEFORE point X for i in range(10): state = ha.State('test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() recorder._INSTANCE.block_till_done() if i < 5: states.append(state) if i == 4: time.sleep(1) point = dt_util.utcnow() self.assertEqual( states, sorted(history.get_states(point), key=lambda state: state.entity_id)) # Test get_state here because we have a DB setup self.assertEqual(states[0], history.get_state(point, states[0].entity_id))
async def test_state_changed_event_exclude_globs(hass, mqtt_mock): """Test that filtering on excluded globs works as expected.""" base_topic = "pub" incl = {} excl = {"entity_globs": ["*.excluded_*"]} # Add the statestream component for publishing state updates # Set the filter to allow *.excluded_* items assert await add_statestream( hass, base_topic=base_topic, publish_include=incl, publish_exclude=excl ) await hass.async_block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mqtt_mock.async_publish.reset_mock() # Set a state of an entity mock_state_change_event(hass, State("fake.entity", "on")) await hass.async_block_till_done() await hass.async_block_till_done() # Make sure 'on' was published to pub/fake/entity/state mqtt_mock.async_publish.assert_called_with("pub/fake/entity/state", "on", 1, True) assert mqtt_mock.async_publish.called mqtt_mock.async_publish.reset_mock() # Set a state of an entity that shouldn't be included by glob mock_state_change_event(hass, State("fake.excluded_entity", "on")) await hass.async_block_till_done() await hass.async_block_till_done() assert not mqtt_mock.async_publish.called
def test_get_states(self): """Test getting states at a specific point in time.""" self.test_setup() states = [] now = dt_util.utcnow() with patch( "homeassistant.components.recorder.dt_util.utcnow", return_value=now ): for i in range(5): state = ha.State( "test.point_in_time_{}".format(i % 5), f"State {i}", {"attribute_test": i}, ) mock_state_change_event(self.hass, state) states.append(state) wait_recording_done(self.hass) future = now + timedelta(seconds=1) with patch( "homeassistant.components.recorder.dt_util.utcnow", return_value=future ): for i in range(5): state = ha.State( "test.point_in_time_{}".format(i % 5), f"State {i}", {"attribute_test": i}, ) mock_state_change_event(self.hass, state) wait_recording_done(self.hass) # Get states returns everything before POINT for state1, state2 in zip( states, sorted( history.get_states(self.hass, future), key=lambda state: state.entity_id ), ): assert state1 == state2 # Test get_state here because we have a DB setup assert states[0] == history.get_state(self.hass, future, states[0].entity_id) time_before_recorder_ran = now - timedelta(days=1000) assert history.get_states(self.hass, time_before_recorder_ran) == [] assert history.get_state(self.hass, time_before_recorder_ran, "demo.id") is None
async def test_state_changed_event_include_domain_globs_exclude_entity( hass, mqtt_mock): """Test filtering with included domain and glob and excluded entity.""" base_topic = "pub" incl = {"domains": ["fake"], "entity_globs": ["*.included_*"]} excl = {"entities": ["fake.entity2"]} # Add the statestream component for publishing state updates # Set the filter to include with exclude filter assert await add_statestream(hass, base_topic=base_topic, publish_include=incl, publish_exclude=excl) await hass.async_block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mqtt_mock.async_publish.reset_mock() # Set a state of an entity included by domain mock_state_change_event(hass, State("fake.entity", "on")) await hass.async_block_till_done() await hass.async_block_till_done() # Make sure 'on' was published to pub/fake/entity/state mqtt_mock.async_publish.assert_called_with("pub/fake/entity/state", "on", 1, True) assert mqtt_mock.async_publish.called mqtt_mock.async_publish.reset_mock() # Set a state of an entity included by glob mock_state_change_event(hass, State("fake.included_entity", "on")) await hass.async_block_till_done() await hass.async_block_till_done() # Make sure 'on' was published to pub/fake/entity/state mqtt_mock.async_publish.assert_called_with( "pub/fake/included_entity/state", "on", 1, True) assert mqtt_mock.async_publish.called mqtt_mock.async_publish.reset_mock() # Set a state of an entity that shouldn't be included mock_state_change_event(hass, State("fake.entity2", "on")) await hass.async_block_till_done() await hass.async_block_till_done() assert not mqtt_mock.async_publish.called mqtt_mock.async_publish.reset_mock() # Set a state of an entity that doesn't match any filters mock_state_change_event(hass, State("fake2.entity", "on")) await hass.async_block_till_done() await hass.async_block_till_done() assert not mqtt_mock.async_publish.called
def test_get_states(self): """Test getting states at a specific point in time.""" self.init_recorder() states = [] now = dt_util.utcnow() with patch("openpeerpower.components.recorder.dt_util.utcnow", return_value=now): for i in range(5): state = op.State( "test.point_in_time_{}".format(i % 5), "State {}".format(i), {"attribute_test": i}, ) mock_state_change_event(self.opp, state) states.append(state) self.wait_recording_done() future = now + timedelta(seconds=1) with patch("openpeerpower.components.recorder.dt_util.utcnow", return_value=future): for i in range(5): state = op.State( "test.point_in_time_{}".format(i % 5), "State {}".format(i), {"attribute_test": i}, ) mock_state_change_event(self.opp, state) self.wait_recording_done() # Get states returns everything before POINT for state1, state2 in zip( states, sorted(history.get_states(self.opp, future), key=lambda state: state.entity_id), ): assert state1 == state2 # Test get_state here because we have a DB setup assert states[0] == history.get_state(self.opp, future, states[0].entity_id)
async def test_state_changed_event_sends_message(hass, mqtt_mock): """Test the sending of a new message if event changed.""" now = dt_util.as_utc(dt_util.now()) e_id = "fake.entity" pub_topic = "bar" with patch( ("homeassistant.core.dt_util.utcnow"), return_value=now, ): # Add the eventstream component for publishing events assert await add_eventstream(hass, pub_topic=pub_topic) await hass.async_block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_eventstream state change on initialization, etc. mqtt_mock.async_publish.reset_mock() # Set a state of an entity mock_state_change_event(hass, State(e_id, "on")) await hass.async_block_till_done() await hass.async_block_till_done() # The order of the JSON is indeterminate, # so first just check that publish was called mqtt_mock.async_publish.assert_called_with(pub_topic, ANY, 0, False) assert mqtt_mock.async_publish.called # Get the actual call to publish and make sure it was the one # we were looking for msg = mqtt_mock.async_publish.call_args[0][1] event = {} event["event_type"] = EVENT_STATE_CHANGED new_state = { "last_updated": now.isoformat(), "state": "on", "entity_id": e_id, "attributes": {}, "last_changed": now.isoformat(), } event["event_data"] = {"new_state": new_state, "entity_id": e_id} # Verify that the message received was that expected result = json.loads(msg) result["event_data"]["new_state"].pop("context") assert result == event
def test_get_states(self): """Test getting states at a specific point in time.""" self.init_recorder() states = [] now = dt_util.utcnow() with patch('homeassistant.components.recorder.dt_util.utcnow', return_value=now): for i in range(5): state = ha.State( 'test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) states.append(state) self.wait_recording_done() future = now + timedelta(seconds=1) with patch('homeassistant.components.recorder.dt_util.utcnow', return_value=future): for i in range(5): state = ha.State( 'test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.wait_recording_done() # Get states returns everything before POINT for state1, state2 in zip( states, sorted(history.get_states(self.hass, future), key=lambda state: state.entity_id)): assert state1 == state2 # Test get_state here because we have a DB setup self.assertEqual( states[0], history.get_state(self.hass, future, states[0].entity_id))
def test_state_changed_event_sends_message(self, mock_utcnow, mock_pub): """Test the sending of a new message if event changed.""" now = dt_util.as_utc(dt_util.now()) e_id = 'fake.entity' pub_topic = 'bar' mock_utcnow.return_value = now # Add the eventstream component for publishing events assert self.add_eventstream(pub_topic=pub_topic) self.hass.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_eventstream state change on initialization, etc. mock_pub.reset_mock() # Set a state of an entity mock_state_change_event(self.hass, State(e_id, 'on')) self.hass.block_till_done() # The order of the JSON is indeterminate, # so first just check that publish was called mock_pub.assert_called_with(self.hass, pub_topic, ANY) assert mock_pub.called # Get the actual call to publish and make sure it was the one # we were looking for msg = mock_pub.call_args[0][2] event = {} event['event_type'] = EVENT_STATE_CHANGED new_state = { "last_updated": now.isoformat(), "state": "on", "entity_id": e_id, "attributes": {}, "last_changed": now.isoformat(), } event['event_data'] = {"new_state": new_state, "entity_id": e_id} # Verify that the message received was that expected result = json.loads(msg) result['event_data']['new_state'].pop('context') assert result == event
def test_state_changed_event_sends_message(self, mock_utcnow, mock_pub): """Test the sending of a new message if event changed.""" e_id = "fake.entity" base_topic = "pub" # Add the statestream component for publishing state updates assert self.add_statestream(base_topic=base_topic) self.hass.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mock_pub.reset_mock() # Set a state of an entity mock_state_change_event(self.hass, State(e_id, "on")) self.hass.block_till_done() # Make sure 'on' was published to pub/fake/entity/state mock_pub.assert_called_with(self.hass, "pub/fake/entity/state", "on", 1, True) assert mock_pub.called
def test_state_changed_event_sends_message(self, mock_utcnow, mock_pub): """"Test the sending of a new message if event changed.""" e_id = 'fake.entity' base_topic = 'pub' # Add the statestream component for publishing state updates assert self.add_statestream(base_topic=base_topic) self.hass.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mock_pub.reset_mock() # Set a state of an entity mock_state_change_event(self.hass, State(e_id, 'on')) self.hass.block_till_done() # Make sure 'on' was published to pub/fake/entity/state mock_pub.assert_called_with(self.hass, 'pub/fake/entity/state', 'on', 1, True) assert mock_pub.called
async def test_state_changed_event_sends_message(hass, mqtt_mock): """Test the sending of a new message if event changed.""" e_id = "fake.entity" base_topic = "pub" # Add the statestream component for publishing state updates assert await add_statestream(hass, base_topic=base_topic) await hass.async_block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mqtt_mock.async_publish.reset_mock() # Set a state of an entity mock_state_change_event(hass, State(e_id, "on")) await hass.async_block_till_done() await hass.async_block_till_done() # Make sure 'on' was published to pub/fake/entity/state mqtt_mock.async_publish.assert_called_with("pub/fake/entity/state", "on", 1, True) assert mqtt_mock.async_publish.called
def test_state_changed_event_sends_message(self, mock_datetime, mock_pub): now = '00:19:19 11-01-2016' e_id = 'fake.entity' pub_topic = 'bar' mock_datetime.return_value = now # Add the eventstream component for publishing events self.assertTrue(self.add_eventstream(pub_topic=pub_topic)) self.hass.pool.block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_eventstream state change on initialization, etc. mock_pub.reset_mock() # Set a state of an entity mock_state_change_event(self.hass, State(e_id, 'on')) self.hass.pool.block_till_done() # The order of the JSON is indeterminate, # so first just check that publish was called mock_pub.assert_called_with(self.hass, pub_topic, ANY) self.assertTrue(mock_pub.called) # Get the actual call to publish and make sure it was the one # we were looking for msg = mock_pub.call_args[0][2] event = {} event['event_type'] = EVENT_STATE_CHANGED new_state = { "last_updated": now, "state": "on", "entity_id": e_id, "attributes": {}, "last_changed": now } event['event_data'] = {"new_state": new_state, "entity_id": e_id} # Verify that the message received was that expected self.assertEqual(json.loads(msg), event)
async def test_ignored_event_doesnt_send_over_stream(hass, mqtt_mock): """Test the ignoring of sending events if defined.""" assert await add_eventstream(hass, pub_topic="bar", ignore_event=["state_changed"]) await hass.async_block_till_done() e_id = "entity.test_id" event = {} event["event_type"] = EVENT_STATE_CHANGED new_state = {"state": "on", "entity_id": e_id, "attributes": {}} event["event_data"] = {"new_state": new_state, "entity_id": e_id} # Reset the mock because it will have already gotten calls for the # mqtt_eventstream state change on initialization, etc. mqtt_mock.async_publish.reset_mock() # Set a state of an entity mock_state_change_event(hass, State(e_id, "on")) await hass.async_block_till_done() assert not mqtt_mock.async_publish.called
def test_get_states(self): """ Test getting states at a specific point in time. """ self.init_recorder() states = [] for i in range(5): state = ha.State( 'test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() states.append(state) recorder._INSTANCE.block_till_done() point = dt_util.utcnow() + timedelta(seconds=1) with patch('homeassistant.util.dt.utcnow', return_value=point): for i in range(5): state = ha.State( 'test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() # Get states returns everything before POINT self.assertEqual(states, sorted(history.get_states(point), key=lambda state: state.entity_id)) # Test get_state here because we have a DB setup self.assertEqual( states[0], history.get_state(point, states[0].entity_id))
async def test_state_changed_event_exclude_domain_include_entity( opp, mqtt_mock): """Test filtering with excluded domain and included entity.""" base_topic = "pub" incl = {"entities": ["fake.entity"]} excl = {"domains": ["fake"]} # Add the statestream component for publishing state updates # Set the filter to allow fake.* items assert await add_statestream(opp, base_topic=base_topic, publish_include=incl, publish_exclude=excl) await opp.async_block_till_done() # Reset the mock because it will have already gotten calls for the # mqtt_statestream state change on initialization, etc. mqtt_mock.async_publish.reset_mock() # Set a state of an entity mock_state_change_event(opp, State("fake.entity", "on")) await opp.async_block_till_done() await opp.async_block_till_done() # Make sure 'on' was published to pub/fake/entity/state mqtt_mock.async_publish.assert_called_with("pub/fake/entity/state", "on", 1, True) assert mqtt_mock.async_publish.called mqtt_mock.async_publish.reset_mock() # Set a state of an entity that shouldn't be included mock_state_change_event(opp, State("fake.entity2", "on")) await opp.async_block_till_done() await opp.async_block_till_done() assert not mqtt_mock.async_publish.called