<commit_before>"""The tests for the Input text component."""
# pylint: disable=protected-access
from unittest.mock import patch

import pytest

from homeassistant.components.input_text import (
    ATTR_MAX,
    ATTR_MIN,
    ATTR_MODE,
    ATTR_VALUE,
    CONF_INITIAL,
    CONF_MAX_VALUE,
    CONF_MIN_VALUE,
    DOMAIN,
    MODE_TEXT,
    SERVICE_SET_VALUE,
)
from homeassistant.const import (
    ATTR_EDITABLE,
    ATTR_ENTITY_ID,
    ATTR_FRIENDLY_NAME,
    ATTR_NAME,
    SERVICE_RELOAD,
)
from homeassistant.core import Context, CoreState, State
from homeassistant.exceptions import Unauthorized
from homeassistant.helpers import entity_registry as er
from homeassistant.loader import bind_hass
from homeassistant.setup import async_setup_component

from tests.common import mock_restore_cache

TEST_VAL_MIN = 2
TEST_VAL_MAX = 22


@pytest.fixture
def storage_setup(hass, hass_storage):
    """Storage setup."""

    async def _storage(items=None, config=None):
        if items is None:
            hass_storage[DOMAIN] = {
                "key": DOMAIN,
                "version": 1,
                "data": {
                    "items": [
                        {
                            "id": "from_storage",
                            "name": "from storage",
                            "initial": "loaded from storage",
                            ATTR_MAX: TEST_VAL_MAX,
                            ATTR_MIN: TEST_VAL_MIN,
                            ATTR_MODE: MODE_TEXT,
                        }
                    ]
                },
            }
        else:
            hass_storage[DOMAIN] = {
                "key": DOMAIN,
                "version": 1,
                "data": {"items": items},
            }
        if config is None:
            config = {DOMAIN: {}}
        return await async_setup_component(hass, DOMAIN, config)

    return _storage


@bind_hass
def set_value(hass, entity_id, value):
    """Set input_text to value.

    This is a legacy helper method. Do not use it for new tests.
    """
    hass.async_create_task(
        hass.services.async_call(
            DOMAIN, SERVICE_SET_VALUE, {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: value}
        )
    )


async def test_config(hass):
    """Test config."""
    invalid_configs = [
        None,
        {},
        {"name with space": None},
        {"test_1": {"min": 50, "max": 50}},
    ]
    for cfg in invalid_configs:
        assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg})


async def test_set_value(hass):
    """Test set_value method."""
    assert await async_setup_component(
        hass, DOMAIN, {DOMAIN: {"test_1": {"initial": "test", "min": 3, "max": 10}}}
    )
    entity_id = "input_text.test_1"

    state = hass.states.get(entity_id)
    assert str(state.state) == "test"

    set_value(hass, entity_id, "testing")
    await hass.async_block_till_done()

    state = hass.states.get(entity_id)
    assert str(state.state) == "testing"

    set_value(hass, entity_id, "testing too long")
    await hass.async_block_till_done()

    state = hass.states.get(entity_id)
    assert str(state.state) == "testing"


async def test_mode(hass):
    """Test mode settings."""
    assert await async_setup_component(
        hass,
        DOMAIN,
        {
            DOMAIN: {
                "test_default_text": {"initial": "test", "min": 3, "max": 10},
                "test_explicit_text": {<commit_msg>Add extra tests for input text (#70283)<commit_after>"""The tests for the Input text component."""
# pylint: disable=protected-access
from unittest.mock import patch

import pytest

from homeassistant.components.input_text import (
    ATTR_MAX,
    ATTR_MIN,
    ATTR_MODE,
    ATTR_VALUE,
    CONF_INITIAL,
    CONF_MAX_VALUE,
    CONF_MIN_VALUE,
    DOMAIN,
    MODE_TEXT,
    SERVICE_SET_VALUE,
)
from homeassistant.const import (
    ATTR_EDITABLE,
    ATTR_ENTITY_ID,
    ATTR_FRIENDLY_NAME,
    ATTR_NAME,
    SERVICE_RELOAD,
)
from homeassistant.core import Context, CoreState, State
from homeassistant.exceptions import Unauthorized
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component

from tests.common import mock_restore_cache

TEST_VAL_MIN = 2
TEST_VAL_MAX = 22


@pytest.fixture
def storage_setup(hass, hass_storage):
    """Storage setup."""

    async def _storage(items=None, config=None):
        if items is None:
            hass_storage[DOMAIN] = {
                "key": DOMAIN,
                "version": 1,
                "data": {
                    "items": [
                        {
                            "id": "from_storage",
                            "name": "from storage",
                            "initial": "loaded from storage",
                            ATTR_MAX: TEST_VAL_MAX,
                            ATTR_MIN: TEST_VAL_MIN,
                            ATTR_MODE: MODE_TEXT,
                        }
                    ]
                },
            }
        else:
            hass_storage[DOMAIN] = {
                "key": DOMAIN,
                "version": 1,
                "data": {"items": items},
            }
        if config is None:
            config = {DOMAIN: {}}
        return await async_setup_component(hass, DOMAIN, config)

    return _storage


async def async_set_value(hass, entity_id, value):
    """Set input_text to value."""
    await hass.services.async_call(
        DOMAIN,
        SERVICE_SET_VALUE,
        {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: value},
        blocking=True,
    )


async def test_config(hass):
    """Test config."""
    invalid_configs = [
        None,
        {},
        {"name with space": None},
        {"test_1": {"min": 50, "max": 50}},
    ]
    for cfg in invalid_configs:
        assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg})


async def test_set_value(hass):
    """Test set_value method."""
    assert await async_setup_component(
        hass,
        DOMAIN,
        {
            DOMAIN: {
                "test_1": {"initial": "test", "min": 3, "max": 10},
                "test_2": {},
            }
        },
    )
    entity_id = "input_text.test_1"
    entity_id_2 = "input_text.test_2"
    assert hass.states.get(entity_id).state == "test"
    assert hass.states.get(entity_id_2).state == "unknown"

    for entity in (entity_id, entity_id_2):
        await async_set_value(hass, entity, "testing")
        assert hass.states.get(entity).state == "testing"

    # Too long for entity 1
    await async_set_value(hass, entity, "testing too long")
    assert hass.states.get(entity_id).state == "testing"

    # Set to empty string
    await async_set_value(hass, entity_id_2, "")
    assert hass.states.get(entity_id_2).state == ""


async def test_mode(hass):
    """Test mode settings."""
    assert await async_setup_component(
        hass,
        DOMAIN,
        {
            DOMAIN: {
                "test_default_text": {"initial": "test", "min": 3, "max": 10},
                "test_explicit_text": {
{'fraction_non_alphanumeric': 0.10538211611809578, 'fraction_numerical': 0.00692188162169798, 'mean_word_length': 2.0140485312899106, 'pattern_counts': {'":': 49, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5818604', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2132, 'n_words': 509}