示例#1
0
async def test_migrate_reboot_button(
    hass: HomeAssistant, mock_entry: MockEntityFixture, mock_light: Light
):
    """Test migrating unique ID of reboot button."""

    light1 = mock_light.copy()
    light1._api = mock_entry.api
    light1.name = "Test Light 1"
    light1.id = "lightid1"

    light2 = mock_light.copy()
    light2._api = mock_entry.api
    light2.name = "Test Light 2"
    light2.id = "lightid2"
    mock_entry.api.bootstrap.lights = {
        light1.id: light1,
        light2.id: light2,
    }
    mock_entry.api.get_bootstrap = AsyncMock(return_value=mock_entry.api.bootstrap)

    registry = er.async_get(hass)
    registry.async_get_or_create(
        Platform.BUTTON, DOMAIN, light1.id, config_entry=mock_entry.entry
    )
    registry.async_get_or_create(
        Platform.BUTTON,
        DOMAIN,
        f"{light2.id}_reboot",
        config_entry=mock_entry.entry,
    )

    await hass.config_entries.async_setup(mock_entry.entry.entry_id)
    await hass.async_block_till_done()

    assert mock_entry.entry.state == ConfigEntryState.LOADED
    assert mock_entry.api.update.called
    assert mock_entry.entry.unique_id == mock_entry.api.bootstrap.nvr.mac

    buttons = []
    for entity in er.async_entries_for_config_entry(
        registry, mock_entry.entry.entry_id
    ):
        if entity.domain == Platform.BUTTON.value:
            buttons.append(entity)
            print(entity.entity_id)
    assert len(buttons) == 2

    assert registry.async_get(f"{Platform.BUTTON}.test_light_1_reboot_device") is None
    assert registry.async_get(f"{Platform.BUTTON}.test_light_1_reboot_device_2") is None
    light = registry.async_get(f"{Platform.BUTTON}.unifiprotect_lightid1")
    assert light is not None
    assert light.unique_id == f"{light1.id}_reboot"

    assert registry.async_get(f"{Platform.BUTTON}.test_light_2_reboot_device") is None
    assert registry.async_get(f"{Platform.BUTTON}.test_light_2_reboot_device_2") is None
    light = registry.async_get(f"{Platform.BUTTON}.unifiprotect_lightid2_reboot")
    assert light is not None
    assert light.unique_id == f"{light2.id}_reboot"
示例#2
0
async def test_migrate_reboot_button(hass: HomeAssistant, ufp: MockUFPFixture,
                                     light: Light):
    """Test migrating unique ID of reboot button."""

    light1 = light.copy()
    light1.name = "Test Light 1"
    regenerate_device_ids(light1)

    light2 = light.copy()
    light2.name = "Test Light 2"
    regenerate_device_ids(light2)

    registry = er.async_get(hass)
    registry.async_get_or_create(Platform.BUTTON,
                                 DOMAIN,
                                 light1.id,
                                 config_entry=ufp.entry)
    registry.async_get_or_create(
        Platform.BUTTON,
        DOMAIN,
        f"{light2.mac}_reboot",
        config_entry=ufp.entry,
    )

    ufp.api.get_bootstrap = AsyncMock(return_value=ufp.api.bootstrap)
    await init_entry(hass, ufp, [light1, light2], regenerate_ids=False)

    assert ufp.entry.state == ConfigEntryState.LOADED
    assert ufp.api.update.called
    assert ufp.entry.unique_id == ufp.api.bootstrap.nvr.mac

    buttons = []
    for entity in er.async_entries_for_config_entry(registry,
                                                    ufp.entry.entry_id):
        if entity.domain == Platform.BUTTON.value:
            buttons.append(entity)
    assert len(buttons) == 4

    assert registry.async_get(
        f"{Platform.BUTTON}.test_light_1_reboot_device") is None
    assert registry.async_get(
        f"{Platform.BUTTON}.test_light_1_reboot_device_2") is None
    light = registry.async_get(
        f"{Platform.BUTTON}.unifiprotect_{light1.id.lower()}")
    assert light is not None
    assert light.unique_id == f"{light1.mac}_reboot"

    assert registry.async_get(
        f"{Platform.BUTTON}.test_light_2_reboot_device") is None
    assert registry.async_get(
        f"{Platform.BUTTON}.test_light_2_reboot_device_2") is None
    light = registry.async_get(
        f"{Platform.BUTTON}.unifiprotect_{light2.mac.lower()}_reboot")
    assert light is not None
    assert light.unique_id == f"{light2.mac}_reboot"
示例#3
0
文件: test_number.py 项目: 2Fake/core
async def light_fixture(hass: HomeAssistant, mock_entry: MockEntityFixture,
                        mock_light: Light):
    """Fixture for a single light for testing the number platform."""

    # disable pydantic validation so mocking can happen
    Light.__config__.validate_assignment = False

    light_obj = mock_light.copy(deep=True)
    light_obj._api = mock_entry.api
    light_obj.name = "Test Light"
    light_obj.light_device_settings.pir_sensitivity = 45
    light_obj.light_device_settings.pir_duration = timedelta(seconds=45)

    mock_entry.api.bootstrap.reset_objects()
    mock_entry.api.bootstrap.lights = {
        light_obj.id: light_obj,
    }

    await hass.config_entries.async_setup(mock_entry.entry.entry_id)
    await hass.async_block_till_done()

    assert_entity_counts(hass, Platform.NUMBER, 2, 2)

    yield light_obj

    Light.__config__.validate_assignment = True
示例#4
0
async def light_fixture(
    hass: HomeAssistant,
    mock_entry: MockEntityFixture,
    mock_light: Light,
    camera: Camera,
):
    """Fixture for a single light for testing the select platform."""

    # disable pydantic validation so mocking can happen
    Light.__config__.validate_assignment = False

    light_obj = mock_light.copy(deep=True)
    light_obj._api = mock_entry.api
    light_obj.name = "Test Light"
    light_obj.camera_id = None
    light_obj.light_mode_settings.mode = LightModeType.MOTION
    light_obj.light_mode_settings.enable_at = LightModeEnableType.DARK

    mock_entry.api.bootstrap.reset_objects()
    mock_entry.api.bootstrap.cameras = {camera.id: camera}
    mock_entry.api.bootstrap.lights = {
        light_obj.id: light_obj,
    }

    await hass.config_entries.async_reload(mock_entry.entry.entry_id)
    await hass.async_block_till_done()

    assert_entity_counts(hass, Platform.SELECT, 6, 6)

    yield light_obj

    Light.__config__.validate_assignment = True
示例#5
0
文件: test_switch.py 项目: 2Fake/core
async def light_fixture(hass: HomeAssistant, mock_entry: MockEntityFixture,
                        mock_light: Light):
    """Fixture for a single light for testing the switch platform."""

    # disable pydantic validation so mocking can happen
    Light.__config__.validate_assignment = False

    light_obj = mock_light.copy(deep=True)
    light_obj._api = mock_entry.api
    light_obj.name = "Test Light"
    light_obj.is_ssh_enabled = False
    light_obj.light_device_settings.is_indicator_enabled = False

    mock_entry.api.bootstrap.reset_objects()
    mock_entry.api.bootstrap.lights = {
        light_obj.id: light_obj,
    }

    await hass.config_entries.async_setup(mock_entry.entry.entry_id)
    await hass.async_block_till_done()

    assert_entity_counts(hass, Platform.SWITCH, 2, 1)

    yield light_obj

    Light.__config__.validate_assignment = True
示例#6
0
async def light_fixture(hass: HomeAssistant, mock_entry: MockEntityFixture,
                        mock_light: Light, now: datetime):
    """Fixture for a single light for testing the binary_sensor platform."""

    # disable pydantic validation so mocking can happen
    Light.__config__.validate_assignment = False

    light_obj = mock_light.copy(deep=True)
    light_obj._api = mock_entry.api
    light_obj.name = "Test Light"
    light_obj.is_dark = False
    light_obj.is_pir_motion_detected = False
    light_obj.last_motion = now - timedelta(hours=1)

    mock_entry.api.bootstrap.reset_objects()
    mock_entry.api.bootstrap.nvr.system_info.storage.devices = []
    mock_entry.api.bootstrap.lights = {
        light_obj.id: light_obj,
    }

    await hass.config_entries.async_setup(mock_entry.entry.entry_id)
    await hass.async_block_till_done()

    assert_entity_counts(hass, Platform.BINARY_SENSOR, 2, 2)

    yield light_obj

    Light.__config__.validate_assignment = True
示例#7
0
def unadopted_light(light: Light):
    """Mock UniFi Protect Light device (unadopted)."""

    no_light = light.copy()
    no_light.name = "Unadopted Light"
    no_light.is_adopted = False
    return no_light
示例#8
0
async def test_migrate_nvr_mac(hass: HomeAssistant, ufp: MockUFPFixture,
                               light: Light):
    """Test migrating unique ID of NVR to use MAC address."""

    light1 = light.copy()
    light1.name = "Test Light 1"
    regenerate_device_ids(light1)

    light2 = light.copy()
    light2.name = "Test Light 2"
    regenerate_device_ids(light2)

    nvr = ufp.api.bootstrap.nvr
    regenerate_device_ids(nvr)
    registry = er.async_get(hass)
    registry.async_get_or_create(
        Platform.SENSOR,
        DOMAIN,
        f"{nvr.id}_storage_utilization",
        config_entry=ufp.entry,
    )

    ufp.api.get_bootstrap = AsyncMock(return_value=ufp.api.bootstrap)
    await init_entry(hass, ufp, [light1, light2], regenerate_ids=False)

    assert ufp.entry.state == ConfigEntryState.LOADED
    assert ufp.api.update.called
    assert ufp.entry.unique_id == ufp.api.bootstrap.nvr.mac

    assert registry.async_get(
        f"{Platform.SENSOR}.{DOMAIN}_storage_utilization") is None
    assert (
        registry.async_get(f"{Platform.SENSOR}.{DOMAIN}_storage_utilization_2")
        is None)
    sensor = registry.async_get(
        f"{Platform.SENSOR}.{DOMAIN}_{nvr.id}_storage_utilization")
    assert sensor is not None
    assert sensor.unique_id == f"{nvr.mac}_storage_utilization"
示例#9
0
async def test_migrate_reboot_button_fail(hass: HomeAssistant,
                                          mock_entry: MockEntityFixture,
                                          mock_light: Light):
    """Test migrating unique ID of reboot button."""

    light1 = mock_light.copy()
    light1._api = mock_entry.api
    light1.name = "Test Light 1"
    light1.id = "lightid1"

    mock_entry.api.bootstrap.lights = {
        light1.id: light1,
    }
    mock_entry.api.get_bootstrap = AsyncMock(
        return_value=mock_entry.api.bootstrap)

    registry = er.async_get(hass)
    registry.async_get_or_create(
        Platform.BUTTON,
        DOMAIN,
        light1.id,
        config_entry=mock_entry.entry,
        suggested_object_id=light1.name,
    )
    registry.async_get_or_create(
        Platform.BUTTON,
        DOMAIN,
        f"{light1.id}_reboot",
        config_entry=mock_entry.entry,
        suggested_object_id=light1.name,
    )

    await hass.config_entries.async_setup(mock_entry.entry.entry_id)
    await hass.async_block_till_done()

    assert mock_entry.entry.state == ConfigEntryState.LOADED
    assert mock_entry.api.update.called
    assert mock_entry.entry.unique_id == mock_entry.api.bootstrap.nvr.mac

    light = registry.async_get(f"{Platform.BUTTON}.test_light_1")
    assert light is not None
    assert light.unique_id == f"{light1.id}"
示例#10
0
async def test_device_remove_devices(
    hass: HomeAssistant,
    mock_entry: MockEntityFixture,
    mock_light: Light,
    hass_ws_client: Callable[[HomeAssistant],
                             Awaitable[aiohttp.ClientWebSocketResponse]],
) -> None:
    """Test we can only remove a device that no longer exists."""
    assert await async_setup_component(hass, "config", {})

    light1 = mock_light.copy()
    light1._api = mock_entry.api
    light1.name = "Test Light 1"
    light1.id = "lightid1"
    light1.mac = "AABBCCDDEEFF"

    mock_entry.api.bootstrap.lights = {
        light1.id: light1,
    }

    mock_entry.api.get_bootstrap = AsyncMock(
        return_value=mock_entry.api.bootstrap)
    light_entity_id = "light.test_light_1"
    await hass.config_entries.async_setup(mock_entry.entry.entry_id)
    await hass.async_block_till_done()
    entry_id = mock_entry.entry.entry_id

    registry: er.EntityRegistry = er.async_get(hass)
    entity = registry.entities[light_entity_id]
    device_registry = dr.async_get(hass)

    live_device_entry = device_registry.async_get(entity.device_id)
    assert (await remove_device(await hass_ws_client(hass),
                                live_device_entry.id, entry_id) is False)

    dead_device_entry = device_registry.async_get_or_create(
        config_entry_id=entry_id,
        connections={(dr.CONNECTION_NETWORK_MAC, "e9:88:e7:b8:b4:40")},
    )
    assert (await remove_device(await hass_ws_client(hass),
                                dead_device_entry.id, entry_id) is True)
示例#11
0
async def test_binary_sensor_update_light_motion(
    hass: HomeAssistant, ufp: MockUFPFixture, light: Light, fixed_now: datetime
):
    """Test binary_sensor motion entity."""

    await init_entry(hass, ufp, [light])
    assert_entity_counts(hass, Platform.BINARY_SENSOR, 8, 8)

    _, entity_id = ids_from_device_description(
        Platform.BINARY_SENSOR, light, LIGHT_SENSOR_WRITE[1]
    )

    event_metadata = EventMetadata(light_id=light.id)
    event = Event(
        id="test_event_id",
        type=EventType.MOTION_LIGHT,
        start=fixed_now - timedelta(seconds=1),
        end=None,
        score=100,
        smart_detect_types=[],
        smart_detect_event_ids=[],
        metadata=event_metadata,
        api=ufp.api,
    )

    new_light = light.copy()
    new_light.is_pir_motion_detected = True
    new_light.last_motion_event_id = event.id

    mock_msg = Mock()
    mock_msg.changed_data = {}
    mock_msg.new_obj = event

    ufp.api.bootstrap.lights = {new_light.id: new_light}
    ufp.api.bootstrap.events = {event.id: event}
    ufp.ws_msg(mock_msg)
    await hass.async_block_till_done()

    state = hass.states.get(entity_id)
    assert state
    assert state.state == STATE_ON
示例#12
0
async def test_binary_sensor_update_light_motion(hass: HomeAssistant,
                                                 mock_entry: MockEntityFixture,
                                                 light: Light, now: datetime):
    """Test binary_sensor motion entity."""

    _, entity_id = ids_from_device_description(Platform.BINARY_SENSOR, light,
                                               LIGHT_SENSORS[1])

    event_metadata = EventMetadata(light_id=light.id)
    event = Event(
        id="test_event_id",
        type=EventType.MOTION_LIGHT,
        start=now - timedelta(seconds=1),
        end=None,
        score=100,
        smart_detect_types=[],
        smart_detect_event_ids=[],
        metadata=event_metadata,
        api=mock_entry.api,
    )

    new_bootstrap = copy(mock_entry.api.bootstrap)
    new_light = light.copy()
    new_light.is_pir_motion_detected = True
    new_light.last_motion_event_id = event.id

    mock_msg = Mock()
    mock_msg.changed_data = {}
    mock_msg.new_obj = event

    new_bootstrap.lights = {new_light.id: new_light}
    new_bootstrap.events = {event.id: event}
    mock_entry.api.bootstrap = new_bootstrap
    mock_entry.api.ws_subscription(mock_msg)
    await hass.async_block_till_done()

    state = hass.states.get(entity_id)
    assert state
    assert state.state == STATE_ON
示例#13
0
async def test_light_update(hass: HomeAssistant, ufp: MockUFPFixture,
                            light: Light, unadopted_light: Light):
    """Test light entity update."""

    await init_entry(hass, ufp, [light, unadopted_light])
    assert_entity_counts(hass, Platform.LIGHT, 1, 1)

    new_light = light.copy()
    new_light.is_light_on = True
    new_light.light_device_settings.led_level = LEDLevel(3)

    mock_msg = Mock()
    mock_msg.changed_data = {}
    mock_msg.new_obj = new_light

    ufp.api.bootstrap.lights = {new_light.id: new_light}
    ufp.ws_msg(mock_msg)
    await hass.async_block_till_done()

    state = hass.states.get("light.test_light")
    assert state
    assert state.state == STATE_ON
    assert state.attributes[ATTR_BRIGHTNESS] == 128
示例#14
0
async def light_fixture(hass: HomeAssistant, mock_entry: MockEntityFixture,
                        mock_light: Light):
    """Fixture for a single light for testing the light platform."""

    # disable pydantic validation so mocking can happen
    Light.__config__.validate_assignment = False

    light_obj = mock_light.copy(deep=True)
    light_obj._api = mock_entry.api
    light_obj.name = "Test Light"
    light_obj.is_light_on = False

    mock_entry.api.bootstrap.lights = {
        light_obj.id: light_obj,
    }

    await hass.config_entries.async_setup(mock_entry.entry.entry_id)
    await hass.async_block_till_done()

    assert_entity_counts(hass, Platform.LIGHT, 1, 1)

    yield (light_obj, "light.test_light")

    Light.__config__.validate_assignment = True