Exemplo n.º 1
0
async def test_exclude_and_light_ids(
    hass: HomeAssistant, vera_component_factory: ComponentFactory, options
) -> None:
    """Test device exclusion, marking switches as lights and fixing the data type."""
    vera_device1 = MagicMock(spec=pv.VeraBinarySensor)  # type: pv.VeraBinarySensor
    vera_device1.device_id = 1
    vera_device1.vera_device_id = 1
    vera_device1.name = "dev1"
    vera_device1.is_tripped = False
    entity_id1 = "binary_sensor.dev1_1"

    vera_device2 = MagicMock(spec=pv.VeraBinarySensor)  # type: pv.VeraBinarySensor
    vera_device2.device_id = 2
    vera_device2.vera_device_id = 2
    vera_device2.name = "dev2"
    vera_device2.is_tripped = False
    entity_id2 = "binary_sensor.dev2_2"

    vera_device3 = MagicMock(spec=pv.VeraSwitch)  # type: pv.VeraSwitch
    vera_device3.device_id = 3
    vera_device3.vera_device_id = 3
    vera_device3.name = "dev3"
    vera_device3.category = pv.CATEGORY_SWITCH
    vera_device3.is_switched_on = MagicMock(return_value=False)
    entity_id3 = "switch.dev3_3"

    vera_device4 = MagicMock(spec=pv.VeraSwitch)  # type: pv.VeraSwitch
    vera_device4.device_id = 4
    vera_device4.vera_device_id = 4
    vera_device4.name = "dev4"
    vera_device4.category = pv.CATEGORY_SWITCH
    vera_device4.is_switched_on = MagicMock(return_value=False)
    entity_id4 = "light.dev4_4"

    component_data = await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            config_source=ConfigSource.CONFIG_ENTRY,
            devices=(vera_device1, vera_device2, vera_device3, vera_device4),
            config={**{CONF_CONTROLLER: "http://127.0.0.1:123"}, **options},
        ),
    )

    # Assert the entries were setup correctly.
    config_entry = next(iter(hass.config_entries.async_entries(DOMAIN)))
    assert config_entry.options[CONF_LIGHTS] == [4, 10, 12]
    assert config_entry.options[CONF_EXCLUDE] == [1]

    update_callback = component_data.controller_data[0].update_callback

    update_callback(vera_device1)
    update_callback(vera_device2)
    update_callback(vera_device3)
    update_callback(vera_device4)
    await hass.async_block_till_done()

    assert hass.states.get(entity_id1) is None
    assert hass.states.get(entity_id2) is not None
    assert hass.states.get(entity_id3) is not None
    assert hass.states.get(entity_id4) is not None
Exemplo n.º 2
0
async def test_multiple_controllers_with_legacy_one(
    hass: HomeAssistant, vera_component_factory: ComponentFactory
) -> None:
    """Test multiple controllers with one legacy controller."""
    vera_device1 = MagicMock(spec=pv.VeraBinarySensor)  # type: pv.VeraBinarySensor
    vera_device1.device_id = 1
    vera_device1.vera_device_id = vera_device1.device_id
    vera_device1.name = "first_dev"
    vera_device1.is_tripped = False
    entity1_id = "binary_sensor.first_dev_1"

    vera_device2 = MagicMock(spec=pv.VeraBinarySensor)  # type: pv.VeraBinarySensor
    vera_device2.device_id = 2
    vera_device2.vera_device_id = vera_device2.device_id
    vera_device2.name = "second_dev"
    vera_device2.is_tripped = False
    entity2_id = "binary_sensor.second_dev_2"

    # Add existing entity registry entry from previous setup.
    entity_registry = mock_registry(hass)
    entity_registry.async_get_or_create(
        domain="switch", platform=DOMAIN, unique_id="12"
    )

    await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            config={CONF_CONTROLLER: "http://127.0.0.1:111"},
            config_source=ConfigSource.FILE,
            serial_number="first_serial",
            devices=(vera_device1,),
        ),
    )

    await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            config={CONF_CONTROLLER: "http://127.0.0.1:222"},
            config_source=ConfigSource.CONFIG_FLOW,
            serial_number="second_serial",
            devices=(vera_device2,),
        ),
    )

    entity_registry = await hass.helpers.entity_registry.async_get_registry()

    entry1 = entity_registry.async_get(entity1_id)
    assert entry1
    assert entry1.unique_id == "1"

    entry2 = entity_registry.async_get(entity2_id)
    assert entry2
    assert entry2.unique_id == "vera_second_serial_2"
async def test_binary_sensor(hass: HomeAssistant,
                             vera_component_factory: ComponentFactory) -> None:
    """Test function."""
    vera_device = MagicMock(
        spec=pv.VeraBinarySensor)  # type: pv.VeraBinarySensor
    vera_device.device_id = 1
    vera_device.vera_device_id = vera_device.device_id
    vera_device.name = "dev1"
    vera_device.is_tripped = False
    entity_id = "binary_sensor.dev1_1"

    component_data = await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            devices=(vera_device, )),
    )
    update_callback = component_data.controller_data[0].update_callback

    vera_device.is_tripped = False
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "off"

    vera_device.is_tripped = True
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "on"
Exemplo n.º 4
0
async def run_sensor_test(
    hass: HomeAssistant,
    vera_component_factory: ComponentFactory,
    category: int,
    class_property: str,
    assert_states: Tuple[Tuple[Any, Any]],
    assert_unit_of_measurement: str = None,
    setup_callback: Callable[[pv.VeraController], None] = None,
) -> None:
    """Test generic sensor."""
    vera_device = MagicMock(spec=pv.VeraSensor)  # type: pv.VeraSensor
    vera_device.device_id = 1
    vera_device.name = "dev1"
    vera_device.category = category
    setattr(vera_device, class_property, "33")
    entity_id = "sensor.dev1_1"

    component_data = await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            devices=(vera_device, ), setup_callback=setup_callback),
    )
    update_callback = component_data.controller_data.update_callback

    for (initial_value, state_value) in assert_states:
        setattr(vera_device, class_property, initial_value)
        update_callback(vera_device)
        await hass.async_block_till_done()
        state = hass.states.get(entity_id)
        assert state.state == state_value
        if assert_unit_of_measurement:
            assert state.attributes[
                "unit_of_measurement"] == assert_unit_of_measurement
Exemplo n.º 5
0
async def test_init(hass: HomeAssistant,
                    vera_component_factory: ComponentFactory) -> None:
    """Test function."""
    vera_device1 = MagicMock(
        spec=pv.VeraBinarySensor)  # type: pv.VeraBinarySensor
    vera_device1.device_id = 1
    vera_device1.vera_device_id = 1
    vera_device1.name = "first_dev"
    vera_device1.is_tripped = False
    entity1_id = "binary_sensor.first_dev_1"

    await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            config={CONF_CONTROLLER: "http://127.0.0.1:111"},
            config_from_file=False,
            serial_number="first_serial",
            devices=(vera_device1, ),
        ),
    )

    entity_registry = await hass.helpers.entity_registry.async_get_registry()
    entry1 = entity_registry.async_get(entity1_id)

    assert entry1
Exemplo n.º 6
0
async def test_switch(hass: HomeAssistant,
                      vera_component_factory: ComponentFactory) -> None:
    """Test function."""
    vera_device = MagicMock(spec=pv.VeraSwitch)  # type: pv.VeraSwitch
    vera_device.device_id = 1
    vera_device.vera_device_id = vera_device.device_id
    vera_device.name = "dev1"
    vera_device.category = pv.CATEGORY_SWITCH
    vera_device.is_switched_on = MagicMock(return_value=False)
    entity_id = "switch.dev1_1"

    component_data = await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            devices=(vera_device, ), legacy_entity_unique_id=False),
    )
    update_callback = component_data.controller_data[0].update_callback

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

    await hass.services.async_call(
        "switch",
        "turn_on",
        {"entity_id": entity_id},
    )
    await hass.async_block_till_done()
    vera_device.switch_on.assert_called()
    vera_device.is_switched_on.return_value = True
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "on"

    await hass.services.async_call(
        "switch",
        "turn_off",
        {"entity_id": entity_id},
    )
    await hass.async_block_till_done()
    vera_device.switch_off.assert_called()
    vera_device.is_switched_on.return_value = False
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "off"
Exemplo n.º 7
0
async def test_lock(hass: HomeAssistant,
                    vera_component_factory: ComponentFactory) -> None:
    """Test function."""
    vera_device = MagicMock(spec=pv.VeraLock)  # type: pv.VeraLock
    vera_device.device_id = 1
    vera_device.vera_device_id = vera_device.device_id
    vera_device.name = "dev1"
    vera_device.category = pv.CATEGORY_LOCK
    vera_device.is_locked = MagicMock(return_value=False)
    entity_id = "lock.dev1_1"

    component_data = await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            devices=(vera_device, )),
    )
    update_callback = component_data.controller_data[0].update_callback

    assert hass.states.get(entity_id).state == STATE_UNLOCKED

    await hass.services.async_call(
        "lock",
        "lock",
        {"entity_id": entity_id},
    )
    await hass.async_block_till_done()
    vera_device.lock.assert_called()
    vera_device.is_locked.return_value = True
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == STATE_LOCKED

    await hass.services.async_call(
        "lock",
        "unlock",
        {"entity_id": entity_id},
    )
    await hass.async_block_till_done()
    vera_device.unlock.assert_called()
    vera_device.is_locked.return_value = False
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == STATE_UNLOCKED
Exemplo n.º 8
0
async def test_unload(hass: HomeAssistant,
                      vera_component_factory: ComponentFactory) -> None:
    """Test function."""
    vera_device1 = MagicMock(
        spec=pv.VeraBinarySensor)  # type: pv.VeraBinarySensor
    vera_device1.device_id = 1
    vera_device1.vera_device_id = 1
    vera_device1.name = "first_dev"
    vera_device1.is_tripped = False

    await vera_component_factory.configure_component(
        hass=hass, controller_config=new_simple_controller_config())

    entries = hass.config_entries.async_entries(DOMAIN)
    assert entries

    for config_entry in entries:
        assert await hass.config_entries.async_unload(config_entry.entry_id)
        assert config_entry.state == ENTRY_STATE_NOT_LOADED
Exemplo n.º 9
0
async def test_scene_controller_sensor(
        hass: HomeAssistant, vera_component_factory: ComponentFactory) -> None:
    """Test function."""
    vera_device = MagicMock(spec=pv.VeraSensor)  # type: pv.VeraSensor
    vera_device.device_id = 1
    vera_device.name = "dev1"
    vera_device.category = pv.CATEGORY_SCENE_CONTROLLER
    vera_device.get_last_scene_id = MagicMock(return_value="id0")
    vera_device.get_last_scene_time = MagicMock(return_value="0000")
    entity_id = "sensor.dev1_1"

    component_data = await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            devices=(vera_device, )),
    )
    update_callback = component_data.controller_data.update_callback

    vera_device.get_last_scene_time.return_value = "1111"
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "id0"
Exemplo n.º 10
0
async def test_climate_f(hass: HomeAssistant,
                         vera_component_factory: ComponentFactory) -> None:
    """Test function."""
    vera_device = MagicMock(spec=pv.VeraThermostat)  # type: pv.VeraThermostat
    vera_device.device_id = 1
    vera_device.name = "dev1"
    vera_device.category = pv.CATEGORY_THERMOSTAT
    vera_device.power = 10
    vera_device.get_current_temperature.return_value = 71
    vera_device.get_hvac_mode.return_value = "Off"
    vera_device.get_current_goal_temperature.return_value = 72
    entity_id = "climate.dev1_1"

    def setup_callback(controller: pv.VeraController) -> None:
        controller.temperature_units = "F"

    component_data = await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            devices=(vera_device, ), setup_callback=setup_callback),
    )
    update_callback = component_data.controller_data.update_callback

    await hass.services.async_call(
        "climate",
        "set_temperature",
        {
            "entity_id": entity_id,
            "temperature": 30
        },
    )
    await hass.async_block_till_done()
    vera_device.set_temperature.assert_called_with(86)
    vera_device.get_current_goal_temperature.return_value = 30
    vera_device.get_current_temperature.return_value = 25
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).attributes["current_temperature"] == -3.9
    assert hass.states.get(entity_id).attributes["temperature"] == -1.1
Exemplo n.º 11
0
async def test_climate(hass: HomeAssistant,
                       vera_component_factory: ComponentFactory) -> None:
    """Test function."""
    vera_device = MagicMock(spec=pv.VeraThermostat)  # type: pv.VeraThermostat
    vera_device.device_id = 1
    vera_device.name = "dev1"
    vera_device.category = pv.CATEGORY_THERMOSTAT
    vera_device.power = 10
    vera_device.get_current_temperature.return_value = 71
    vera_device.get_hvac_mode.return_value = "Off"
    vera_device.get_current_goal_temperature.return_value = 72
    entity_id = "climate.dev1_1"

    component_data = await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            devices=(vera_device, )),
    )
    update_callback = component_data.controller_data.update_callback

    assert hass.states.get(entity_id).state == HVAC_MODE_OFF

    await hass.services.async_call(
        "climate",
        "set_hvac_mode",
        {
            "entity_id": entity_id,
            "hvac_mode": HVAC_MODE_COOL
        },
    )
    await hass.async_block_till_done()
    vera_device.turn_cool_on.assert_called()
    vera_device.get_hvac_mode.return_value = "CoolOn"
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == HVAC_MODE_COOL

    await hass.services.async_call(
        "climate",
        "set_hvac_mode",
        {
            "entity_id": entity_id,
            "hvac_mode": HVAC_MODE_HEAT
        },
    )
    await hass.async_block_till_done()
    vera_device.turn_heat_on.assert_called()
    vera_device.get_hvac_mode.return_value = "HeatOn"
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == HVAC_MODE_HEAT

    await hass.services.async_call(
        "climate",
        "set_hvac_mode",
        {
            "entity_id": entity_id,
            "hvac_mode": HVAC_MODE_HEAT_COOL
        },
    )
    await hass.async_block_till_done()
    vera_device.turn_auto_on.assert_called()
    vera_device.get_hvac_mode.return_value = "AutoChangeOver"
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == HVAC_MODE_HEAT_COOL

    await hass.services.async_call(
        "climate",
        "set_hvac_mode",
        {
            "entity_id": entity_id,
            "hvac_mode": HVAC_MODE_OFF
        },
    )
    await hass.async_block_till_done()
    vera_device.turn_auto_on.assert_called()
    vera_device.get_hvac_mode.return_value = "Off"
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == HVAC_MODE_OFF

    await hass.services.async_call(
        "climate",
        "set_fan_mode",
        {
            "entity_id": entity_id,
            "fan_mode": "on"
        },
    )
    await hass.async_block_till_done()
    vera_device.turn_auto_on.assert_called()
    vera_device.get_fan_mode.return_value = "ContinuousOn"
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).attributes["fan_mode"] == FAN_ON

    await hass.services.async_call(
        "climate",
        "set_fan_mode",
        {
            "entity_id": entity_id,
            "fan_mode": "off"
        },
    )
    await hass.async_block_till_done()
    vera_device.turn_auto_on.assert_called()
    vera_device.get_fan_mode.return_value = "Auto"
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).attributes["fan_mode"] == FAN_AUTO

    await hass.services.async_call(
        "climate",
        "set_temperature",
        {
            "entity_id": entity_id,
            "temperature": 30
        },
    )
    await hass.async_block_till_done()
    vera_device.set_temperature.assert_called_with(30)
    vera_device.get_current_goal_temperature.return_value = 30
    vera_device.get_current_temperature.return_value = 25
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).attributes["current_temperature"] == 25
    assert hass.states.get(entity_id).attributes["temperature"] == 30
Exemplo n.º 12
0
async def test_light(hass: HomeAssistant,
                     vera_component_factory: ComponentFactory) -> None:
    """Test function."""
    vera_device = MagicMock(spec=pv.VeraDimmer)  # type: pv.VeraDimmer
    vera_device.device_id = 1
    vera_device.name = "dev1"
    vera_device.category = pv.CATEGORY_DIMMER
    vera_device.is_switched_on = MagicMock(return_value=False)
    vera_device.get_brightness = MagicMock(return_value=0)
    vera_device.get_color = MagicMock(return_value=[0, 0, 0])
    vera_device.is_dimmable = True
    entity_id = "light.dev1_1"

    component_data = await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            devices=(vera_device, )),
    )
    update_callback = component_data.controller_data.update_callback

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

    await hass.services.async_call(
        "light",
        "turn_on",
        {"entity_id": entity_id},
    )
    await hass.async_block_till_done()
    vera_device.switch_on.assert_called()
    vera_device.is_switched_on.return_value = True
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "on"

    await hass.services.async_call(
        "light",
        "turn_on",
        {
            "entity_id": entity_id,
            ATTR_HS_COLOR: [300, 70]
        },
    )
    await hass.async_block_till_done()
    vera_device.set_color.assert_called_with((255, 76, 255))
    vera_device.is_switched_on.return_value = True
    vera_device.get_color.return_value = (255, 76, 255)
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "on"
    assert hass.states.get(entity_id).attributes["hs_color"] == (300.0, 70.196)

    await hass.services.async_call(
        "light",
        "turn_on",
        {
            "entity_id": entity_id,
            ATTR_BRIGHTNESS: 55
        },
    )
    await hass.async_block_till_done()
    vera_device.set_brightness.assert_called_with(55)
    vera_device.is_switched_on.return_value = True
    vera_device.get_brightness.return_value = 55
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "on"
    assert hass.states.get(entity_id).attributes["brightness"] == 55

    await hass.services.async_call(
        "light",
        "turn_off",
        {"entity_id": entity_id},
    )
    await hass.async_block_till_done()
    vera_device.switch_off.assert_called()
    vera_device.is_switched_on.return_value = False
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "off"
Exemplo n.º 13
0
async def test_cover(hass: HomeAssistant,
                     vera_component_factory: ComponentFactory) -> None:
    """Test function."""
    vera_device = MagicMock(spec=pv.VeraCurtain)  # type: pv.VeraCurtain
    vera_device.device_id = 1
    vera_device.name = "dev1"
    vera_device.category = pv.CATEGORY_CURTAIN
    vera_device.is_closed = False
    vera_device.get_level.return_value = 0
    entity_id = "cover.dev1_1"

    component_data = await vera_component_factory.configure_component(
        hass=hass,
        controller_config=new_simple_controller_config(
            devices=(vera_device, )),
    )
    update_callback = component_data.controller_data.update_callback

    assert hass.states.get(entity_id).state == "closed"
    assert hass.states.get(entity_id).attributes["current_position"] == 0

    await hass.services.async_call(
        "cover",
        "open_cover",
        {"entity_id": entity_id},
    )
    await hass.async_block_till_done()
    vera_device.open.assert_called()
    vera_device.is_open.return_value = True
    vera_device.get_level.return_value = 100
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "open"
    assert hass.states.get(entity_id).attributes["current_position"] == 100

    await hass.services.async_call(
        "cover",
        "set_cover_position",
        {
            "entity_id": entity_id,
            "position": 50
        },
    )
    await hass.async_block_till_done()
    vera_device.set_level.assert_called_with(50)
    vera_device.is_open.return_value = True
    vera_device.get_level.return_value = 50
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "open"
    assert hass.states.get(entity_id).attributes["current_position"] == 50

    await hass.services.async_call(
        "cover",
        "stop_cover",
        {"entity_id": entity_id},
    )
    await hass.async_block_till_done()
    vera_device.stop.assert_called()
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "open"
    assert hass.states.get(entity_id).attributes["current_position"] == 50

    await hass.services.async_call(
        "cover",
        "close_cover",
        {"entity_id": entity_id},
    )
    await hass.async_block_till_done()
    vera_device.close.assert_called()
    vera_device.is_open.return_value = False
    vera_device.get_level.return_value = 00
    update_callback(vera_device)
    await hass.async_block_till_done()
    assert hass.states.get(entity_id).state == "closed"
    assert hass.states.get(entity_id).attributes["current_position"] == 00