Exemplo n.º 1
0
async def test_humanify_command_received(opp):
    """Test humanifying command event."""
    opp.config.components.add("recorder")
    opp.config.components.add("frontend")
    opp.config.components.add("google_assistant")
    assert await async_setup_component(opp, "logbook", {})
    entity_attr_cache = logbook.EntityAttributeCache(opp)

    opp.states.async_set(
        "light.kitchen", "on", {ATTR_FRIENDLY_NAME: "The Kitchen Lights"}
    )

    events = list(
        logbook.humanify(
            opp,
            [
                MockLazyEventPartialState(
                    EVENT_COMMAND_RECEIVED,
                    {
                        "request_id": "abcd",
                        ATTR_ENTITY_ID: ["light.kitchen"],
                        "execution": [
                            {
                                "command": "action.devices.commands.OnOff",
                                "params": {"on": True},
                            }
                        ],
                        "source": SOURCE_LOCAL,
                    },
                ),
                MockLazyEventPartialState(
                    EVENT_COMMAND_RECEIVED,
                    {
                        "request_id": "abcd",
                        ATTR_ENTITY_ID: ["light.non_existing"],
                        "execution": [
                            {
                                "command": "action.devices.commands.OnOff",
                                "params": {"on": False},
                            }
                        ],
                        "source": SOURCE_CLOUD,
                    },
                ),
            ],
            entity_attr_cache,
            {},
        )
    )

    assert len(events) == 2
    event1, event2 = events

    assert event1["name"] == "Google Assistant"
    assert event1["domain"] == DOMAIN
    assert event1["message"] == "sent command OnOff (via local)"

    assert event2["name"] == "Google Assistant"
    assert event2["domain"] == DOMAIN
    assert event2["message"] == "sent command OnOff"
Exemplo n.º 2
0
async def test_humanify_automation_trigger_event(opp):
    """Test humanifying Shelly click event."""
    opp.config.components.add("recorder")
    assert await async_setup_component(opp, "automation", {})
    assert await async_setup_component(opp, "logbook", {})
    entity_attr_cache = logbook.EntityAttributeCache(opp)
    context = Context()

    event1, event2 = list(
        logbook.humanify(
            opp,
            [
                MockLazyEventPartialState(
                    automation.EVENT_AUTOMATION_TRIGGERED,
                    {
                        "name": "Bla",
                        "entity_id": "automation.bla",
                        "source": "state change of input_boolean.yo",
                    },
                    context=context,
                ),
                MockLazyEventPartialState(
                    automation.EVENT_AUTOMATION_TRIGGERED,
                    {
                        "name": "Bla",
                        "entity_id": "automation.bla",
                    },
                    context=context,
                ),
            ],
            entity_attr_cache,
            {},
        )
    )

    assert event1["name"] == "Bla"
    assert event1["message"] == "has been triggered by state change of input_boolean.yo"
    assert event1["source"] == "state change of input_boolean.yo"
    assert event1["context_id"] == context.id
    assert event1["entity_id"] == "automation.bla"

    assert event2["name"] == "Bla"
    assert event2["message"] == "has been triggered"
    assert event2["source"] is None
    assert event2["context_id"] == context.id
    assert event2["entity_id"] == "automation.bla"
Exemplo n.º 3
0
async def test_humanify_shelly_click_event(opp, coap_wrapper):
    """Test humanifying Shelly click event."""
    assert coap_wrapper
    opp.config.components.add("recorder")
    assert await async_setup_component(opp, "logbook", {})
    entity_attr_cache = logbook.EntityAttributeCache(opp)

    event1, event2 = list(
        logbook.humanify(
            opp,
            [
                MockLazyEventPartialState(
                    EVENT_SHELLY_CLICK,
                    {
                        ATTR_DEVICE_ID: coap_wrapper.device_id,
                        ATTR_DEVICE: "shellyix3-12345678",
                        ATTR_CLICK_TYPE: "single",
                        ATTR_CHANNEL: 1,
                    },
                ),
                MockLazyEventPartialState(
                    EVENT_SHELLY_CLICK,
                    {
                        ATTR_DEVICE_ID: "no_device_id",
                        ATTR_DEVICE: "shellyswitch25-12345678",
                        ATTR_CLICK_TYPE: "long",
                        ATTR_CHANNEL: 2,
                    },
                ),
            ],
            entity_attr_cache,
            {},
        ))

    assert event1["name"] == "Shelly"
    assert event1["domain"] == DOMAIN
    assert (event1["message"] ==
            "'single' click event for Test name channel 1 was fired.")

    assert event2["name"] == "Shelly"
    assert event2["domain"] == DOMAIN
    assert (
        event2["message"] ==
        "'long' click event for shellyswitch25-12345678 channel 2 was fired.")
Exemplo n.º 4
0
async def test_humanify_homekit_changed_event(opp, hk_driver):
    """Test humanifying HomeKit changed event."""
    opp.config.components.add("recorder")
    with patch("openpeerpower.components.homekit.HomeKit"):
        assert await async_setup_component(opp, "homekit", {"homekit": {}})
    assert await async_setup_component(opp, "logbook", {})
    entity_attr_cache = logbook.EntityAttributeCache(opp)

    event1, event2 = list(
        logbook.humanify(
            opp,
            [
                MockLazyEventPartialState(
                    EVENT_HOMEKIT_CHANGED,
                    {
                        ATTR_ENTITY_ID: "lock.front_door",
                        ATTR_DISPLAY_NAME: "Front Door",
                        ATTR_SERVICE: "lock",
                    },
                ),
                MockLazyEventPartialState(
                    EVENT_HOMEKIT_CHANGED,
                    {
                        ATTR_ENTITY_ID: "cover.window",
                        ATTR_DISPLAY_NAME: "Window",
                        ATTR_SERVICE: "set_cover_position",
                        ATTR_VALUE: 75,
                    },
                ),
            ],
            entity_attr_cache,
            {},
        )
    )

    assert event1["name"] == "HomeKit"
    assert event1["domain"] == DOMAIN_HOMEKIT
    assert event1["message"] == "send command lock for Front Door"
    assert event1["entity_id"] == "lock.front_door"

    assert event2["name"] == "HomeKit"
    assert event2["domain"] == DOMAIN_HOMEKIT
    assert event2["message"] == "send command set_cover_position to 75 for Window"
    assert event2["entity_id"] == "cover.window"
Exemplo n.º 5
0
async def test_logbook_humanify_automation_triggered_event(opp):
    """Test humanifying Automation Trigger event."""
    opp.config.components.add("recorder")
    await async_setup_component(opp, automation.DOMAIN, {})
    await async_setup_component(opp, "logbook", {})
    entity_attr_cache = logbook.EntityAttributeCache(opp)

    event1, event2 = list(
        logbook.humanify(
            opp,
            [
                MockLazyEventPartialState(
                    EVENT_AUTOMATION_TRIGGERED,
                    {ATTR_ENTITY_ID: "automation.hello", ATTR_NAME: "Hello Automation"},
                ),
                MockLazyEventPartialState(
                    EVENT_AUTOMATION_TRIGGERED,
                    {
                        ATTR_ENTITY_ID: "automation.bye",
                        ATTR_NAME: "Bye Automation",
                        ATTR_SOURCE: "source of trigger",
                    },
                ),
            ],
            entity_attr_cache,
            {},
        )
    )

    assert event1["name"] == "Hello Automation"
    assert event1["domain"] == "automation"
    assert event1["message"] == "has been triggered"
    assert event1["entity_id"] == "automation.hello"

    assert event2["name"] == "Bye Automation"
    assert event2["domain"] == "automation"
    assert event2["message"] == "has been triggered by source of trigger"
    assert event2["entity_id"] == "automation.bye"
Exemplo n.º 6
0
async def _logbook_filtering(opp, last_changed, last_updated):
    # pylint: disable=import-outside-toplevel
    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 = _create_state_changed_event_from_old_new(
        entity_id, dt_util.utcnow(), old_state, new_state
    )

    entity_attr_cache = logbook.EntityAttributeCache(opp)

    entities_filter = convert_include_exclude_filter(
        logbook.INCLUDE_EXCLUDE_BASE_FILTER_SCHEMA({})
    )

    def yield_events(event):
        for _ in range(10 ** 5):
            # pylint: disable=protected-access
            if logbook._keep_event(opp, event, entities_filter):
                yield event

    start = timer()

    list(logbook.humanify(opp, yield_events(event), entity_attr_cache, {}))

    return timer() - start
Exemplo n.º 7
0
async def test_humanifying_deconz_event(opp, aioclient_mock):
    """Test humanifying deCONZ event."""
    data = {
        "sensors": {
            "1": {
                "name": "Switch 1",
                "type": "ZHASwitch",
                "state": {
                    "buttonevent": 1000
                },
                "config": {},
                "uniqueid": "00:00:00:00:00:00:00:01-00",
            },
            "2": {
                "name": "Hue remote",
                "type": "ZHASwitch",
                "modelid": "RWL021",
                "state": {
                    "buttonevent": 1000
                },
                "config": {},
                "uniqueid": "00:00:00:00:00:00:00:02-00",
            },
            "3": {
                "name": "Xiaomi cube",
                "type": "ZHASwitch",
                "modelid": "lumi.sensor_cube",
                "state": {
                    "buttonevent": 1000,
                    "gesture": 1
                },
                "config": {},
                "uniqueid": "00:00:00:00:00:00:00:03-00",
            },
            "4": {
                "name": "Faulty event",
                "type": "ZHASwitch",
                "state": {},
                "config": {},
                "uniqueid": "00:00:00:00:00:00:00:04-00",
            },
        }
    }
    with patch.dict(DECONZ_WEB_REQUEST, data):
        await setup_deconz_integration(opp, aioclient_mock)

    device_registry = await opp.helpers.device_registry.async_get_registry()

    switch_event_id = slugify(data["sensors"]["1"]["name"])
    switch_serial = data["sensors"]["1"]["uniqueid"].split("-", 1)[0]
    switch_entry = device_registry.async_get_device(
        identifiers={(DECONZ_DOMAIN, switch_serial)})

    hue_remote_event_id = slugify(data["sensors"]["2"]["name"])
    hue_remote_serial = data["sensors"]["2"]["uniqueid"].split("-", 1)[0]
    hue_remote_entry = device_registry.async_get_device(
        identifiers={(DECONZ_DOMAIN, hue_remote_serial)})

    xiaomi_cube_event_id = slugify(data["sensors"]["3"]["name"])
    xiaomi_cube_serial = data["sensors"]["3"]["uniqueid"].split("-", 1)[0]
    xiaomi_cube_entry = device_registry.async_get_device(
        identifiers={(DECONZ_DOMAIN, xiaomi_cube_serial)})

    faulty_event_id = slugify(data["sensors"]["4"]["name"])
    faulty_serial = data["sensors"]["4"]["uniqueid"].split("-", 1)[0]
    faulty_entry = device_registry.async_get_device(
        identifiers={(DECONZ_DOMAIN, faulty_serial)})

    opp.config.components.add("recorder")
    assert await async_setup_component(opp, "logbook", {})
    entity_attr_cache = logbook.EntityAttributeCache(opp)

    events = list(
        logbook.humanify(
            opp,
            [
                # Event without matching device trigger
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: switch_entry.id,
                        CONF_EVENT: 2000,
                        CONF_ID: switch_event_id,
                        CONF_UNIQUE_ID: switch_serial,
                    },
                ),
                # Event with matching device trigger
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: hue_remote_entry.id,
                        CONF_EVENT: 2001,
                        CONF_ID: hue_remote_event_id,
                        CONF_UNIQUE_ID: hue_remote_serial,
                    },
                ),
                # Gesture with matching device trigger
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: xiaomi_cube_entry.id,
                        CONF_GESTURE: 1,
                        CONF_ID: xiaomi_cube_event_id,
                        CONF_UNIQUE_ID: xiaomi_cube_serial,
                    },
                ),
                # Unsupported device trigger
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: xiaomi_cube_entry.id,
                        CONF_GESTURE: "unsupported_gesture",
                        CONF_ID: xiaomi_cube_event_id,
                        CONF_UNIQUE_ID: xiaomi_cube_serial,
                    },
                ),
                # Unknown event
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: faulty_entry.id,
                        "unknown_event": None,
                        CONF_ID: faulty_event_id,
                        CONF_UNIQUE_ID: faulty_serial,
                    },
                ),
            ],
            entity_attr_cache,
            {},
        ))

    assert events[0]["name"] == "Switch 1"
    assert events[0]["domain"] == "deconz"
    assert events[0]["message"] == "fired event '2000'."

    assert events[1]["name"] == "Hue remote"
    assert events[1]["domain"] == "deconz"
    assert events[1]["message"] == "'Long press' event for 'Dim up' was fired."

    assert events[2]["name"] == "Xiaomi cube"
    assert events[2]["domain"] == "deconz"
    assert events[2]["message"] == "fired event 'Shake'."

    assert events[3]["name"] == "Xiaomi cube"
    assert events[3]["domain"] == "deconz"
    assert events[3]["message"] == "fired event 'unsupported_gesture'."

    assert events[4]["name"] == "Faulty event"
    assert events[4]["domain"] == "deconz"
    assert events[4]["message"] == "fired an unknown event."
Exemplo n.º 8
0
async def test_humanifying_deconz_alarm_event(opp, aioclient_mock):
    """Test humanifying deCONZ event."""
    data = {
        "sensors": {
            "1": {
                "config": {
                    "armed": "disarmed",
                    "enrolled": 0,
                    "on": True,
                    "panel": "disarmed",
                    "pending": [],
                    "reachable": True,
                },
                "ep": 1,
                "etag": "3c4008d74035dfaa1f0bb30d24468b12",
                "lastseen": "2021-04-02T13:07Z",
                "manufacturername": "Universal Electronics Inc",
                "modelid": "URC4450BC0-X-R",
                "name": "Keypad",
                "state": {
                    "action": "armed_away,1111,55",
                    "lastupdated": "2021-04-02T13:08:18.937",
                    "lowbattery": False,
                    "tampered": True,
                },
                "type": "ZHAAncillaryControl",
                "uniqueid": "00:0d:6f:00:13:4f:61:39-01-0501",
            }
        }
    }
    with patch.dict(DECONZ_WEB_REQUEST, data):
        await setup_deconz_integration(opp, aioclient_mock)

    device_registry = await opp.helpers.device_registry.async_get_registry()

    keypad_event_id = slugify(data["sensors"]["1"]["name"])
    keypad_serial = data["sensors"]["1"]["uniqueid"].split("-", 1)[0]
    keypad_entry = device_registry.async_get_device(
        identifiers={(DECONZ_DOMAIN, keypad_serial)})

    opp.config.components.add("recorder")
    assert await async_setup_component(opp, "logbook", {})
    entity_attr_cache = logbook.EntityAttributeCache(opp)

    events = list(
        logbook.humanify(
            opp,
            [
                MockLazyEventPartialState(
                    CONF_DECONZ_ALARM_EVENT,
                    {
                        CONF_CODE: 1234,
                        CONF_DEVICE_ID: keypad_entry.id,
                        CONF_EVENT: STATE_ALARM_ARMED_AWAY,
                        CONF_ID: keypad_event_id,
                        CONF_UNIQUE_ID: keypad_serial,
                    },
                ),
            ],
            entity_attr_cache,
            {},
        ))

    assert events[0]["name"] == "Keypad"
    assert events[0]["domain"] == "deconz"
    assert events[0]["message"] == "fired event 'armed_away'."