Пример #1
0
async def test_bulb_off_while_adding_in_ha(opp: OpenPeerPower):
    """Test Yeelight off while adding to ha, for example on OPP start."""
    config_entry = MockConfigEntry(
        domain=DOMAIN,
        data={
            **CONFIG_ENTRY_DATA,
            CONF_HOST: IP_ADDRESS,
        },
        unique_id=ID,
    )
    config_entry.add_to_opp(opp)

    mocked_bulb = _mocked_bulb(True)
    mocked_bulb.bulb_type = BulbType.WhiteTempMood

    with patch(f"{MODULE}.Bulb", return_value=mocked_bulb), patch(
            f"{MODULE}.config_flow.yeelight.Bulb", return_value=mocked_bulb):
        assert await opp.config_entries.async_setup(config_entry.entry_id)
        await opp.async_block_till_done()

    binary_sensor_entity_id = ENTITY_BINARY_SENSOR_TEMPLATE.format(
        IP_ADDRESS.replace(".", "_"))
    entity_registry = er.async_get(opp)
    assert entity_registry.async_get(binary_sensor_entity_id) is None

    type(mocked_bulb).get_capabilities = MagicMock(CAPABILITIES)
    type(mocked_bulb).get_properties = MagicMock(None)

    opp.data[DOMAIN][DATA_CONFIG_ENTRIES][
        config_entry.entry_id][DATA_DEVICE].update()
    await opp.async_block_till_done()
    await opp.async_block_till_done()

    entity_registry = er.async_get(opp)
    assert entity_registry.async_get(binary_sensor_entity_id) is not None
Пример #2
0
async def test_unique_ids_entry(opp: OpenPeerPower):
    """Test Yeelight unique IDs from entry IDs."""
    config_entry = MockConfigEntry(
        domain=DOMAIN,
        data={
            **CONFIG_ENTRY_DATA,
            CONF_NIGHTLIGHT_SWITCH: True,
        },
    )
    config_entry.add_to_opp(opp)

    mocked_bulb = _mocked_bulb()
    mocked_bulb.bulb_type = BulbType.WhiteTempMood

    with _patch_discovery(MODULE), patch(f"{MODULE}.Bulb",
                                         return_value=mocked_bulb):
        assert await opp.config_entries.async_setup(config_entry.entry_id)
        await opp.async_block_till_done()

    entity_registry = er.async_get(opp)
    assert (entity_registry.async_get(ENTITY_BINARY_SENSOR).unique_id ==
            f"{config_entry.entry_id}-nightlight_sensor")
    assert entity_registry.async_get(
        ENTITY_LIGHT).unique_id == config_entry.entry_id
    assert (entity_registry.async_get(ENTITY_NIGHTLIGHT).unique_id ==
            f"{config_entry.entry_id}-nightlight")
    assert (entity_registry.async_get(ENTITY_AMBILIGHT).unique_id ==
            f"{config_entry.entry_id}-ambilight")
Пример #3
0
async def test_ws_create(opp, opp_ws_client, storage_setup):
    """Test create WS."""
    assert await storage_setup(items=[])

    input_id = "new_input"
    input_entity_id = f"{DOMAIN}.{input_id}"
    ent_reg = er.async_get(opp)

    state = opp.states.get(input_entity_id)
    assert state is None
    assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None

    client = await opp_ws_client(opp)

    await client.send_json({
        "id": 6,
        "type": f"{DOMAIN}/create",
        "name": "New Input",
        "latitude": 3,
        "longitude": 4,
        "passive": True,
    })
    resp = await client.receive_json()
    assert resp["success"]

    state = opp.states.get(input_entity_id)
    assert state.state == "zoning"
    assert state.attributes["latitude"] == 3
    assert state.attributes["longitude"] == 4
    assert state.attributes["passive"] is True
Пример #4
0
async def test_cleanup_entity_registry_change(opp):
    """Test we run a cleanup when entity registry changes.

    Don't pre-load the registries as the debouncer will then not be waiting for
    EVENT_ENTITY_REGISTRY_UPDATED events.
    """
    await device_registry.async_load(opp)
    await entity_registry.async_load(opp)
    ent_reg = entity_registry.async_get(opp)

    with patch("openpeerpower.helpers.device_registry.Debouncer.async_call"
               ) as mock_call:
        entity = ent_reg.async_get_or_create("light", "hue", "e1")
        await opp.async_block_till_done()
        assert len(mock_call.mock_calls) == 0

        # Normal update does not trigger
        ent_reg.async_update_entity(entity.entity_id, name="updated")
        await opp.async_block_till_done()
        assert len(mock_call.mock_calls) == 0

        # Device ID update triggers
        ent_reg.async_get_or_create("light", "hue", "e1", device_id="bla")
        await opp.async_block_till_done()
        assert len(mock_call.mock_calls) == 1

        # Removal also triggers
        ent_reg.async_remove(entity.entity_id)
        await opp.async_block_till_done()
        assert len(mock_call.mock_calls) == 2
Пример #5
0
async def test_cleanup_device_registry(opp, registry):
    """Test cleanup works."""
    config_entry = MockConfigEntry(domain="hue")
    config_entry.add_to_opp(opp)

    d1 = registry.async_get_or_create(identifiers={("hue", "d1")},
                                      config_entry_id=config_entry.entry_id)
    registry.async_get_or_create(identifiers={("hue", "d2")},
                                 config_entry_id=config_entry.entry_id)
    d3 = registry.async_get_or_create(identifiers={("hue", "d3")},
                                      config_entry_id=config_entry.entry_id)
    registry.async_get_or_create(identifiers={("something", "d4")},
                                 config_entry_id="non_existing")

    ent_reg = entity_registry.async_get(opp)
    ent_reg.async_get_or_create("light", "hue", "e1", device_id=d1.id)
    ent_reg.async_get_or_create("light", "hue", "e2", device_id=d1.id)
    ent_reg.async_get_or_create("light", "hue", "e3", device_id=d3.id)

    device_registry.async_cleanup(opp, registry, ent_reg)

    assert registry.async_get_device({("hue", "d1")}) is not None
    assert registry.async_get_device({("hue", "d2")}) is not None
    assert registry.async_get_device({("hue", "d3")}) is not None
    assert registry.async_get_device({("something", "d4")}) is None
Пример #6
0
async def _create_entries(
        opp: OpenPeerPower,
        client=None) -> tuple[RegistryEntry, DeviceEntry, ClientMock]:
    client = ClientMock() if client is None else client

    def get_client_mock(client, _):
        return client

    with patch("twinkly_client.TwinklyClient", side_effect=get_client_mock):
        config_entry = MockConfigEntry(
            domain=TWINKLY_DOMAIN,
            data={
                CONF_ENTRY_HOST: client,
                CONF_ENTRY_ID: client.id,
                CONF_ENTRY_NAME: TEST_NAME_ORIGINAL,
                CONF_ENTRY_MODEL: TEST_MODEL,
            },
            entry_id=client.id,
        )
        config_entry.add_to_opp(opp)
        assert await opp.config_entries.async_setup(client.id)
        await opp.async_block_till_done()

    device_registry = dr.async_get(opp)
    entity_registry = er.async_get(opp)

    entity_id = entity_registry.async_get_entity_id("light", TWINKLY_DOMAIN,
                                                    client.id)
    entity = entity_registry.async_get(entity_id)
    device = device_registry.async_get_device({(TWINKLY_DOMAIN, client.id)})

    assert entity is not None
    assert device is not None

    return entity, device, client
Пример #7
0
async def test_simpleconnect_fan_setup(opp):
    """Test that a SIMPLEconnect fan can be correctly setup in HA."""
    accessories = await setup_accessories_from_file(opp,
                                                    "simpleconnect_fan.json")
    config_entry, pairing = await setup_test_accessories(opp, accessories)

    entity_registry = er.async_get(opp)

    # Check that the fan is correctly found and set up
    fan_id = "fan.simpleconnect_fan_06f674"
    fan = entity_registry.async_get(fan_id)
    assert fan.unique_id == "homekit-1234567890abcd-8"

    fan_helper = Helper(
        opp,
        "fan.simpleconnect_fan_06f674",
        pairing,
        accessories[0],
        config_entry,
    )

    fan_state = await fan_helper.poll_and_get_state()
    assert fan_state.attributes["friendly_name"] == "SIMPLEconnect Fan-06F674"
    assert fan_state.state == "off"
    assert fan_state.attributes["supported_features"] == (SUPPORT_DIRECTION
                                                          | SUPPORT_SET_SPEED)

    device_registry = dr.async_get(opp)

    device = device_registry.async_get(fan.device_id)
    assert device.manufacturer == "Hunter Fan"
    assert device.name == "SIMPLEconnect Fan-06F674"
    assert device.model == "SIMPLEconnect"
    assert device.sw_version == ""
    assert device.via_device_id is None
Пример #8
0
async def test_restoring_devices(opp):
    """Test restoring existing device_tracker entities if not detected on startup."""
    config_entry = MockConfigEntry(domain=mikrotik.DOMAIN,
                                   data=MOCK_DATA,
                                   options=MOCK_OPTIONS)
    config_entry.add_to_opp(opp)

    registry = er.async_get(opp)
    registry.async_get_or_create(
        device_tracker.DOMAIN,
        mikrotik.DOMAIN,
        "00:00:00:00:00:01",
        suggested_object_id="device_1",
        config_entry=config_entry,
    )
    registry.async_get_or_create(
        device_tracker.DOMAIN,
        mikrotik.DOMAIN,
        "00:00:00:00:00:02",
        suggested_object_id="device_2",
        config_entry=config_entry,
    )

    await setup_mikrotik_entry(opp)

    # test device_2 which is not in wireless list is restored
    device_1 = opp.states.get("device_tracker.device_1")
    assert device_1 is not None
    assert device_1.state == "home"
    device_2 = opp.states.get("device_tracker.device_2")
    assert device_2 is not None
    assert device_2.state == "not_home"
Пример #9
0
async def test_entity_registry(opp):
    """Tests that the devices are registered in the entity registry."""
    await setup_platform(opp, COVER_DOMAIN)
    entity_registry = er.async_get(opp)

    entry = entity_registry.async_get(DEVICE_ID)
    assert entry.unique_id == "61cbz3b542d2o33ed2fz02721bda3324"
Пример #10
0
async def test_hmip_remove_device(opp, default_mock_hap_factory):
    """Test Remove of hmip device."""
    entity_id = "light.treppe_ch"
    entity_name = "Treppe CH"
    device_model = "HmIP-BSL"
    mock_hap = await default_mock_hap_factory.async_get_mock_hap(
        test_devices=["Treppe"]
    )

    ha_state, hmip_device = get_and_check_entity_basics(
        opp, mock_hap, entity_id, entity_name, device_model
    )

    assert ha_state.state == STATE_ON
    assert hmip_device

    device_registry = dr.async_get(opp)
    entity_registry = er.async_get(opp)

    pre_device_count = len(device_registry.devices)
    pre_entity_count = len(entity_registry.entities)
    pre_mapping_count = len(mock_hap.hmip_device_by_entity_id)

    hmip_device.fire_remove_event()

    await opp.async_block_till_done()

    assert len(device_registry.devices) == pre_device_count - 1
    assert len(entity_registry.entities) == pre_entity_count - 3
    assert len(mock_hap.hmip_device_by_entity_id) == pre_mapping_count - 3
Пример #11
0
async def test_hmip_multi_area_device(opp, default_mock_hap_factory):
    """Test multi area device. Check if devices are created and referenced."""
    entity_id = "binary_sensor.wired_eingangsmodul_32_fach_channel5"
    entity_name = "Wired Eingangsmodul – 32-fach Channel5"
    device_model = "HmIPW-DRI32"
    mock_hap = await default_mock_hap_factory.async_get_mock_hap(
        test_devices=["Wired Eingangsmodul – 32-fach"]
    )

    ha_state, hmip_device = get_and_check_entity_basics(
        opp, mock_hap, entity_id, entity_name, device_model
    )
    assert ha_state

    # get the entity
    entity_registry = er.async_get(opp)
    entity = entity_registry.async_get(ha_state.entity_id)
    assert entity

    # get the device
    device_registry = dr.async_get(opp)
    device = device_registry.async_get(entity.device_id)
    assert device.name == "Wired Eingangsmodul – 32-fach"

    # get the hap
    hap_device = device_registry.async_get(device.via_device_id)
    assert hap_device.name == "Home"
Пример #12
0
async def test_hmip_remove_group(opp, default_mock_hap_factory):
    """Test Remove of hmip group."""
    entity_id = "switch.strom_group"
    entity_name = "Strom Group"
    device_model = None
    mock_hap = await default_mock_hap_factory.async_get_mock_hap(test_groups=["Strom"])

    ha_state, hmip_device = get_and_check_entity_basics(
        opp, mock_hap, entity_id, entity_name, device_model
    )

    assert ha_state.state == STATE_ON
    assert hmip_device

    device_registry = dr.async_get(opp)
    entity_registry = er.async_get(opp)

    pre_device_count = len(device_registry.devices)
    pre_entity_count = len(entity_registry.entities)
    pre_mapping_count = len(mock_hap.hmip_device_by_entity_id)

    hmip_device.fire_remove_event()
    await opp.async_block_till_done()

    assert len(device_registry.devices) == pre_device_count
    assert len(entity_registry.entities) == pre_entity_count - 1
    assert len(mock_hap.hmip_device_by_entity_id) == pre_mapping_count - 1
Пример #13
0
async def test_backupsses600m1(opp):
    """Test creation of BACKUPSES600M1 sensors."""

    await async_init_integration(opp, "BACKUPSES600M1", ["battery.charge"])
    registry = er.async_get(opp)
    entry = registry.async_get("sensor.ups1_battery_charge")
    assert entry
    assert (
        entry.unique_id ==
        "American Power Conversion_Back-UPS ES 600M1_4B1713P32195 _battery.charge"
    )

    state = opp.states.get("sensor.ups1_battery_charge")
    assert state.state == "100"

    expected_attributes = {
        "device_class": "battery",
        "friendly_name": "Ups1 Battery Charge",
        "state": "Online",
        "unit_of_measurement": PERCENTAGE,
    }
    # Only test for a subset of attributes in case
    # OPP changes the implementation and a new one appears
    assert all(state.attributes[key] == expected_attributes[key]
               for key in expected_attributes)
Пример #14
0
async def test_entity_registry(opp):
    """Tests that the devices are registered in the entity registry."""
    await setup_platform(opp, SENSOR_DOMAIN)
    entity_registry = er.async_get(opp)

    entry = entity_registry.async_get("sensor.environment_sensor_humidity")
    assert entry.unique_id == "13545b21f4bdcd33d9abd461f8443e65-humidity"
Пример #15
0
async def test_aqara_switch_setup(opp):
    """Test that a Aqara Switch can be correctly setup in HA."""
    accessories = await setup_accessories_from_file(opp, "aqara_switch.json")
    config_entry, pairing = await setup_test_accessories(opp, accessories)

    entity_registry = er.async_get(opp)

    battery_id = "sensor.programmable_switch_battery"
    battery = entity_registry.async_get(battery_id)
    assert battery.unique_id == "homekit-111a1111a1a111-5"

    # The fixture file has 1 button and a battery

    expected = [{
        "device_id": battery.device_id,
        "domain": "sensor",
        "entity_id": "sensor.programmable_switch_battery",
        "platform": "device",
        "type": "battery_level",
    }]

    for subtype in ("single_press", "double_press", "long_press"):
        expected.append({
            "device_id": battery.device_id,
            "domain": "homekit_controller",
            "platform": "device",
            "type": "button1",
            "subtype": subtype,
        })

    triggers = await async_get_device_automations(opp, "trigger",
                                                  battery.device_id)
    assert_lists_same(triggers, expected)
Пример #16
0
async def test_sensor_restore(opp, hk_driver, events):
    """Test setting up an entity from state in the event registry."""
    opp.state = CoreState.not_running

    registry = er.async_get(opp)

    registry.async_get_or_create(
        "sensor",
        "generic",
        "1234",
        suggested_object_id="temperature",
        device_class="temperature",
    )
    registry.async_get_or_create(
        "sensor",
        "generic",
        "12345",
        suggested_object_id="humidity",
        device_class="humidity",
        unit_of_measurement=PERCENTAGE,
    )
    opp.bus.async_fire(EVENT_OPENPEERPOWER_START, {})
    await opp.async_block_till_done()

    acc = get_accessory(opp, hk_driver, opp.states.get("sensor.temperature"),
                        2, {})
    assert acc.category == 10

    acc = get_accessory(opp, hk_driver, opp.states.get("sensor.humidity"), 2,
                        {})
    assert acc.category == 10
Пример #17
0
async def test_lights_can_be_enabled(opp: OpenPeerPower) -> None:
    """Verify lights can be enabled."""
    client = create_mock_client()
    await setup_test_config_entry(opp, hyperion_client=client)

    entity_registry = er.async_get(opp)
    entry = entity_registry.async_get(TEST_PRIORITY_LIGHT_ENTITY_ID_1)
    assert entry
    assert entry.disabled
    assert entry.disabled_by == er.DISABLED_INTEGRATION
    entity_state = opp.states.get(TEST_PRIORITY_LIGHT_ENTITY_ID_1)
    assert not entity_state

    with patch(
            "openpeerpower.components.hyperion.client.HyperionClient",
            return_value=client,
    ):
        updated_entry = entity_registry.async_update_entity(
            TEST_PRIORITY_LIGHT_ENTITY_ID_1, disabled_by=None)
        assert not updated_entry.disabled
        await opp.async_block_till_done()

        async_fire_time_changed(  # type: ignore[no-untyped-call]
            opp,
            dt.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
        )
        await opp.async_block_till_done()

    entity_state = opp.states.get(TEST_PRIORITY_LIGHT_ENTITY_ID_1)
    assert entity_state
Пример #18
0
async def test_entity_registry(opp):
    """Tests that the devices are registered in the entity registry."""
    await setup_platform(opp, LOCK_DOMAIN)
    entity_registry = er.async_get(opp)

    entry = entity_registry.async_get(DEVICE_ID)
    assert entry.unique_id == "51cab3b545d2o34ed7fz02731bda5324"
Пример #19
0
async def test_simpleconnect_cover_setup(opp):
    """Test that a velux gateway can be correctly setup in HA."""
    accessories = await setup_accessories_from_file(opp, "velux_gateway.json")
    config_entry, pairing = await setup_test_accessories(opp, accessories)

    entity_registry = er.async_get(opp)

    # Check that the cover is correctly found and set up
    cover_id = "cover.velux_window"
    cover = entity_registry.async_get(cover_id)
    assert cover.unique_id == "homekit-1111111a114a111a-8"

    cover_helper = Helper(
        opp,
        cover_id,
        pairing,
        accessories[0],
        config_entry,
    )

    cover_state = await cover_helper.poll_and_get_state()
    assert cover_state.attributes["friendly_name"] == "VELUX Window"
    assert cover_state.state == "closed"
    assert cover_state.attributes["supported_features"] == (
        SUPPORT_CLOSE | SUPPORT_SET_POSITION | SUPPORT_OPEN
    )

    # Check that one of the sensors is correctly found and set up
    sensor_id = "sensor.velux_sensor_temperature"
    sensor = entity_registry.async_get(sensor_id)
    assert sensor.unique_id == "homekit-a11b111-8"

    sensor_helper = Helper(
        opp,
        sensor_id,
        pairing,
        accessories[0],
        config_entry,
    )

    sensor_state = await sensor_helper.poll_and_get_state()
    assert sensor_state.attributes["friendly_name"] == "VELUX Sensor Temperature"
    assert sensor_state.state == "18.9"

    # The cover and sensor are different devices (accessories) attached to the same bridge
    assert cover.device_id != sensor.device_id

    device_registry = dr.async_get(opp)

    device = device_registry.async_get(cover.device_id)
    assert device.manufacturer == "VELUX"
    assert device.name == "VELUX Window"
    assert device.model == "VELUX Window"
    assert device.sw_version == "48"

    bridge = device_registry.async_get(device.via_device_id)
    assert bridge.manufacturer == "VELUX"
    assert bridge.name == "VELUX Gateway"
    assert bridge.model == "VELUX Gateway"
    assert bridge.sw_version == "70"
Пример #20
0
async def test_migration(opp):
    """Test migration from old data to new."""
    mock_config = MockConfigEntry(domain="test-platform",
                                  entry_id="test-config-id")

    old_conf = {
        "light.kitchen": {
            "config_entry_id": "test-config-id",
            "unique_id": "test-unique",
            "platform": "test-platform",
            "name": "Test Name",
            "disabled_by": er.DISABLED_OPP,
        }
    }
    with patch("os.path.isfile", return_value=True), patch("os.remove"), patch(
            "openpeerpower.helpers.entity_registry.load_yaml",
            return_value=old_conf):
        await er.async_load(opp)
        registry = er.async_get(opp)

    assert registry.async_is_registered("light.kitchen")
    entry = registry.async_get_or_create(
        domain="light",
        platform="test-platform",
        unique_id="test-unique",
        config_entry=mock_config,
    )
    assert entry.name == "Test Name"
    assert entry.disabled_by == er.DISABLED_OPP
    assert entry.config_entry_id == "test-config-id"
Пример #21
0
    async def async_configure_accessories(self):
        """Configure accessories for the included states."""
        dev_reg = device_registry.async_get(self.opp)
        ent_reg = entity_registry.async_get(self.opp)
        device_lookup = ent_reg.async_get_device_class_lookup({
            (BINARY_SENSOR_DOMAIN, DEVICE_CLASS_BATTERY_CHARGING),
            (BINARY_SENSOR_DOMAIN, DEVICE_CLASS_MOTION),
            (BINARY_SENSOR_DOMAIN, DEVICE_CLASS_OCCUPANCY),
            (SENSOR_DOMAIN, DEVICE_CLASS_BATTERY),
            (SENSOR_DOMAIN, DEVICE_CLASS_HUMIDITY),
        })

        entity_states = []
        for state in self.opp.states.async_all():
            entity_id = state.entity_id
            if not self._filter(entity_id):
                continue

            ent_reg_ent = ent_reg.async_get(entity_id)
            if ent_reg_ent:
                await self._async_set_device_info_attributes(
                    ent_reg_ent, dev_reg, entity_id)
                self._async_configure_linked_sensors(ent_reg_ent,
                                                     device_lookup, state)

            entity_states.append(state)

        return entity_states
Пример #22
0
async def test_state(opp):
    """Test state of the entity."""
    mocked_device = _create_mocked_device()
    entry = MockConfigEntry(domain=songpal.DOMAIN, data=CONF_DATA)
    entry.add_to_opp(opp)

    with _patch_media_player_device(mocked_device):
        await opp.config_entries.async_setup(entry.entry_id)
        await opp.async_block_till_done()

    state = opp.states.get(ENTITY_ID)
    assert state.name == FRIENDLY_NAME
    assert state.state == STATE_ON
    attributes = state.as_dict()["attributes"]
    assert attributes["volume_level"] == 0.5
    assert attributes["is_volume_muted"] is False
    assert attributes["source_list"] == ["title1", "title2"]
    assert attributes["source"] == "title2"
    assert attributes["supported_features"] == SUPPORT_SONGPAL

    device_registry = dr.async_get(opp)
    device = device_registry.async_get_device(identifiers={(songpal.DOMAIN,
                                                            MAC)})
    assert device.connections == {(dr.CONNECTION_NETWORK_MAC, MAC)}
    assert device.manufacturer == "Sony Corporation"
    assert device.name == FRIENDLY_NAME
    assert device.sw_version == SW_VERSION
    assert device.model == MODEL

    entity_registry = er.async_get(opp)
    entity = entity_registry.async_get(ENTITY_ID)
    assert entity.unique_id == MAC
Пример #23
0
async def test_ws_create(opp, opp_ws_client, storage_setup):
    """Test create WS."""
    assert await storage_setup(items=[])

    input_id = "new_input"
    input_entity_id = f"{DOMAIN}.{input_id}"
    ent_reg = er.async_get(opp)

    state = opp.states.get(input_entity_id)
    assert state is None
    assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None

    client = await opp_ws_client(opp)

    await client.send_json({
        "id": 6,
        "type": f"{DOMAIN}/create",
        "name": "New Input",
        "max": 20,
        "min": 0,
        "initial": 10,
        "step": 1,
        "mode": "slider",
    })
    resp = await client.receive_json()
    assert resp["success"]

    state = opp.states.get(input_entity_id)
    assert float(state.state) == 10
Пример #24
0
async def test_ws_create(opp, opp_ws_client, storage_setup):
    """Test create WS."""
    assert await storage_setup(items=[])

    input_id = "new_input"
    input_entity_id = f"{DOMAIN}.{input_id}"
    ent_reg = er.async_get(opp)

    state = opp.states.get(input_entity_id)
    assert state is None
    assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None

    client = await opp_ws_client(opp)

    await client.send_json(
        {
            "id": 6,
            "type": f"{DOMAIN}/create",
            "name": "New Input",
            "initial": "even newer option",
            ATTR_MAX: 44,
        }
    )
    resp = await client.receive_json()
    assert resp["success"]

    state = opp.states.get(input_entity_id)
    assert state.state == "even newer option"
    assert state.attributes[ATTR_FRIENDLY_NAME] == "New Input"
    assert state.attributes[ATTR_EDITABLE]
    assert state.attributes[ATTR_MAX] == 44
    assert state.attributes[ATTR_MIN] == 0
Пример #25
0
async def test_cleanup_device_registry_removes_expired_orphaned_devices(
        opp, registry):
    """Test cleanup removes expired orphaned devices."""
    config_entry = MockConfigEntry(domain="hue")
    config_entry.add_to_opp(opp)

    registry.async_get_or_create(identifiers={("hue", "d1")},
                                 config_entry_id=config_entry.entry_id)
    registry.async_get_or_create(identifiers={("hue", "d2")},
                                 config_entry_id=config_entry.entry_id)
    registry.async_get_or_create(identifiers={("hue", "d3")},
                                 config_entry_id=config_entry.entry_id)

    registry.async_clear_config_entry(config_entry.entry_id)
    assert len(registry.devices) == 0
    assert len(registry.deleted_devices) == 3

    ent_reg = entity_registry.async_get(opp)
    device_registry.async_cleanup(opp, registry, ent_reg)

    assert len(registry.devices) == 0
    assert len(registry.deleted_devices) == 3

    future_time = time.time(
    ) + device_registry.ORPHANED_DEVICE_KEEP_SECONDS + 1

    with patch("time.time", return_value=future_time):
        device_registry.async_cleanup(opp, registry, ent_reg)

    assert len(registry.devices) == 0
    assert len(registry.deleted_devices) == 0
Пример #26
0
async def test_sensors(opp: OpenPeerPower, device: DysonPureCoolLink,
                       sensors: list[str]) -> None:
    """Test the sensors."""
    # Temperature is given by the device in kelvin
    # Make sure no other sensors are set up
    assert len(opp.states.async_all()) == len(sensors)

    entity_registry = er.async_get(opp)
    for sensor in sensors:
        entity_id = _async_get_entity_id(sensor)

        # Test unique id
        assert entity_registry.async_get(
            entity_id).unique_id == f"{SERIAL}-{sensor}"

        # Test state
        state = opp.states.get(entity_id)
        assert state.state == str(MOCKED_VALUES[sensor])
        assert state.name == f"{NAME} {SENSOR_NAMES[sensor]}"

        # Test attributes
        attributes = state.attributes
        for attr, value in SENSOR_ATTRIBUTES[sensor].items():
            assert attributes[attr] == value

    # Test data update
    _async_assign_values(device, MOCKED_UPDATED_VALUES)
    await async_update_device(opp, device)
    for sensor in sensors:
        state = opp.states.get(_async_get_entity_id(sensor))
        assert state.state == str(MOCKED_UPDATED_VALUES[sensor])
Пример #27
0
async def test_ws_delete(opp, opp_ws_client, storage_setup):
    """Test WS delete cleans up entity registry."""
    assert await storage_setup()

    input_id = "from_storage"
    input_entity_id = f"{DOMAIN}.{input_id}"
    ent_reg = er.async_get(opp)

    state = opp.states.get(input_entity_id)
    assert state is not None
    assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None

    client = await opp_ws_client(opp)

    await client.send_json({
        "id": 6,
        "type": f"{DOMAIN}/delete",
        f"{DOMAIN}_id": f"{input_id}"
    })
    resp = await client.receive_json()
    assert resp["success"]

    state = opp.states.get(input_entity_id)
    assert state is None
    assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
Пример #28
0
async def test_config_parameter_sensor(opp, lock_id_lock_as_id150,
                                       integration):
    """Test config parameter sensor is created."""
    ent_reg = er.async_get(opp)
    entity_entry = ent_reg.async_get(ID_LOCK_CONFIG_PARAMETER_SENSOR)
    assert entity_entry
    assert entity_entry.disabled
Пример #29
0
async def test_enter_and_exit(opp, gpslogger_client, webhook_id):
    """Test when there is a known zone."""
    url = f"/api/webhook/{webhook_id}"

    data = {"latitude": HOME_LATITUDE, "longitude": HOME_LONGITUDE, "device": "123"}

    # Enter the Home
    req = await gpslogger_client.post(url, data=data)
    await opp.async_block_till_done()
    assert req.status == HTTP_OK
    state_name = opp.states.get(f"{DEVICE_TRACKER_DOMAIN}.{data['device']}").state
    assert state_name == STATE_HOME

    # Enter Home again
    req = await gpslogger_client.post(url, data=data)
    await opp.async_block_till_done()
    assert req.status == HTTP_OK
    state_name = opp.states.get(f"{DEVICE_TRACKER_DOMAIN}.{data['device']}").state
    assert state_name == STATE_HOME

    data["longitude"] = 0
    data["latitude"] = 0

    # Enter Somewhere else
    req = await gpslogger_client.post(url, data=data)
    await opp.async_block_till_done()
    assert req.status == HTTP_OK
    state_name = opp.states.get(f"{DEVICE_TRACKER_DOMAIN}.{data['device']}").state
    assert state_name == STATE_NOT_HOME

    dev_reg = dr.async_get(opp)
    assert len(dev_reg.devices) == 1

    ent_reg = er.async_get(opp)
    assert len(ent_reg.entities) == 1
Пример #30
0
async def test_koogeek_ls1_setup(opp):
    """Test that a Koogeek LS1 can be correctly setup in HA."""
    accessories = await setup_accessories_from_file(opp, "koogeek_ls1.json")
    config_entry, pairing = await setup_test_accessories(opp, accessories)

    entity_registry = er.async_get(opp)

    # Assert that the entity is correctly added to the entity registry
    entry = entity_registry.async_get("light.koogeek_ls1_20833f")
    assert entry.unique_id == "homekit-AAAA011111111111-7"

    helper = Helper(opp, "light.koogeek_ls1_20833f", pairing, accessories[0],
                    config_entry)
    state = await helper.poll_and_get_state()

    # Assert that the friendly name is detected correctly
    assert state.attributes["friendly_name"] == "Koogeek-LS1-20833F"

    # Assert that all optional features the LS1 supports are detected
    assert state.attributes["supported_features"] == (SUPPORT_BRIGHTNESS
                                                      | SUPPORT_COLOR)

    device_registry = dr.async_get(opp)

    device = device_registry.async_get(entry.device_id)
    assert device.manufacturer == "Koogeek"
    assert device.name == "Koogeek-LS1-20833F"
    assert device.model == "LS1"
    assert device.sw_version == "2.2.15"
    assert device.via_device_id is None