Пример #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
Пример #2
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
def _generate_mock_feed_entry(
    external_id,
    title,
    distance_to_home,
    coordinates,
    category=None,
    location=None,
    attribution=None,
    publication_date=None,
    council_area=None,
    status=None,
    entry_type=None,
    fire=True,
    size=None,
    responsible_agency=None,
):
    """Construct a mock feed entry for testing purposes."""
    feed_entry = MagicMock()
    feed_entry.external_id = external_id
    feed_entry.title = title
    feed_entry.distance_to_home = distance_to_home
    feed_entry.coordinates = coordinates
    feed_entry.category = category
    feed_entry.location = location
    feed_entry.attribution = attribution
    feed_entry.publication_date = publication_date
    feed_entry.council_area = council_area
    feed_entry.status = status
    feed_entry.type = entry_type
    feed_entry.fire = fire
    feed_entry.size = size
    feed_entry.responsible_agency = responsible_agency
    return feed_entry
Пример #4
0
 def _generate_mock_feed_entry(external_id, title, distance_to_home,
                               coordinates, category):
     """Construct a mock feed entry for testing purposes."""
     feed_entry = MagicMock()
     feed_entry.external_id = external_id
     feed_entry.title = title
     feed_entry.distance_to_home = distance_to_home
     feed_entry.coordinates = coordinates
     feed_entry.category = category
     return feed_entry
Пример #5
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"
Пример #6
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
Пример #7
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"
Пример #8
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
Пример #9
0
def _generate_mock_feed_entry(
    external_id,
    title,
    distance_to_home,
    coordinates,
    category=None,
    attribution=None,
    published=None,
    updated=None,
    status=None,
):
    """Construct a mock feed entry for testing purposes."""
    feed_entry = MagicMock()
    feed_entry.external_id = external_id
    feed_entry.title = title
    feed_entry.distance_to_home = distance_to_home
    feed_entry.coordinates = coordinates
    feed_entry.category = category
    feed_entry.attribution = attribution
    feed_entry.published = published
    feed_entry.updated = updated
    feed_entry.status = status
    return feed_entry
Пример #10
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
Пример #11
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"
Пример #12
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