示例#1
0
async def sensor_none_fixture(
    hass: HomeAssistant,
    mock_entry: MockEntityFixture,
    mock_sensor: Sensor,
    now: datetime,
):
    """Fixture for a single sensor for testing the binary_sensor platform."""

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

    sensor_obj = mock_sensor.copy(deep=True)
    sensor_obj._api = mock_entry.api
    sensor_obj.name = "Test Sensor"
    sensor_obj.mount_type = MountType.LEAK
    sensor_obj.battery_status.is_low = False
    sensor_obj.alarm_settings.is_enabled = False
    sensor_obj.tampering_detected_at = None

    mock_entry.api.bootstrap.reset_objects()
    mock_entry.api.bootstrap.nvr.system_info.storage.devices = []
    mock_entry.api.bootstrap.sensors = {
        sensor_obj.id: sensor_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, 4, 4)

    yield sensor_obj

    Sensor.__config__.validate_assignment = True
示例#2
0
async def test_binary_sensor_update_mount_type_garage(
        hass: HomeAssistant, mock_entry: MockEntityFixture, sensor: Sensor):
    """Test binary_sensor motion entity."""

    _, entity_id = ids_from_device_description(Platform.BINARY_SENSOR, sensor,
                                               SENSE_SENSORS[0])

    state = hass.states.get(entity_id)
    assert state
    assert state.attributes[
        ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.DOOR.value

    new_bootstrap = copy(mock_entry.api.bootstrap)
    new_sensor = sensor.copy()
    new_sensor.mount_type = MountType.GARAGE

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

    new_bootstrap.sensors = {new_sensor.id: new_sensor}
    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.attributes[ATTR_DEVICE_CLASS] ==
            BinarySensorDeviceClass.GARAGE_DOOR.value)
示例#3
0
async def test_binary_sensor_update_mount_type_garage(
    hass: HomeAssistant, ufp: MockUFPFixture, sensor_all: Sensor
):
    """Test binary_sensor motion entity."""

    await init_entry(hass, ufp, [sensor_all])
    assert_entity_counts(hass, Platform.BINARY_SENSOR, 10, 10)

    _, entity_id = ids_from_device_description(
        Platform.BINARY_SENSOR, sensor_all, SENSE_SENSORS_WRITE[0]
    )

    state = hass.states.get(entity_id)
    assert state
    assert state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.DOOR.value

    new_sensor = sensor_all.copy()
    new_sensor.mount_type = MountType.GARAGE

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

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

    state = hass.states.get(entity_id)
    assert state
    assert (
        state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.GARAGE_DOOR.value
    )
示例#4
0
async def test_binary_sensor_setup_sensor_none(
    hass: HomeAssistant, ufp: MockUFPFixture, sensor: Sensor
):
    """Test binary_sensor entity setup for sensor with most sensors disabled."""

    sensor.mount_type = MountType.LEAK
    await init_entry(hass, ufp, [sensor])
    assert_entity_counts(hass, Platform.BINARY_SENSOR, 10, 10)

    entity_registry = er.async_get(hass)

    expected = [
        STATE_UNAVAILABLE,
        STATE_OFF,
        STATE_UNAVAILABLE,
        STATE_OFF,
    ]
    for index, description in enumerate(SENSE_SENSORS_WRITE):
        unique_id, entity_id = ids_from_device_description(
            Platform.BINARY_SENSOR, sensor, description
        )

        entity = entity_registry.async_get(entity_id)
        assert entity
        assert entity.unique_id == unique_id

        state = hass.states.get(entity_id)
        assert state
        assert state.state == expected[index]
        assert state.attributes[ATTR_ATTRIBUTION] == DEFAULT_ATTRIBUTION
示例#5
0
def csensor_all_fixture(sensor: Sensor):
    """Mock UniFi Protect Sensor device."""

    all_sensor = sensor.copy()
    all_sensor.light_settings.is_enabled = True
    all_sensor.humidity_settings.is_enabled = True
    all_sensor.temperature_settings.is_enabled = True
    all_sensor.alarm_settings.is_enabled = True
    all_sensor.led_settings.is_enabled = True
    all_sensor.motion_settings.is_enabled = True

    return all_sensor
示例#6
0
def sensor_fixture(fixed_now: datetime):
    """Mock UniFi Protect Sensor device."""

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

    data = json.loads(load_fixture("sample_sensor.json", integration=DOMAIN))
    sensor: Sensor = Sensor.from_unifi_dict(**data)
    sensor.motion_detected_at = fixed_now - timedelta(hours=1)
    sensor.open_status_changed_at = fixed_now - timedelta(hours=1)
    sensor.alarm_triggered_at = fixed_now - timedelta(hours=1)
    yield sensor

    Sensor.__config__.validate_assignment = True
示例#7
0
async def test_sensor_update_alarm(
    hass: HomeAssistant, mock_entry: MockEntityFixture, sensor: Sensor, now: datetime
):
    """Test sensor motion entity."""
    # 5 from all, 5 from sense, 12 NVR
    assert_entity_counts(hass, Platform.SENSOR, 22, 14)

    _, entity_id = ids_from_device_description(
        Platform.SENSOR, sensor, SENSE_SENSORS[4]
    )

    event_metadata = EventMetadata(sensor_id=sensor.id, alarm_type="smoke")
    event = Event(
        id="test_event_id",
        type=EventType.SENSOR_ALARM,
        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_sensor = sensor.copy()
    new_sensor.set_alarm_timeout()
    new_sensor.last_alarm_event_id = event.id

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

    new_bootstrap.sensors = {new_sensor.id: new_sensor}
    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 == "smoke"
    await time_changed(hass, 10)
示例#8
0
文件: test_init.py 项目: jbouwh/core
async def test_device_remove_devices(
    hass: HomeAssistant,
    ufp: MockUFPFixture,
    light: Light,
    doorlock: Doorlock,
    sensor: Sensor,
    hass_ws_client: Callable[[HomeAssistant],
                             Awaitable[aiohttp.ClientWebSocketResponse]],
) -> None:
    """Test we can only remove a device that no longer exists."""

    sensor.mac = "FFFFFFFFFFFF"

    await init_entry(hass,
                     ufp, [light, doorlock, sensor],
                     regenerate_ids=False)
    assert await async_setup_component(hass, "config", {})

    entry_id = ufp.entry.entry_id
    device_registry = dr.async_get(hass)

    light_device = get_device_from_ufp_device(hass, light)
    assert light_device is not None
    assert (await remove_device(await hass_ws_client(hass), light_device.id,
                                entry_id) is True)

    doorlock_device = get_device_from_ufp_device(hass, doorlock)
    assert (await remove_device(await hass_ws_client(hass), doorlock_device.id,
                                entry_id) is True)

    sensor_device = get_device_from_ufp_device(hass, sensor)
    assert sensor_device is None

    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)

    await time_changed(hass, 60)
    entry = hass.config_entries.async_get_entry(entry_id)
    entry.options[CONF_IGNORED] == f"{light.mac},{doorlock.mac}"
示例#9
0
async def test_sensor_update_alarm(hass: HomeAssistant, ufp: MockUFPFixture,
                                   sensor_all: Sensor, fixed_now: datetime):
    """Test sensor motion entity."""

    await init_entry(hass, ufp, [sensor_all])
    assert_entity_counts(hass, Platform.SENSOR, 22, 14)

    _, entity_id = ids_from_device_description(Platform.SENSOR, sensor_all,
                                               SENSE_SENSORS_WRITE[4])

    event_metadata = EventMetadata(sensor_id=sensor_all.id, alarm_type="smoke")
    event = Event(
        id="test_event_id",
        type=EventType.SENSOR_ALARM,
        start=fixed_now - timedelta(seconds=1),
        end=None,
        score=100,
        smart_detect_types=[],
        smart_detect_event_ids=[],
        metadata=event_metadata,
        api=ufp.api,
    )

    new_sensor = sensor_all.copy()
    new_sensor.set_alarm_timeout()
    new_sensor.last_alarm_event_id = event.id

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

    ufp.api.bootstrap.sensors = {new_sensor.id: new_sensor}
    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 == "smoke"
    await time_changed(hass, 10)
示例#10
0
文件: test_sensor.py 项目: 2Fake/core
async def sensor_fixture(
    hass: HomeAssistant,
    mock_entry: MockEntityFixture,
    mock_sensor: Sensor,
    now: datetime,
):
    """Fixture for a single sensor for testing the sensor platform."""

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

    sensor_obj = mock_sensor.copy(deep=True)
    sensor_obj._api = mock_entry.api
    sensor_obj.name = "Test Sensor"
    sensor_obj.battery_status.percentage = 10.0
    sensor_obj.light_settings.is_enabled = True
    sensor_obj.humidity_settings.is_enabled = True
    sensor_obj.temperature_settings.is_enabled = True
    sensor_obj.alarm_settings.is_enabled = True
    sensor_obj.stats.light.value = 10.0
    sensor_obj.stats.humidity.value = 10.0
    sensor_obj.stats.temperature.value = 10.0
    sensor_obj.up_since = now
    sensor_obj.bluetooth_connection_state.signal_strength = -50.0

    mock_entry.api.bootstrap.reset_objects()
    mock_entry.api.bootstrap.sensors = {
        sensor_obj.id: sensor_obj,
    }

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

    # 2 from all, 4 from sense, 12 NVR
    assert_entity_counts(hass, Platform.SENSOR, 19, 14)

    yield sensor_obj

    Sensor.__config__.validate_assignment = True
示例#11
0
def mock_sensor():
    """Mock UniFi Protect Sensor device."""

    data = json.loads(load_fixture("sample_sensor.json", integration=DOMAIN))
    return Sensor.from_unifi_dict(**data)