Example #1
0
async def test_sensor_state(opp):
    """Test a configuration using a sensor in a template."""
    config = CONFIG_SENSOR[DOMAIN][CONF_ENTITIES]
    assert await async_setup_component(
        opp,
        SENSOR_DOMAIN,
        {SENSOR_DOMAIN: {
            "platform": "demo"
        }},
    )
    with patch(
            "sense_energy.SenseLink",
            return_value=Mock(start=AsyncMock(), close=AsyncMock()),
    ):
        assert await async_setup_component(opp, DOMAIN, CONFIG_SENSOR) is True
    await opp.async_block_till_done()
    await emulated_kasa.validate_configs(opp, config)

    opp.states.async_set(ENTITY_SENSOR, 35)

    sensor = opp.states.get(ENTITY_SENSOR)
    assert sensor.state == "35"

    # sensor
    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_SENSOR_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, 35)

    # change power sensor
    opp.states.async_set(ENTITY_SENSOR, 40)

    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_SENSOR_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, 40)

    # report 0 if device is off
    opp.states.async_set(ENTITY_SENSOR, 0)

    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_SENSOR_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, 0)
Example #2
0
async def test_float(opp):
    """Test a configuration using a simple float."""
    config = CONFIG_SWITCH[DOMAIN][CONF_ENTITIES]
    assert await async_setup_component(
        opp,
        SWITCH_DOMAIN,
        {SWITCH_DOMAIN: {
            "platform": "demo"
        }},
    )
    with patch(
            "sense_energy.SenseLink",
            return_value=Mock(start=AsyncMock(), close=AsyncMock()),
    ):
        assert await async_setup_component(opp, DOMAIN, CONFIG_SWITCH) is True
    await opp.async_block_till_done()
    await emulated_kasa.validate_configs(opp, config)

    # Turn switch on
    await opp.services.async_call(SWITCH_DOMAIN,
                                  SERVICE_TURN_ON,
                                  {ATTR_ENTITY_ID: ENTITY_SWITCH},
                                  blocking=True)

    switch = opp.states.get(ENTITY_SWITCH)
    assert switch.state == STATE_ON

    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()

    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_SWITCH_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, ENTITY_SWITCH_POWER)

    # Turn off
    await opp.services.async_call(SWITCH_DOMAIN,
                                  SERVICE_TURN_OFF,
                                  {ATTR_ENTITY_ID: ENTITY_SWITCH},
                                  blocking=True)

    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_SWITCH_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, 0)
Example #3
0
async def test_multiple_devices(opp):
    """Test that devices are reported correctly."""
    config = CONFIG[DOMAIN][CONF_ENTITIES]
    assert await async_setup_component(opp, SWITCH_DOMAIN,
                                       {SWITCH_DOMAIN: {
                                           "platform": "demo"
                                       }})
    assert await async_setup_component(opp, LIGHT_DOMAIN,
                                       {LIGHT_DOMAIN: {
                                           "platform": "demo"
                                       }})
    assert await async_setup_component(opp, FAN_DOMAIN,
                                       {FAN_DOMAIN: {
                                           "platform": "demo"
                                       }})
    assert await async_setup_component(
        opp,
        SENSOR_DOMAIN,
        {SENSOR_DOMAIN: {
            "platform": "demo"
        }},
    )
    with patch(
            "sense_energy.SenseLink",
            return_value=Mock(start=AsyncMock(), close=AsyncMock()),
    ):
        assert await emulated_kasa.async_setup(opp, CONFIG) is True
    await opp.async_block_till_done()
    await emulated_kasa.validate_configs(opp, config)

    # Turn all devices on to known state
    await opp.services.async_call(SWITCH_DOMAIN,
                                  SERVICE_TURN_ON,
                                  {ATTR_ENTITY_ID: ENTITY_SWITCH},
                                  blocking=True)
    await opp.services.async_call(LIGHT_DOMAIN,
                                  SERVICE_TURN_ON,
                                  {ATTR_ENTITY_ID: ENTITY_LIGHT},
                                  blocking=True)
    opp.states.async_set(ENTITY_SENSOR, 35)
    await opp.services.async_call(FAN_DOMAIN,
                                  SERVICE_TURN_ON,
                                  {ATTR_ENTITY_ID: ENTITY_FAN},
                                  blocking=True)
    await opp.services.async_call(
        FAN_DOMAIN,
        SERVICE_SET_SPEED,
        {
            ATTR_ENTITY_ID: ENTITY_FAN,
            ATTR_SPEED: "medium"
        },
        blocking=True,
    )

    # All of them should now be on
    switch = opp.states.get(ENTITY_SWITCH)
    assert switch.state == STATE_ON
    light = opp.states.get(ENTITY_LIGHT)
    assert light.state == STATE_ON
    sensor = opp.states.get(ENTITY_SENSOR)
    assert sensor.state == "35"
    fan = opp.states.get(ENTITY_FAN)
    assert fan.state == STATE_ON

    plug_it = emulated_kasa.get_plug_devices(opp, config)
    # switch
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_SWITCH_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, ENTITY_SWITCH_POWER)

    # light
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_LIGHT_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, 35)

    # fan
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_FAN_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, ENTITY_FAN_SPEED_MED)

    # No more devices
    assert next(plug_it, None) is None
Example #4
0
async def test_sensor(opp):
    """Test a configuration using a sensor in a template."""
    config = CONFIG_LIGHT[DOMAIN][CONF_ENTITIES]
    assert await async_setup_component(opp, LIGHT_DOMAIN,
                                       {LIGHT_DOMAIN: {
                                           "platform": "demo"
                                       }})
    assert await async_setup_component(
        opp,
        SENSOR_DOMAIN,
        {SENSOR_DOMAIN: {
            "platform": "demo"
        }},
    )
    with patch(
            "sense_energy.SenseLink",
            return_value=Mock(start=AsyncMock(), close=AsyncMock()),
    ):
        assert await async_setup_component(opp, DOMAIN, CONFIG_LIGHT) is True
    await opp.async_block_till_done()
    await emulated_kasa.validate_configs(opp, config)

    await opp.services.async_call(LIGHT_DOMAIN,
                                  SERVICE_TURN_ON,
                                  {ATTR_ENTITY_ID: ENTITY_LIGHT},
                                  blocking=True)
    opp.states.async_set(ENTITY_SENSOR, 35)

    light = opp.states.get(ENTITY_LIGHT)
    assert light.state == STATE_ON
    sensor = opp.states.get(ENTITY_SENSOR)
    assert sensor.state == "35"

    # light
    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_LIGHT_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, 35)

    # change power sensor
    opp.states.async_set(ENTITY_SENSOR, 40)

    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_LIGHT_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, 40)

    # report 0 if device is off
    await opp.services.async_call(LIGHT_DOMAIN,
                                  SERVICE_TURN_OFF,
                                  {ATTR_ENTITY_ID: ENTITY_LIGHT},
                                  blocking=True)

    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_LIGHT_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, 0)
Example #5
0
async def test_template(opp):
    """Test a configuration using a complex template."""
    config = CONFIG_FAN[DOMAIN][CONF_ENTITIES]
    assert await async_setup_component(opp, FAN_DOMAIN,
                                       {FAN_DOMAIN: {
                                           "platform": "demo"
                                       }})
    with patch(
            "sense_energy.SenseLink",
            return_value=Mock(start=AsyncMock(), close=AsyncMock()),
    ):
        assert await async_setup_component(opp, DOMAIN, CONFIG_FAN) is True
    await opp.async_block_till_done()
    await emulated_kasa.validate_configs(opp, config)

    # Turn all devices on to known state
    await opp.services.async_call(FAN_DOMAIN,
                                  SERVICE_TURN_ON,
                                  {ATTR_ENTITY_ID: ENTITY_FAN},
                                  blocking=True)
    await opp.services.async_call(
        FAN_DOMAIN,
        SERVICE_SET_SPEED,
        {
            ATTR_ENTITY_ID: ENTITY_FAN,
            ATTR_SPEED: "low"
        },
        blocking=True,
    )

    fan = opp.states.get(ENTITY_FAN)
    assert fan.state == STATE_ON

    # Fan low:
    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_FAN_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, ENTITY_FAN_SPEED_LOW)

    # Fan High:
    await opp.services.async_call(
        FAN_DOMAIN,
        SERVICE_SET_SPEED,
        {
            ATTR_ENTITY_ID: ENTITY_FAN,
            ATTR_SPEED: "high"
        },
        blocking=True,
    )
    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_FAN_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, ENTITY_FAN_SPEED_HIGH)

    # Fan off:
    await opp.services.async_call(FAN_DOMAIN,
                                  SERVICE_TURN_OFF,
                                  {ATTR_ENTITY_ID: ENTITY_FAN},
                                  blocking=True)
    plug_it = emulated_kasa.get_plug_devices(opp, config)
    plug = next(plug_it).generate_response()
    assert nested_value(plug, "system", "get_sysinfo",
                        "alias") == ENTITY_FAN_NAME
    power = nested_value(plug, "emeter", "get_realtime", "power")
    assert math.isclose(power, 0)