async def test_parse_invalid_str(self): with OpsDroid() as opsdroid: mock_skill = await self.getMockSkill() opsdroid.skills.append(match_event("Message2")(mock_skill)) mock_connector = amock.CoroutineMock() message = events.Message("Hello World", "user", "default", mock_connector) with self.assertRaises(ValueError): await parse_event_type(opsdroid, message)
async def test_parse_str_event(self): with OpsDroid() as opsdroid: opsdroid.run_skill = amock.CoroutineMock() mock_skill = await self.getMockSkill() opsdroid.skills.append(match_event("Message")(mock_skill)) mock_connector = amock.CoroutineMock() message = events.Message("Hello World", "user", "default", mock_connector) await opsdroid.parse(message) self.assertTrue(opsdroid.run_skill.called)
async def test_parse_event_with_args_list(self): with OpsDroid() as opsdroid: opsdroid.run_skill = amock.CoroutineMock() mock_skill = await self.getMockSkill() opsdroid.skills.append( match_event(events.Message, value=["click_me_123"])(mock_skill)) mock_connector = amock.CoroutineMock() message1 = events.Message("Hello World", "user", "default", mock_connector) message1.update_entity("value", ["click_me_123"]) await opsdroid.parse(message1) self.assertTrue(opsdroid.run_skill.called)
async def test_receive_status(opsdroid, connector, mock_api): """Test a PR create event creates a message and parses it.""" test_skill = match_event(Message)(CoroutineMock()) opsdroid.register_skill(test_skill, config={"name": "test"}) async with running_opsdroid(opsdroid): resp = await call_endpoint( opsdroid, "/connector/github", "POST", data=get_webhook_payload("status.json"), ) assert resp.status == 201 assert not test_skill.called
async def test_parse_event_with_constraint(self): with OpsDroid() as opsdroid: opsdroid.run_skill = amock.CoroutineMock() mock_skill = await self.getMockSkill() mock_skill = match_event(events.JoinRoom)(mock_skill) mock_skill = constrain_rooms(["#general"])(mock_skill) opsdroid.skills.append(mock_skill) mock_connector = amock.CoroutineMock() mock_connector.lookup_target = amock.Mock(return_value="some_room_id") message = events.JoinRoom( user="******", target="some_room_id", connector=mock_connector ) await opsdroid.parse(message) self.assertTrue(opsdroid.run_skill.called)
def match_hass_state_changed(entity_id: str, **kwargs) -> Callable: """A matcher for state changes in Home Assistant. When an entity changes state in Home Assistant an event is triggered in Opsdroid. This matcher can be used to watch for a specific entity to change state:: from opsdroid_homeassistant import HassSkill, match_hass_state_changed class SunriseSkill(HassSkill): @match_hass_state_changed("sun.sun", state="below_horizon") async def lights_on_at_sunset(self, event): await self.turn_on("light.outside") Alternatively you can use the :func:`opsdroid.matchers.match_event` matcher from the core Opsdroid set of matchers along with the :class:`opsdroid_homeassistant.HassEvent` class from opsdroid-homeassistant. With this method you much specify the entity with the kwarg ``entity_id`` and set ``changed=True`` if you only wish for the skill to trigger when the state changes. Sometimes entities send an event even if the state hasn't changed:: from opsdroid.matchers import match_event from opsdroid_homeassistant import HassSkill, HassEvent class SunriseSkill(HassSkill): @match_event(HassEvent, entity_id="sun.sun", changed=True, state="below_horizon") async def lights_on_at_sunset(self, event): await self.turn_on("light.outside") Note: For sunrise and sunset triggers you can also use the :func:`match_sunrise` and :func:`match_sunset` helper matchers. Args: entity_id: The full domain and name of the entity you want to watch. e,g ``sun.sun`` state (optional): The state you want to watch for. e.g ``on`` """ return match_event(HassEvent, entity_id=entity_id, changed=True, **kwargs)