示例#1
0
async def _logbook_filtering(hass, last_changed, last_updated):
    # pylint: disable=import-outside-toplevel
    from homeassistant.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(hass)

    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(hass, event, entities_filter, entity_attr_cache):
                yield event

    start = timer()

    list(logbook.humanify(hass, yield_events(event), entity_attr_cache))

    return timer() - start
示例#2
0
async def test_humanifying_deconz_event(hass, aioclient_mock):
    """Test humanifying deCONZ event."""
    data = deepcopy(DECONZ_WEB_REQUEST)
    data["sensors"] = {
        "0": {
            "id": "Switch 1 id",
            "name": "Switch 1",
            "type": "ZHASwitch",
            "state": {
                "buttonevent": 1000
            },
            "config": {},
            "uniqueid": "00:00:00:00:00:00:00:01-00",
        },
        "1": {
            "id": "Hue remote id",
            "name": "Hue remote",
            "type": "ZHASwitch",
            "modelid": "RWL021",
            "state": {
                "buttonevent": 1000
            },
            "config": {},
            "uniqueid": "00:00:00:00:00:00:00:02-00",
        },
        "2": {
            "id": "Xiaomi cube id",
            "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",
        },
        "3": {
            "id": "faulty",
            "name": "Faulty event",
            "type": "ZHASwitch",
            "state": {},
            "config": {},
            "uniqueid": "00:00:00:00:00:00:00:04-00",
        },
    }
    config_entry = await setup_deconz_integration(hass,
                                                  aioclient_mock,
                                                  get_state_response=data)
    gateway = get_gateway_from_config_entry(hass, config_entry)

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

    events = list(
        logbook.humanify(
            hass,
            [
                # Event without matching device trigger
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: gateway.events[0].device_id,
                        CONF_EVENT: 2000,
                        CONF_ID: gateway.events[0].event_id,
                        CONF_UNIQUE_ID: gateway.events[0].serial,
                    },
                ),
                # Event with matching device trigger
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: gateway.events[1].device_id,
                        CONF_EVENT: 2001,
                        CONF_ID: gateway.events[1].event_id,
                        CONF_UNIQUE_ID: gateway.events[1].serial,
                    },
                ),
                # Gesture with matching device trigger
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: gateway.events[2].device_id,
                        CONF_GESTURE: 1,
                        CONF_ID: gateway.events[2].event_id,
                        CONF_UNIQUE_ID: gateway.events[2].serial,
                    },
                ),
                # Unsupported device trigger
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: gateway.events[2].device_id,
                        CONF_GESTURE: "unsupported_gesture",
                        CONF_ID: gateway.events[2].event_id,
                        CONF_UNIQUE_ID: gateway.events[2].serial,
                    },
                ),
                # Unknown event
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: gateway.events[3].device_id,
                        "unknown_event": None,
                        CONF_ID: gateway.events[3].event_id,
                        CONF_UNIQUE_ID: gateway.events[3].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."
示例#3
0
async def test_humanify_alexa_event(hass):
    """Test humanifying Alexa event."""
    hass.config.components.add("recorder")
    await async_setup_component(hass, "alexa", {})
    await async_setup_component(hass, "logbook", {})
    hass.states.async_set("light.kitchen", "on",
                          {"friendly_name": "Kitchen Light"})
    entity_attr_cache = logbook.EntityAttributeCache(hass)

    results = list(
        logbook.humanify(
            hass,
            [
                MockLazyEventPartialState(
                    EVENT_ALEXA_SMART_HOME,
                    {
                        "request": {
                            "namespace": "Alexa.Discovery",
                            "name": "Discover"
                        }
                    },
                ),
                MockLazyEventPartialState(
                    EVENT_ALEXA_SMART_HOME,
                    {
                        "request": {
                            "namespace": "Alexa.PowerController",
                            "name": "TurnOn",
                            "entity_id": "light.kitchen",
                        }
                    },
                ),
                MockLazyEventPartialState(
                    EVENT_ALEXA_SMART_HOME,
                    {
                        "request": {
                            "namespace": "Alexa.PowerController",
                            "name": "TurnOn",
                            "entity_id": "light.non_existing",
                        }
                    },
                ),
            ],
            entity_attr_cache,
        ))

    event1, event2, event3 = results

    assert event1["name"] == "Amazon Alexa"
    assert event1["message"] == "send command Alexa.Discovery/Discover"
    assert event1["entity_id"] is None

    assert event2["name"] == "Amazon Alexa"
    assert (event2["message"] ==
            "send command Alexa.PowerController/TurnOn for Kitchen Light")
    assert event2["entity_id"] == "light.kitchen"

    assert event3["name"] == "Amazon Alexa"
    assert (event3["message"] ==
            "send command Alexa.PowerController/TurnOn for light.non_existing")
    assert event3["entity_id"] == "light.non_existing"
示例#4
0
async def test_humanifying_deconz_event(hass, 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(hass, aioclient_mock)

    device_registry = await hass.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)})

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

    events = list(
        logbook.humanify(
            hass,
            [
                # 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."
async def test_humanifying_deconz_alarm_event(hass, 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(hass, aioclient_mock)

    device_registry = await hass.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)})

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

    events = list(
        logbook.humanify(
            hass,
            [
                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'."
async def test_humanify_command_received(hass):
    """Test humanifying command event."""
    hass.config.components.add("recorder")
    hass.config.components.add("frontend")
    hass.config.components.add("google_assistant")
    assert await async_setup_component(hass, "logbook", {})
    entity_attr_cache = logbook.EntityAttributeCache(hass)

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

    events = list(
        logbook.humanify(
            hass,
            [
                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"
示例#7
0
async def test_humanifying_deconz_event(hass):
    """Test humanifying deCONZ event."""
    data = deepcopy(DECONZ_WEB_REQUEST)
    data["sensors"] = {
        "0": {
            "id": "Switch 1 id",
            "name": "Switch 1",
            "type": "ZHASwitch",
            "state": {
                "buttonevent": 1000
            },
            "config": {},
            "uniqueid": "00:00:00:00:00:00:00:01-00",
        },
        "1": {
            "id": "Hue remote id",
            "name": "Hue remote",
            "type": "ZHASwitch",
            "modelid": "RWL021",
            "state": {
                "buttonevent": 1000
            },
            "config": {},
            "uniqueid": "00:00:00:00:00:00:00:02-00",
        },
    }
    config_entry = await setup_deconz_integration(hass,
                                                  get_state_response=data)
    gateway = get_gateway_from_config_entry(hass, config_entry)

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

    events = list(
        logbook.humanify(
            hass,
            [
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: gateway.events[0].device_id,
                        CONF_EVENT: 2000,
                        CONF_ID: gateway.events[0].event_id,
                        CONF_UNIQUE_ID: gateway.events[0].serial,
                    },
                ),
                MockLazyEventPartialState(
                    CONF_DECONZ_EVENT,
                    {
                        CONF_DEVICE_ID: gateway.events[1].device_id,
                        CONF_EVENT: 2001,
                        CONF_ID: gateway.events[1].event_id,
                        CONF_UNIQUE_ID: gateway.events[1].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."