mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Use collection helpers for counter integration (#32295)
* Refactor counter to use config dict. * Use collection helpers for counter integration. * Update tests. * Use callbacks were applicable.
This commit is contained in:
@@ -2,8 +2,13 @@
|
||||
# pylint: disable=protected-access
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.counter import (
|
||||
ATTR_EDITABLE,
|
||||
ATTR_INITIAL,
|
||||
ATTR_MAXIMUM,
|
||||
ATTR_MINIMUM,
|
||||
ATTR_STEP,
|
||||
CONF_ICON,
|
||||
CONF_INITIAL,
|
||||
@@ -14,8 +19,9 @@ from homeassistant.components.counter import (
|
||||
DEFAULT_STEP,
|
||||
DOMAIN,
|
||||
)
|
||||
from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_ICON
|
||||
from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_ICON, ATTR_NAME
|
||||
from homeassistant.core import Context, CoreState, State
|
||||
from homeassistant.helpers import entity_registry
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import mock_restore_cache
|
||||
@@ -28,6 +34,42 @@ from tests.components.counter.common import (
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@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",
|
||||
"initial": 10,
|
||||
"name": "from storage",
|
||||
"maximum": 100,
|
||||
"minimum": 3,
|
||||
"step": 2,
|
||||
"restore": False,
|
||||
}
|
||||
]
|
||||
},
|
||||
}
|
||||
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 test_config(hass):
|
||||
"""Test config."""
|
||||
invalid_configs = [None, 1, {}, {"name with space": None}]
|
||||
@@ -452,3 +494,209 @@ async def test_configure(hass, hass_admin_user):
|
||||
assert 0 == state.attributes.get("minimum")
|
||||
assert 9 == state.attributes.get("maximum")
|
||||
assert 6 == state.attributes.get("initial")
|
||||
|
||||
|
||||
async def test_load_from_storage(hass, storage_setup):
|
||||
"""Test set up from storage."""
|
||||
assert await storage_setup()
|
||||
state = hass.states.get(f"{DOMAIN}.from_storage")
|
||||
assert int(state.state) == 10
|
||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "from storage"
|
||||
assert state.attributes.get(ATTR_EDITABLE)
|
||||
|
||||
|
||||
async def test_editable_state_attribute(hass, storage_setup):
|
||||
"""Test editable attribute."""
|
||||
assert await storage_setup(
|
||||
config={
|
||||
DOMAIN: {
|
||||
"from_yaml": {
|
||||
"minimum": 1,
|
||||
"maximum": 10,
|
||||
"initial": 5,
|
||||
"step": 1,
|
||||
"restore": False,
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.from_storage")
|
||||
assert int(state.state) == 10
|
||||
assert state.attributes[ATTR_FRIENDLY_NAME] == "from storage"
|
||||
assert state.attributes[ATTR_EDITABLE] is True
|
||||
|
||||
state = hass.states.get(f"{DOMAIN}.from_yaml")
|
||||
assert int(state.state) == 5
|
||||
assert state.attributes[ATTR_EDITABLE] is False
|
||||
|
||||
|
||||
async def test_ws_list(hass, hass_ws_client, storage_setup):
|
||||
"""Test listing via WS."""
|
||||
assert await storage_setup(
|
||||
config={
|
||||
DOMAIN: {
|
||||
"from_yaml": {
|
||||
"minimum": 1,
|
||||
"maximum": 10,
|
||||
"initial": 5,
|
||||
"step": 1,
|
||||
"restore": False,
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json({"id": 6, "type": f"{DOMAIN}/list"})
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
|
||||
storage_ent = "from_storage"
|
||||
yaml_ent = "from_yaml"
|
||||
result = {item["id"]: item for item in resp["result"]}
|
||||
|
||||
assert len(result) == 1
|
||||
assert storage_ent in result
|
||||
assert yaml_ent not in result
|
||||
assert result[storage_ent][ATTR_NAME] == "from storage"
|
||||
|
||||
|
||||
async def test_ws_delete(hass, hass_ws_client, storage_setup):
|
||||
"""Test WS delete cleans up entity registry."""
|
||||
assert await storage_setup()
|
||||
|
||||
input_id = "from_storage"
|
||||
input_entity_id = f"{DOMAIN}.{input_id}"
|
||||
ent_reg = await entity_registry.async_get_registry(hass)
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state is not None
|
||||
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json(
|
||||
{"id": 6, "type": f"{DOMAIN}/delete", f"{DOMAIN}_id": f"{input_id}"}
|
||||
)
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state is None
|
||||
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is None
|
||||
|
||||
|
||||
async def test_update_min_max(hass, hass_ws_client, storage_setup):
|
||||
"""Test updating min/max updates the state."""
|
||||
|
||||
items = [
|
||||
{
|
||||
"id": "from_storage",
|
||||
"initial": 15,
|
||||
"name": "from storage",
|
||||
"maximum": 100,
|
||||
"minimum": 10,
|
||||
"step": 3,
|
||||
"restore": True,
|
||||
}
|
||||
]
|
||||
assert await storage_setup(items)
|
||||
|
||||
input_id = "from_storage"
|
||||
input_entity_id = f"{DOMAIN}.{input_id}"
|
||||
ent_reg = await entity_registry.async_get_registry(hass)
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state is not None
|
||||
assert int(state.state) == 15
|
||||
assert state.attributes[ATTR_MAXIMUM] == 100
|
||||
assert state.attributes[ATTR_MINIMUM] == 10
|
||||
assert state.attributes[ATTR_STEP] == 3
|
||||
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, input_id) is not None
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 6,
|
||||
"type": f"{DOMAIN}/update",
|
||||
f"{DOMAIN}_id": f"{input_id}",
|
||||
"minimum": 19,
|
||||
}
|
||||
)
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert int(state.state) == 19
|
||||
assert state.attributes[ATTR_MINIMUM] == 19
|
||||
assert state.attributes[ATTR_MAXIMUM] == 100
|
||||
assert state.attributes[ATTR_STEP] == 3
|
||||
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 7,
|
||||
"type": f"{DOMAIN}/update",
|
||||
f"{DOMAIN}_id": f"{input_id}",
|
||||
"maximum": 5,
|
||||
"minimum": 2,
|
||||
"step": 5,
|
||||
}
|
||||
)
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert int(state.state) == 5
|
||||
assert state.attributes[ATTR_MINIMUM] == 2
|
||||
assert state.attributes[ATTR_MAXIMUM] == 5
|
||||
assert state.attributes[ATTR_STEP] == 5
|
||||
|
||||
await client.send_json(
|
||||
{
|
||||
"id": 8,
|
||||
"type": f"{DOMAIN}/update",
|
||||
f"{DOMAIN}_id": f"{input_id}",
|
||||
"maximum": None,
|
||||
"minimum": None,
|
||||
"step": 6,
|
||||
}
|
||||
)
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert int(state.state) == 5
|
||||
assert ATTR_MINIMUM not in state.attributes
|
||||
assert ATTR_MAXIMUM not in state.attributes
|
||||
assert state.attributes[ATTR_STEP] == 6
|
||||
|
||||
|
||||
async def test_create(hass, hass_ws_client, storage_setup):
|
||||
"""Test creating counter using WS."""
|
||||
|
||||
items = []
|
||||
|
||||
assert await storage_setup(items)
|
||||
|
||||
counter_id = "new_counter"
|
||||
input_entity_id = f"{DOMAIN}.{counter_id}"
|
||||
ent_reg = await entity_registry.async_get_registry(hass)
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert state is None
|
||||
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, counter_id) is None
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json({"id": 6, "type": f"{DOMAIN}/create", "name": "new counter"})
|
||||
resp = await client.receive_json()
|
||||
assert resp["success"]
|
||||
|
||||
state = hass.states.get(input_entity_id)
|
||||
assert int(state.state) == 0
|
||||
assert ATTR_MINIMUM not in state.attributes
|
||||
assert ATTR_MAXIMUM not in state.attributes
|
||||
assert state.attributes[ATTR_STEP] == 1
|
||||
|
||||
Reference in New Issue
Block a user