core/tests/components/wallbox/test_lock.py

112 lines
3.1 KiB
Python

"""Test Wallbox Lock component."""
import json
import pytest
import requests_mock
from homeassistant.components.lock import SERVICE_LOCK, SERVICE_UNLOCK
from homeassistant.components.wallbox import CHARGER_LOCKED_UNLOCKED_KEY
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from tests.components.wallbox import (
authorisation_response,
entry,
setup_integration,
setup_integration_read_only,
)
from tests.components.wallbox.const import MOCK_LOCK_ENTITY_ID
async def test_wallbox_lock_class(hass: HomeAssistant) -> None:
"""Test wallbox lock class."""
await setup_integration(hass)
state = hass.states.get(MOCK_LOCK_ENTITY_ID)
assert state
assert state.state == "unlocked"
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
json=authorisation_response,
status_code=200,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_LOCKED_UNLOCKED_KEY: False})),
status_code=200,
)
await hass.services.async_call(
"lock",
SERVICE_LOCK,
{
ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID,
},
blocking=True,
)
await hass.services.async_call(
"lock",
SERVICE_UNLOCK,
{
ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID,
},
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)
async def test_wallbox_lock_class_connection_error(hass: HomeAssistant) -> None:
"""Test wallbox lock class connection error."""
await setup_integration(hass)
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
json=authorisation_response,
status_code=200,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_LOCKED_UNLOCKED_KEY: False})),
status_code=404,
)
with pytest.raises(ConnectionError):
await hass.services.async_call(
"lock",
SERVICE_LOCK,
{
ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID,
},
blocking=True,
)
with pytest.raises(ConnectionError):
await hass.services.async_call(
"lock",
SERVICE_UNLOCK,
{
ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID,
},
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)
async def test_wallbox_lock_class_authentication_error(hass: HomeAssistant) -> None:
"""Test wallbox lock not loaded on authentication error."""
await setup_integration_read_only(hass)
state = hass.states.get(MOCK_LOCK_ENTITY_ID)
assert state is None
await hass.config_entries.async_unload(entry.entry_id)