예제 #1
0
async def test_existing_node_not_ready(hass, client, multisensor_6,
                                       device_registry):
    """Test we handle a non ready node that exists during integration setup."""
    node = multisensor_6
    node.data = deepcopy(node.data)  # Copy to allow modification in tests.
    node.data["ready"] = False
    event = {"node": node}
    air_temperature_device_id = f"{client.driver.controller.home_id}-{node.node_id}"
    entry = MockConfigEntry(domain="zwave_js", data={"url": "ws://test.org"})
    entry.add_to_hass(hass)

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

    state = hass.states.get(AIR_TEMPERATURE_SENSOR)

    assert not state  # entity not yet added
    assert device_registry.async_get_device(  # device should be added
        identifiers={(DOMAIN, air_temperature_device_id)})

    node.data["ready"] = True
    node.emit("ready", event)
    await hass.async_block_till_done()

    state = hass.states.get(AIR_TEMPERATURE_SENSOR)

    assert state  # entity and device added
    assert state.state != STATE_UNAVAILABLE
    assert device_registry.async_get_device(
        identifiers={(DOMAIN, air_temperature_device_id)})
예제 #2
0
async def test_on_node_added_not_ready(hass, multisensor_6_state, client,
                                       integration, device_registry):
    """Test we handle a non ready node added event."""
    node_data = deepcopy(
        multisensor_6_state)  # Copy to allow modification in tests.
    node = Node(client, node_data)
    node.data["ready"] = False
    event = {"node": node}
    air_temperature_device_id = f"{client.driver.controller.home_id}-{node.node_id}"

    state = hass.states.get(AIR_TEMPERATURE_SENSOR)

    assert not state  # entity and device not yet added
    assert not device_registry.async_get_device(
        identifiers={(DOMAIN, air_temperature_device_id)})

    client.driver.controller.emit("node added", event)
    await hass.async_block_till_done()

    state = hass.states.get(AIR_TEMPERATURE_SENSOR)

    assert not state  # entity not yet added but device added in registry
    assert device_registry.async_get_device(
        identifiers={(DOMAIN, air_temperature_device_id)})

    node.data["ready"] = True
    node.emit("ready", event)
    await hass.async_block_till_done()

    state = hass.states.get(AIR_TEMPERATURE_SENSOR)

    assert state  # entity added
    assert state.state != STATE_UNAVAILABLE
예제 #3
0
async def test_existing_node_ready(hass, client, multisensor_6, integration,
                                   device_registry):
    """Test we handle a ready node that exists during integration setup."""
    node = multisensor_6
    air_temperature_device_id = f"{client.driver.controller.home_id}-{node.node_id}"

    state = hass.states.get(AIR_TEMPERATURE_SENSOR)

    assert state  # entity and device added
    assert state.state != STATE_UNAVAILABLE
    assert device_registry.async_get_device(
        identifiers={(DOMAIN, air_temperature_device_id)})
예제 #4
0
async def test_on_node_added_ready(hass, multisensor_6_state, client,
                                   integration, device_registry):
    """Test we handle a ready node added event."""
    node = Node(client, multisensor_6_state)
    event = {"node": node}
    air_temperature_device_id = f"{client.driver.controller.home_id}-{node.node_id}"

    state = hass.states.get(AIR_TEMPERATURE_SENSOR)

    assert not state  # entity and device not yet added
    assert not device_registry.async_get_device(
        identifiers={(DOMAIN, air_temperature_device_id)})

    client.driver.controller.emit("node added", event)
    await hass.async_block_till_done()

    state = hass.states.get(AIR_TEMPERATURE_SENSOR)

    assert state  # entity and device added
    assert state.state != STATE_UNAVAILABLE
    assert device_registry.async_get_device(
        identifiers={(DOMAIN, air_temperature_device_id)})
예제 #5
0
    async def async_step_add_remove_device(self, user_input=None):
        """Add/remove device step."""
        errors = {}
        if user_input is not None:
            _LOGGER.debug("async_step_add_remove_device: %s", user_input)
            if user_input[
                    CONF_MAC] and not user_input[CONF_DEVICE_DELETE_DEVICE]:
                if (self._sel_device and user_input[CONF_MAC].upper() !=
                        self._sel_device.get(CONF_MAC).upper()):
                    errors[CONF_MAC] = "cannot_change_mac"
                    user_input[CONF_MAC] = self._sel_device.get(CONF_MAC)
                else:
                    self.validate_mac(user_input[CONF_MAC], errors)
                    self.validate_key(user_input[CONF_DEVICE_ENCRYPTION_KEY],
                                      errors)
                if not errors:
                    # updating device configuration instead of overwriting
                    try:
                        self._devices[user_input["mac"].upper()].update(
                            copy.deepcopy(user_input))
                    except KeyError:
                        self._devices.update({
                            user_input["mac"].upper():
                            copy.deepcopy(user_input)
                        })
                    self._sel_device = {}  # prevent deletion
            if errors:
                retry_device_option_schema = vol.Schema({
                    vol.Optional(CONF_MAC, default=user_input[CONF_MAC]):
                    str,
                    vol.Optional(
                        CONF_DEVICE_ENCRYPTION_KEY,
                        default=user_input[CONF_DEVICE_ENCRYPTION_KEY],
                    ):
                    str,
                    vol.Optional(
                        CONF_TEMPERATURE_UNIT,
                        default=user_input[CONF_TEMPERATURE_UNIT],
                    ):
                    vol.In([TEMP_CELSIUS, TEMP_FAHRENHEIT]),
                    vol.Optional(
                        CONF_DEVICE_DECIMALS,
                        default=user_input[CONF_DEVICE_DECIMALS],
                    ):
                    vol.In([DEFAULT_DEVICE_DECIMALS, 0, 1, 2, 3]),
                    vol.Optional(
                        CONF_DEVICE_USE_MEDIAN,
                        default=user_input[CONF_DEVICE_USE_MEDIAN],
                    ):
                    vol.In([DEFAULT_DEVICE_USE_MEDIAN, True, False]),
                    vol.Optional(
                        CONF_DEVICE_RESTORE_STATE,
                        default=user_input[CONF_DEVICE_RESTORE_STATE],
                    ):
                    vol.In([DEFAULT_DEVICE_RESTORE_STATE, True, False]),
                    vol.Optional(
                        CONF_DEVICE_RESET_TIMER,
                        default=user_input[CONF_DEVICE_RESET_TIMER],
                    ):
                    cv.positive_int,
                    vol.Optional(
                        CONF_DEVICE_TRACK,
                        default=user_input[CONF_DEVICE_TRACK],
                    ):
                    cv.boolean,
                    vol.Optional(
                        CONF_DEVICE_TRACKER_SCAN_INTERVAL,
                        default=user_input[CONF_DEVICE_TRACKER_SCAN_INTERVAL],
                    ):
                    cv.positive_int,
                    vol.Optional(
                        CONF_DEVICE_TRACKER_CONSIDER_HOME,
                        default=user_input[CONF_DEVICE_TRACKER_CONSIDER_HOME],
                    ):
                    cv.positive_int,
                    vol.Optional(
                        CONF_DEVICE_DELETE_DEVICE,
                        default=DEFAULT_DEVICE_DELETE_DEVICE,
                    ):
                    cv.boolean,
                })
                return self.async_show_form(
                    step_id="add_remove_device",
                    data_schema=retry_device_option_schema,
                    errors=errors,
                )
            if self._sel_device:
                """Remove device from device registry."""
                device_registry = await self.hass.helpers.device_registry.async_get_registry(
                )
                mac = self._sel_device.get(CONF_MAC).upper()
                device = device_registry.async_get_device({(DOMAIN, mac)},
                                                          set())
                if device is None:
                    errors[CONF_MAC] = "cannot_delete_device"
                else:
                    _LOGGER.debug("Removing BLE monitor device %s", mac)
                    device_registry.async_remove_device(device.id)
                    del self._devices[self._sel_device.get(CONF_MAC).upper()]
            return self._show_main_form(errors)
        device_option_schema = vol.Schema({
            vol.Optional(
                CONF_MAC,
                default=self._sel_device.get(CONF_MAC, DEFAULT_DEVICE_MAC),
            ):
            str,
            vol.Optional(
                CONF_DEVICE_ENCRYPTION_KEY,
                default=self._sel_device.get(CONF_DEVICE_ENCRYPTION_KEY, DEFAULT_DEVICE_ENCRYPTION_KEY),
            ):
            str,
            vol.Optional(
                CONF_TEMPERATURE_UNIT,
                default=self._sel_device.get(CONF_TEMPERATURE_UNIT, TEMP_CELSIUS),
            ):
            vol.In([TEMP_CELSIUS, TEMP_FAHRENHEIT]),
            vol.Optional(
                CONF_DEVICE_DECIMALS,
                default=self._sel_device.get(CONF_DEVICE_DECIMALS, DEFAULT_DEVICE_DECIMALS),
            ):
            vol.In([DEFAULT_DEVICE_DECIMALS, 0, 1, 2, 3]),
            vol.Optional(
                CONF_DEVICE_USE_MEDIAN,
                default=self._sel_device.get(CONF_DEVICE_USE_MEDIAN, DEFAULT_DEVICE_USE_MEDIAN),
            ):
            vol.In([DEFAULT_DEVICE_USE_MEDIAN, True, False]),
            vol.Optional(
                CONF_DEVICE_RESTORE_STATE,
                default=self._sel_device.get(CONF_DEVICE_RESTORE_STATE, DEFAULT_DEVICE_RESTORE_STATE),
            ):
            vol.
            In([DEFAULT_DEVICE_RESTORE_STATE, True, False]),
            vol.Optional(
                CONF_DEVICE_RESET_TIMER,
                default=self._sel_device.get(CONF_DEVICE_RESET_TIMER, DEFAULT_DEVICE_RESET_TIMER),
            ):
            cv.positive_int,
            vol.Optional(
                CONF_DEVICE_TRACK,
                default=self._sel_device.get(CONF_DEVICE_TRACK, DEFAULT_DEVICE_TRACK),
            ):
            cv.boolean,
            vol.Optional(
                CONF_DEVICE_TRACKER_SCAN_INTERVAL,
                default=self._sel_device.get(
                    CONF_DEVICE_TRACKER_SCAN_INTERVAL, DEFAULT_DEVICE_TRACKER_SCAN_INTERVAL),
            ):
            cv.positive_int,
            vol.Optional(
                CONF_DEVICE_TRACKER_CONSIDER_HOME,
                default=self._sel_device.get(
                    CONF_DEVICE_TRACKER_CONSIDER_HOME, DEFAULT_DEVICE_TRACKER_CONSIDER_HOME),
            ):
            cv.positive_int,
            vol.Optional(
                CONF_DEVICE_DELETE_DEVICE,
                default=DEFAULT_DEVICE_DELETE_DEVICE,
            ):
            cv.boolean,
        })

        return self.async_show_form(
            step_id="add_remove_device",
            data_schema=device_option_schema,
            errors=errors,
        )