Beispiel #1
0
async def _create_entries(
        hass: HomeAssistant,
        client=None) -> tuple[RegistryEntry, DeviceEntry, ClientMock]:
    client = ClientMock() if client is None else client

    with patch("homeassistant.components.twinkly.Twinkly",
               return_value=client):
        config_entry = MockConfigEntry(
            domain=TWINKLY_DOMAIN,
            data={
                CONF_HOST: client,
                CONF_ID: client.id,
                CONF_NAME: TEST_NAME_ORIGINAL,
                CONF_MODEL: TEST_MODEL,
            },
            entry_id=client.id,
        )
        config_entry.add_to_hass(hass)
        assert await hass.config_entries.async_setup(client.id)
        await hass.async_block_till_done()

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

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

    assert entity_entry is not None
    assert device is not None

    return entity_entry, device, client, config_entry
Beispiel #2
0
async def test_load_unload_entry(hass: HomeAssistant):
    """Validate that setup entry also configure the client."""
    client = ClientMock()

    id = str(uuid4())
    config_entry = MockConfigEntry(
        domain=TWINKLY_DOMAIN,
        data={
            CONF_HOST: TEST_HOST,
            CONF_ID: id,
            CONF_NAME: TEST_NAME_ORIGINAL,
            CONF_MODEL: TEST_MODEL,
        },
        entry_id=id,
    )

    config_entry.add_to_hass(hass)

    with patch("homeassistant.components.twinkly.Twinkly", return_value=client):
        await hass.config_entries.async_setup(config_entry.entry_id)

    assert config_entry.state == ConfigEntryState.LOADED

    await hass.config_entries.async_unload(config_entry.entry_id)

    assert config_entry.state == ConfigEntryState.NOT_LOADED
Beispiel #3
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
Beispiel #4
0
async def test_success_flow(hass):
    """Test that an entity is created when the flow completes."""
    client = ClientMock()
    with patch("twinkly_client.TwinklyClient", return_value=client):
        result = await hass.config_entries.flow.async_init(
            TWINKLY_DOMAIN, context={"source": config_entries.SOURCE_USER}
        )

        assert result["type"] == "form"
        assert result["step_id"] == "user"
        assert result["errors"] == {}

        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {CONF_ENTRY_HOST: "dummy"},
        )

    assert result["type"] == "create_entry"
    assert result["title"] == client.id
    assert result["data"] == {
        CONF_ENTRY_HOST: "dummy",
        CONF_ENTRY_ID: client.id,
        CONF_ENTRY_NAME: client.id,
        CONF_ENTRY_MODEL: TEST_MODEL,
    }
Beispiel #5
0
async def test_turn_on(hass: HomeAssistant):
    """Test support of the light.turn_on service."""
    client = ClientMock()
    client.is_on = False
    client.brightness = 20
    entity, _, _ = await _create_entries(hass, client)

    assert hass.states.get(entity.entity_id).state == "off"

    await hass.services.async_call(
        "light", "turn_on", service_data={"entity_id": entity.entity_id})
    await hass.async_block_till_done()

    state = hass.states.get(entity.entity_id)

    assert state.state == "on"
    assert state.attributes["brightness"] == 51
Beispiel #6
0
async def test_turn_on_off(hass: HomeAssistant):
    """Test support of the light.turn_on service."""
    client = ClientMock()
    client.state = False
    client.brightness = {"mode": "enabled", "value": 20}
    entity, _, _, _ = await _create_entries(hass, client)

    assert hass.states.get(entity.entity_id).state == "off"

    await hass.services.async_call(
        "light", "turn_on", service_data={"entity_id": entity.entity_id})
    await hass.async_block_till_done()

    state = hass.states.get(entity.entity_id)

    assert state.state == "on"
    assert state.attributes[ATTR_BRIGHTNESS] == 51
Beispiel #7
0
async def test_initial_state_offline(opp: OpenPeerPower):
    """Validate that entity and device are restored from config is offline on startup."""
    client = ClientMock()
    client.is_offline = True
    entity, device, _ = await _create_entries(opp, client)

    state = opp.states.get(entity.entity_id)

    assert state.name == TEST_NAME_ORIGINAL
    assert state.state == "unavailable"
    assert state.attributes["friendly_name"] == TEST_NAME_ORIGINAL
    assert state.attributes["icon"] == "mdi:string-lights"

    assert entity.original_name == TEST_NAME_ORIGINAL
    assert entity.original_icon == "mdi:string-lights"

    assert device.name == TEST_NAME_ORIGINAL
    assert device.model == TEST_MODEL
    assert device.manufacturer == "LEDWORKS"
Beispiel #8
0
async def test_config_entry_not_ready(hass: HomeAssistant):
    """Validate that config entry is retried."""
    client = ClientMock()
    client.is_offline = True

    config_entry = MockConfigEntry(
        domain=TWINKLY_DOMAIN,
        data={
            CONF_HOST: TEST_HOST,
            CONF_ID: id,
            CONF_NAME: TEST_NAME_ORIGINAL,
            CONF_MODEL: TEST_MODEL,
        },
    )

    config_entry.add_to_hass(hass)

    with patch("homeassistant.components.twinkly.Twinkly", return_value=client):
        await hass.config_entries.async_setup(config_entry.entry_id)

    assert config_entry.state is ConfigEntryState.SETUP_RETRY
Beispiel #9
0
async def test_turn_on_with_brightness(opp: OpenPeerPower):
    """Test support of the light.turn_on service with a brightness parameter."""
    client = ClientMock()
    client.is_on = False
    client.brightness = 20
    entity, _, _ = await _create_entries(opp, client)

    assert opp.states.get(entity.entity_id).state == "off"

    await opp.services.async_call(
        "light",
        "turn_on",
        service_data={
            "entity_id": entity.entity_id,
            "brightness": 255
        },
    )
    await opp.async_block_till_done()

    state = opp.states.get(entity.entity_id)

    assert state.state == "on"
    assert state.attributes["brightness"] == 255
Beispiel #10
0
async def test_turn_on_with_color_rgb(hass: HomeAssistant):
    """Test support of the light.turn_on service with a brightness parameter."""
    client = ClientMock()
    client.state = False
    client.device_info["led_profile"] = "RGB"
    client.brightness = {"mode": "enabled", "value": 255}
    entity, _, _, _ = await _create_entries(hass, client)

    assert hass.states.get(entity.entity_id).state == "off"

    await hass.services.async_call(
        "light",
        "turn_on",
        service_data={
            "entity_id": entity.entity_id,
            "rgb_color": (128, 64, 32)
        },
    )
    await hass.async_block_till_done()

    state = hass.states.get(entity.entity_id)

    assert state.state == "on"
    assert client.color == (128, 64, 32)