コード例 #1
0
async def test_humanify_hue_events_devices_removed(hass, mock_bridge_v2):
    """Test hue events when the devices have been removed from the registry."""
    await setup_platform(hass, mock_bridge_v2, "sensor")
    hass.config.components.add("recorder")
    assert await async_setup_component(hass, "logbook", {})
    await hass.async_block_till_done()

    (v1_event, v2_event) = mock_humanify(
        hass,
        [
            MockRow(
                ATTR_HUE_EVENT,
                SAMPLE_V1_EVENT,
            ),
            MockRow(
                ATTR_HUE_EVENT,
                SAMPLE_V2_EVENT,
            ),
        ],
    )

    assert v1_event["name"] == "hue_tap"
    assert v1_event["domain"] == DOMAIN
    assert v1_event["message"] == "Event 18"

    assert v2_event["name"] == "wall_switch_with_2_controls_button"
    assert v2_event["domain"] == DOMAIN
    assert v2_event["message"] == "first button pressed initially"
コード例 #2
0
async def test_logbook_humanify_script_started_event(hass):
    """Test humanifying script started event."""
    hass.config.components.add("recorder")
    await async_setup_component(hass, DOMAIN, {})
    await async_setup_component(hass, "logbook", {})

    event1, event2 = mock_humanify(
        hass,
        [
            MockRow(
                EVENT_SCRIPT_STARTED,
                {
                    ATTR_ENTITY_ID: "script.hello",
                    ATTR_NAME: "Hello Script"
                },
            ),
            MockRow(
                EVENT_SCRIPT_STARTED,
                {
                    ATTR_ENTITY_ID: "script.bye",
                    ATTR_NAME: "Bye Script"
                },
            ),
        ],
    )

    assert event1["name"] == "Hello Script"
    assert event1["domain"] == "script"
    assert event1["message"] == "started"
    assert event1["entity_id"] == "script.hello"

    assert event2["name"] == "Bye Script"
    assert event2["domain"] == "script"
    assert event2["message"] == "started"
    assert event2["entity_id"] == "script.bye"
コード例 #3
0
ファイル: test_init.py プロジェクト: jcgoette/core
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"})

    results = mock_humanify(
        hass,
        [
            MockRow(
                EVENT_ALEXA_SMART_HOME,
                {
                    "request": {
                        "namespace": "Alexa.Discovery",
                        "name": "Discover"
                    }
                },
            ),
            MockRow(
                EVENT_ALEXA_SMART_HOME,
                {
                    "request": {
                        "namespace": "Alexa.PowerController",
                        "name": "TurnOn",
                        "entity_id": "light.kitchen",
                    }
                },
            ),
            MockRow(
                EVENT_ALEXA_SMART_HOME,
                {
                    "request": {
                        "namespace": "Alexa.PowerController",
                        "name": "TurnOn",
                        "entity_id": "light.non_existing",
                    }
                },
            ),
        ],
    )

    event1, event2, event3 = results

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

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

    assert event3["name"] == "Amazon Alexa"
    assert (event3["message"] ==
            "sent command Alexa.PowerController/TurnOn for light.non_existing")
    assert event3["entity_id"] == "light.non_existing"
コード例 #4
0
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", {})

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

    events = mock_humanify(
        hass,
        [
            MockRow(
                EVENT_COMMAND_RECEIVED,
                {
                    "request_id":
                    "abcd",
                    ATTR_ENTITY_ID: ["light.kitchen"],
                    "execution": [{
                        "command": "action.devices.commands.OnOff",
                        "params": {
                            "on": True
                        },
                    }],
                    "source":
                    SOURCE_LOCAL,
                },
            ),
            MockRow(
                EVENT_COMMAND_RECEIVED,
                {
                    "request_id":
                    "abcd",
                    ATTR_ENTITY_ID: ["light.non_existing"],
                    "execution": [{
                        "command": "action.devices.commands.OnOff",
                        "params": {
                            "on": False
                        },
                    }],
                    "source":
                    SOURCE_CLOUD,
                },
            ),
        ],
    )

    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"
コード例 #5
0
ファイル: test_logbook.py プロジェクト: jbouwh/core
async def test_zha_logbook_event_device_no_device(hass, mock_devices):
    """Test zha logbook events without device and without triggers."""

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

    events = mock_humanify(
        hass,
        [
            MockRow(
                ZHA_EVENT,
                {
                    CONF_DEVICE_ID: "non-existing-device",
                    COMMAND: COMMAND_SHAKE,
                    "device_ieee": "90:fd:9f:ff:fe:fe:d8:a1",
                    CONF_UNIQUE_ID: "90:fd:9f:ff:fe:fe:d8:a1:1:0x0006",
                    "endpoint_id": 1,
                    "cluster_id": 6,
                    "params": {
                        "test": "test",
                    },
                },
            ),
        ],
    )

    assert events[0]["name"] == "Unknown device"
    assert events[0]["domain"] == "zha"
    assert (events[0]["message"] ==
            "Shake event was fired with parameters: {'test': 'test'}")
コード例 #6
0
async def test_humanifying_zwave_js_value_notification_event(
        hass, client, lock_schlage_be469, integration):
    """Test humanifying Z-Wave JS value notification events."""
    dev_reg = dr.async_get(hass)
    device = dev_reg.async_get_device(
        identifiers={get_device_id(client.driver, lock_schlage_be469)})
    assert device

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

    events = mock_humanify(
        hass,
        [
            MockRow(
                ZWAVE_JS_VALUE_NOTIFICATION_EVENT,
                {
                    "device_id": device.id,
                    "command_class": CommandClass.SCENE_ACTIVATION.value,
                    "command_class_name": "Scene Activation",
                    "label": "Scene ID",
                    "value": "001",
                },
            ),
        ],
    )

    assert events[0]["name"] == "Touchscreen Deadbolt"
    assert events[0]["domain"] == "zwave_js"
    assert (
        events[0]["message"] ==
        "fired Scene Activation CC 'value notification' event for 'Scene ID': '001'"
    )
コード例 #7
0
async def test_humanify_ios_events(hass):
    """Test humanifying ios events."""
    hass.config.components.add("recorder")
    assert await async_setup_component(hass, "logbook", {})
    await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
    hass.states.async_set(
        "zone.bad_place",
        "0",
        {
            ATTR_FRIENDLY_NAME: "passport control",
            ATTR_ICON: "mdi:airplane-marker"
        },
    )
    await hass.async_block_till_done()

    (event1, event2) = mock_humanify(
        hass,
        [
            MockRow(
                IOS_EVENT_ZONE_ENTERED,
                {
                    "sourceDeviceName": "test_phone",
                    "zone": "zone.happy_place"
                },
            ),
            MockRow(
                IOS_EVENT_ZONE_EXITED,
                {
                    "sourceDeviceName": "test_phone",
                    "zone": "zone.bad_place"
                },
            ),
        ],
    )

    assert event1["name"] == "test_phone"
    assert event1["domain"] == DOMAIN
    assert event1["message"] == "entered zone zone.happy_place"
    assert event1["icon"] == "mdi:crosshairs-gps"
    assert event1["entity_id"] == "zone.happy_place"

    assert event2["name"] == "test_phone"
    assert event2["domain"] == DOMAIN
    assert event2["message"] == "exited zone passport control"
    assert event2["icon"] == "mdi:airplane-marker"
    assert event2["entity_id"] == "zone.bad_place"
コード例 #8
0
async def test_humanify_elkm1_keypad_event(hass):
    """Test humanifying elkm1 keypad presses."""
    hass.config.components.add("recorder")
    assert await async_setup_component(hass, "logbook", {})
    config_entry = MockConfigEntry(
        domain=DOMAIN,
        data={CONF_HOST: "elks://1.2.3.4"},
        unique_id="aa:bb:cc:dd:ee:ff",
    )
    config_entry.add_to_hass(hass)

    with _patch_discovery(), _patch_elk():
        await hass.config_entries.async_setup(config_entry.entry_id)
        await hass.async_block_till_done()

    (event1, event2) = mock_humanify(
        hass,
        [
            MockRow(
                EVENT_ELKM1_KEYPAD_KEY_PRESSED,
                {
                    ATTR_KEYPAD_ID: 1,
                    ATTR_KEY_NAME: "four",
                    ATTR_KEY: "4",
                    ATTR_KEYPAD_NAME: "Main Bedroom",
                },
            ),
            MockRow(
                EVENT_ELKM1_KEYPAD_KEY_PRESSED,
                {
                    ATTR_KEYPAD_ID: 1,
                    ATTR_KEY_NAME: "five",
                    ATTR_KEY: "5",
                },
            ),
        ],
    )

    assert event1["name"] == "Elk Keypad Main Bedroom"
    assert event1["domain"] == DOMAIN
    assert event1["message"] == "pressed four (4)"

    assert event2["name"] == "Elk Keypad 1"
    assert event2["domain"] == DOMAIN
    assert event2["message"] == "pressed five (5)"
コード例 #9
0
async def test_humanify_shelly_click_event_rpc_device(hass, rpc_wrapper):
    """Test humanifying Shelly click event for rpc device."""
    assert rpc_wrapper
    hass.config.components.add("recorder")
    assert await async_setup_component(hass, "logbook", {})

    event1, event2 = mock_humanify(
        hass,
        [
            MockRow(
                EVENT_SHELLY_CLICK,
                {
                    ATTR_DEVICE_ID: rpc_wrapper.device_id,
                    ATTR_DEVICE: "shellyplus1pm-12345678",
                    ATTR_CLICK_TYPE: "single_push",
                    ATTR_CHANNEL: 1,
                },
            ),
            MockRow(
                EVENT_SHELLY_CLICK,
                {
                    ATTR_DEVICE_ID: "no_device_id",
                    ATTR_DEVICE: "shellypro4pm-12345678",
                    ATTR_CLICK_TYPE: "btn_down",
                    ATTR_CHANNEL: 2,
                },
            ),
        ],
    )

    assert event1["name"] == "Shelly"
    assert event1["domain"] == DOMAIN
    assert (event1["message"] ==
            "'single_push' click event for test switch_0 Input was fired.")

    assert event2["name"] == "Shelly"
    assert event2["domain"] == DOMAIN
    assert (
        event2["message"] ==
        "'btn_down' click event for shellypro4pm-12345678 channel 2 Input was fired."
    )
コード例 #10
0
async def test_humanify_automation_trigger_event(hass):
    """Test humanifying Shelly click event."""
    hass.config.components.add("recorder")
    assert await async_setup_component(hass, "automation", {})
    assert await async_setup_component(hass, "logbook", {})
    context = Context()

    event1, event2 = mock_humanify(
        hass,
        [
            MockRow(
                automation.EVENT_AUTOMATION_TRIGGERED,
                {
                    "name": "Bla",
                    "entity_id": "automation.bla",
                    "source": "state change of input_boolean.yo",
                },
                context=context,
            ),
            MockRow(
                automation.EVENT_AUTOMATION_TRIGGERED,
                {
                    "name": "Bla",
                    "entity_id": "automation.bla",
                },
                context=context,
            ),
        ],
    )

    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"
コード例 #11
0
ファイル: test_init.py プロジェクト: jcgoette/core
async def test_humanify_homekit_changed_event(hass, hk_driver,
                                              mock_get_source_ip):
    """Test humanifying HomeKit changed event."""
    hass.config.components.add("recorder")
    with patch("homeassistant.components.homekit.HomeKit"):
        assert await async_setup_component(hass, "homekit", {"homekit": {}})
    assert await async_setup_component(hass, "logbook", {})

    event1, event2 = mock_humanify(
        hass,
        [
            MockRow(
                EVENT_HOMEKIT_CHANGED,
                {
                    ATTR_ENTITY_ID: "lock.front_door",
                    ATTR_DISPLAY_NAME: "Front Door",
                    ATTR_SERVICE: "lock",
                },
            ),
            MockRow(
                EVENT_HOMEKIT_CHANGED,
                {
                    ATTR_ENTITY_ID: "cover.window",
                    ATTR_DISPLAY_NAME: "Window",
                    ATTR_SERVICE: "set_cover_position",
                    ATTR_VALUE: 75,
                },
            ),
        ],
    )

    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"
コード例 #12
0
async def test_humanify_shelly_click_event_block_device(hass, coap_wrapper):
    """Test humanifying Shelly click event for block device."""
    assert coap_wrapper
    hass.config.components.add("recorder")
    assert await async_setup_component(hass, "logbook", {})

    event1, event2 = mock_humanify(
        hass,
        [
            MockRow(
                EVENT_SHELLY_CLICK,
                {
                    ATTR_DEVICE_ID: coap_wrapper.device_id,
                    ATTR_DEVICE: "shellyix3-12345678",
                    ATTR_CLICK_TYPE: "single",
                    ATTR_CHANNEL: 1,
                },
            ),
            MockRow(
                EVENT_SHELLY_CLICK,
                {
                    ATTR_DEVICE_ID: "no_device_id",
                    ATTR_DEVICE: "shellyswitch25-12345678",
                    ATTR_CLICK_TYPE: "long",
                    ATTR_CHANNEL: 2,
                },
            ),
        ],
    )

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

    assert event2["name"] == "Shelly"
    assert event2["domain"] == DOMAIN
    assert (
        event2["message"] ==
        "'long' click event for shellyswitch25-12345678 channel 2 Input was fired."
    )
コード例 #13
0
async def test_humanify_hue_events(hass, mock_bridge_v2):
    """Test hue events when the devices are present in the registry."""
    await setup_platform(hass, mock_bridge_v2, "sensor")
    hass.config.components.add("recorder")
    assert await async_setup_component(hass, "logbook", {})
    await hass.async_block_till_done()
    entry: ConfigEntry = hass.config_entries.async_entries(DOMAIN)[0]

    dev_reg = device_registry.async_get(hass)
    v1_device = dev_reg.async_get_or_create(
        identifiers={(DOMAIN, "v1")}, name="Remote 1", config_entry_id=entry.entry_id
    )
    v2_device = dev_reg.async_get_or_create(
        identifiers={(DOMAIN, "v2")}, name="Remote 2", config_entry_id=entry.entry_id
    )

    (v1_event, v2_event) = mock_humanify(
        hass,
        [
            MockRow(
                ATTR_HUE_EVENT,
                {**SAMPLE_V1_EVENT, CONF_DEVICE_ID: v1_device.id},
            ),
            MockRow(
                ATTR_HUE_EVENT,
                {**SAMPLE_V2_EVENT, CONF_DEVICE_ID: v2_device.id},
            ),
        ],
    )

    assert v1_event["name"] == "Remote 1"
    assert v1_event["domain"] == DOMAIN
    assert v1_event["message"] == "Event 18"

    assert v2_event["name"] == "Remote 2"
    assert v2_event["domain"] == DOMAIN
    assert v2_event["message"] == "first button pressed initially"
コード例 #14
0
ファイル: test_logbook.py プロジェクト: jcgoette/core
async def test_humanify_lutron_caseta_button_event(hass):
    """Test humanifying lutron_caseta_button_events."""
    hass.config.components.add("recorder")
    assert await async_setup_component(hass, "logbook", {})
    config_entry = MockConfigEntry(
        domain=DOMAIN,
        data={
            CONF_HOST: "1.1.1.1",
            CONF_KEYFILE: "",
            CONF_CERTFILE: "",
            CONF_CA_CERTS: "",
        },
        unique_id="abc",
    )
    config_entry.add_to_hass(hass)

    with patch(
            "homeassistant.components.lutron_caseta.Smartbridge.create_tls",
            return_value=MockBridge(can_connect=True),
    ):
        await hass.config_entries.async_setup(config_entry.entry_id)
        await hass.async_block_till_done()

    await hass.async_block_till_done()

    (event1, ) = mock_humanify(
        hass,
        [
            MockRow(
                LUTRON_CASETA_BUTTON_EVENT,
                {
                    ATTR_SERIAL: "123",
                    ATTR_DEVICE_ID: "1234",
                    ATTR_TYPE: "Pico3ButtonRaiseLower",
                    ATTR_LEAP_BUTTON_NUMBER: 3,
                    ATTR_BUTTON_NUMBER: 3,
                    ATTR_DEVICE_NAME: "Pico",
                    ATTR_AREA_NAME: "Living Room",
                    ATTR_ACTION: "press",
                },
            ),
        ],
    )

    assert event1["name"] == "Living Room Pico"
    assert event1["domain"] == DOMAIN
    assert event1["message"] == "press raise"
コード例 #15
0
async def test_humanifying_zwave_js_notification_event(hass, client,
                                                       lock_schlage_be469,
                                                       integration):
    """Test humanifying Z-Wave JS notification events."""
    dev_reg = dr.async_get(hass)
    device = dev_reg.async_get_device(
        identifiers={get_device_id(client.driver, lock_schlage_be469)})
    assert device

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

    events = mock_humanify(
        hass,
        [
            MockRow(
                ZWAVE_JS_NOTIFICATION_EVENT,
                {
                    "device_id": device.id,
                    "command_class": CommandClass.NOTIFICATION.value,
                    "command_class_name": "Notification",
                    "label": "label",
                    "event_label": "event_label",
                },
            ),
            MockRow(
                ZWAVE_JS_NOTIFICATION_EVENT,
                {
                    "device_id": device.id,
                    "command_class": CommandClass.ENTRY_CONTROL.value,
                    "command_class_name": "Entry Control",
                    "event_type": 1,
                    "data_type": 2,
                },
            ),
            MockRow(
                ZWAVE_JS_NOTIFICATION_EVENT,
                {
                    "device_id": device.id,
                    "command_class": CommandClass.SWITCH_MULTILEVEL.value,
                    "command_class_name": "Multilevel Switch",
                    "event_type": 1,
                    "direction": "up",
                },
            ),
            MockRow(
                ZWAVE_JS_NOTIFICATION_EVENT,
                {
                    "device_id": device.id,
                    "command_class": CommandClass.POWERLEVEL.value,
                    "command_class_name": "Powerlevel",
                },
            ),
        ],
    )

    assert events[0]["name"] == "Touchscreen Deadbolt"
    assert events[0]["domain"] == "zwave_js"
    assert (
        events[0]["message"] ==
        "fired Notification CC 'notification' event 'label': 'event_label'")

    assert events[1]["name"] == "Touchscreen Deadbolt"
    assert events[1]["domain"] == "zwave_js"
    assert (
        events[1]["message"] ==
        "fired Entry Control CC 'notification' event for event type '1' with data type '2'"
    )

    assert events[2]["name"] == "Touchscreen Deadbolt"
    assert events[2]["domain"] == "zwave_js"
    assert (
        events[2]["message"] ==
        "fired Multilevel Switch CC 'notification' event for event type '1': 'up'"
    )

    assert events[3]["name"] == "Touchscreen Deadbolt"
    assert events[3]["domain"] == "zwave_js"
    assert events[3]["message"] == "fired Powerlevel CC 'notification' event"
コード例 #16
0
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", {})

    events = mock_humanify(
        hass,
        [
            MockRow(
                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,
                },
            ),
        ],
    )

    assert events[0]["name"] == "Keypad"
    assert events[0]["domain"] == "deconz"
    assert events[0]["message"] == "fired event 'armed_away'."
コード例 #17
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", {})

    events = mock_humanify(
        hass,
        [
            # Event without matching device trigger
            MockRow(
                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
            MockRow(
                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
            MockRow(
                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
            MockRow(
                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
            MockRow(
                CONF_DECONZ_EVENT,
                {
                    CONF_DEVICE_ID: faulty_entry.id,
                    "unknown_event": None,
                    CONF_ID: faulty_event_id,
                    CONF_UNIQUE_ID: faulty_serial,
                },
            ),
        ],
    )

    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."
コード例 #18
0
ファイル: test_logbook.py プロジェクト: jbouwh/core
async def test_zha_logbook_event_device_with_triggers(hass, mock_devices):
    """Test zha logbook events with device and triggers."""

    zigpy_device, zha_device = mock_devices

    zigpy_device.device_automation_triggers = {
        (SHAKEN, SHAKEN): {
            COMMAND: COMMAND_SHAKE
        },
        (UP, DOUBLE_PRESS): {
            COMMAND: COMMAND_DOUBLE,
            "endpoint_id": 1
        },
        (DOWN, DOUBLE_PRESS): {
            COMMAND: COMMAND_DOUBLE,
            "endpoint_id": 2
        },
        (SHORT_PRESS, SHORT_PRESS): {
            COMMAND: COMMAND_SINGLE
        },
        (LONG_PRESS, LONG_PRESS): {
            COMMAND: COMMAND_HOLD
        },
        (LONG_RELEASE, LONG_RELEASE): {
            COMMAND: COMMAND_HOLD
        },
    }

    ieee_address = str(zha_device.ieee)

    ha_device_registry = dr.async_get(hass)
    reg_device = ha_device_registry.async_get_device({("zha", ieee_address)})

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

    events = mock_humanify(
        hass,
        [
            MockRow(
                ZHA_EVENT,
                {
                    CONF_DEVICE_ID: reg_device.id,
                    COMMAND: COMMAND_SHAKE,
                    "device_ieee": str(ieee_address),
                    CONF_UNIQUE_ID: f"{str(ieee_address)}:1:0x0006",
                    "endpoint_id": 1,
                    "cluster_id": 6,
                    "params": {
                        "test": "test",
                    },
                },
            ),
            MockRow(
                ZHA_EVENT,
                {
                    CONF_DEVICE_ID: reg_device.id,
                    COMMAND: COMMAND_DOUBLE,
                    "device_ieee": str(ieee_address),
                    CONF_UNIQUE_ID: f"{str(ieee_address)}:1:0x0006",
                    "endpoint_id": 1,
                    "cluster_id": 6,
                    "params": {
                        "test": "test",
                    },
                },
            ),
            MockRow(
                ZHA_EVENT,
                {
                    CONF_DEVICE_ID: reg_device.id,
                    COMMAND: COMMAND_DOUBLE,
                    "device_ieee": str(ieee_address),
                    CONF_UNIQUE_ID: f"{str(ieee_address)}:1:0x0006",
                    "endpoint_id": 2,
                    "cluster_id": 6,
                    "params": {
                        "test": "test",
                    },
                },
            ),
        ],
    )

    assert events[0]["name"] == "FakeManufacturer FakeModel"
    assert events[0]["domain"] == "zha"
    assert (events[0]["message"] ==
            "Device Shaken event was fired with parameters: {'test': 'test'}")

    assert events[1]["name"] == "FakeManufacturer FakeModel"
    assert events[1]["domain"] == "zha"
    assert (
        events[1]["message"] ==
        "Up - Remote Button Double Press event was fired with parameters: {'test': 'test'}"
    )
コード例 #19
0
ファイル: test_logbook.py プロジェクト: jbouwh/core
async def test_zha_logbook_event_device_no_triggers(hass, mock_devices):
    """Test zha logbook events with device and without triggers."""

    zigpy_device, zha_device = mock_devices
    ieee_address = str(zha_device.ieee)
    ha_device_registry = dr.async_get(hass)
    reg_device = ha_device_registry.async_get_device({("zha", ieee_address)})

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

    events = mock_humanify(
        hass,
        [
            MockRow(
                ZHA_EVENT,
                {
                    CONF_DEVICE_ID: reg_device.id,
                    COMMAND: COMMAND_SHAKE,
                    "device_ieee": str(ieee_address),
                    CONF_UNIQUE_ID: f"{str(ieee_address)}:1:0x0006",
                    "endpoint_id": 1,
                    "cluster_id": 6,
                    "params": {
                        "test": "test",
                    },
                },
            ),
            MockRow(
                ZHA_EVENT,
                {
                    CONF_DEVICE_ID: reg_device.id,
                    "device_ieee": str(ieee_address),
                    CONF_UNIQUE_ID: f"{str(ieee_address)}:1:0x0006",
                    "endpoint_id": 1,
                    "cluster_id": 6,
                    "params": {
                        "test": "test",
                    },
                },
            ),
            MockRow(
                ZHA_EVENT,
                {
                    CONF_DEVICE_ID: reg_device.id,
                    "device_ieee": str(ieee_address),
                    CONF_UNIQUE_ID: f"{str(ieee_address)}:1:0x0006",
                    "endpoint_id": 1,
                    "cluster_id": 6,
                    "params": {},
                },
            ),
            MockRow(
                ZHA_EVENT,
                {
                    CONF_DEVICE_ID: reg_device.id,
                    "device_ieee": str(ieee_address),
                    CONF_UNIQUE_ID: f"{str(ieee_address)}:1:0x0006",
                    "endpoint_id": 1,
                    "cluster_id": 6,
                },
            ),
        ],
    )

    assert events[0]["name"] == "FakeManufacturer FakeModel"
    assert events[0]["domain"] == "zha"
    assert (events[0]["message"] ==
            "Shake event was fired with parameters: {'test': 'test'}")

    assert events[1]["name"] == "FakeManufacturer FakeModel"
    assert events[1]["domain"] == "zha"
    assert (events[1]["message"] ==
            "Zha Event was fired with parameters: {'test': 'test'}")

    assert events[2]["name"] == "FakeManufacturer FakeModel"
    assert events[2]["domain"] == "zha"
    assert events[2]["message"] == "Zha Event was fired"

    assert events[3]["name"] == "FakeManufacturer FakeModel"
    assert events[3]["domain"] == "zha"
    assert events[3]["message"] == "Zha Event was fired"