|
| 1 | +"""Test Wallbox Select component.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests_mock |
| 5 | + |
| 6 | +from homeassistant.components.select import ( |
| 7 | + ATTR_OPTION, |
| 8 | + DOMAIN as SELECT_DOMAIN, |
| 9 | + SERVICE_SELECT_OPTION, |
| 10 | +) |
| 11 | +from homeassistant.components.wallbox.const import CHARGER_STATUS_ID_KEY, EcoSmartMode |
| 12 | +from homeassistant.const import ATTR_ENTITY_ID |
| 13 | +from homeassistant.core import HomeAssistant |
| 14 | +from homeassistant.exceptions import ConfigEntryAuthFailed |
| 15 | + |
| 16 | +from . import authorisation_response, setup_integration |
| 17 | +from .const import MOCK_SELECT_ENTITY_ID |
| 18 | + |
| 19 | +from tests.common import MockConfigEntry |
| 20 | + |
| 21 | + |
| 22 | +async def test_wallbox_select_class( |
| 23 | + hass: HomeAssistant, entry: MockConfigEntry |
| 24 | +) -> None: |
| 25 | + """Test wallbox select class.""" |
| 26 | + |
| 27 | + await setup_integration(hass, entry) |
| 28 | + |
| 29 | + state = hass.states.get(MOCK_SELECT_ENTITY_ID) |
| 30 | + assert state |
| 31 | + assert state.state == EcoSmartMode.OFF |
| 32 | + |
| 33 | + with requests_mock.Mocker() as mock_request: |
| 34 | + mock_request.get( |
| 35 | + "https://user-api.wall-box.com/users/signin", |
| 36 | + json=authorisation_response, |
| 37 | + status_code=200, |
| 38 | + ) |
| 39 | + mock_request.put( |
| 40 | + "https://api.wall-box.com/v4/chargers/12345/eco-smart", |
| 41 | + json={CHARGER_STATUS_ID_KEY: 193}, |
| 42 | + status_code=200, |
| 43 | + ) |
| 44 | + |
| 45 | + await hass.services.async_call( |
| 46 | + SELECT_DOMAIN, |
| 47 | + SERVICE_SELECT_OPTION, |
| 48 | + { |
| 49 | + ATTR_ENTITY_ID: MOCK_SELECT_ENTITY_ID, |
| 50 | + ATTR_OPTION: EcoSmartMode.ECO_MODE, |
| 51 | + }, |
| 52 | + blocking=True, |
| 53 | + ) |
| 54 | + |
| 55 | + await hass.services.async_call( |
| 56 | + SELECT_DOMAIN, |
| 57 | + SERVICE_SELECT_OPTION, |
| 58 | + { |
| 59 | + ATTR_ENTITY_ID: MOCK_SELECT_ENTITY_ID, |
| 60 | + ATTR_OPTION: EcoSmartMode.FULL_SOLAR, |
| 61 | + }, |
| 62 | + blocking=True, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +async def test_wallbox_select_class_connection_error( |
| 67 | + hass: HomeAssistant, entry: MockConfigEntry |
| 68 | +) -> None: |
| 69 | + """Test wallbox select class connection error.""" |
| 70 | + |
| 71 | + await setup_integration(hass, entry) |
| 72 | + |
| 73 | + with requests_mock.Mocker() as mock_request: |
| 74 | + mock_request.get( |
| 75 | + "https://user-api.wall-box.com/users/signin", |
| 76 | + json=authorisation_response, |
| 77 | + status_code=200, |
| 78 | + ) |
| 79 | + mock_request.put( |
| 80 | + "https://api.wall-box.com/v4/chargers/12345/eco-smart", |
| 81 | + json={CHARGER_STATUS_ID_KEY: 193}, |
| 82 | + status_code=404, |
| 83 | + ) |
| 84 | + |
| 85 | + with pytest.raises(ConnectionError): |
| 86 | + await hass.services.async_call( |
| 87 | + SELECT_DOMAIN, |
| 88 | + SERVICE_SELECT_OPTION, |
| 89 | + { |
| 90 | + ATTR_ENTITY_ID: MOCK_SELECT_ENTITY_ID, |
| 91 | + ATTR_OPTION: EcoSmartMode.ECO_MODE, |
| 92 | + }, |
| 93 | + blocking=True, |
| 94 | + ) |
| 95 | + |
| 96 | + with pytest.raises(ConnectionError): |
| 97 | + await hass.services.async_call( |
| 98 | + SELECT_DOMAIN, |
| 99 | + SERVICE_SELECT_OPTION, |
| 100 | + { |
| 101 | + ATTR_ENTITY_ID: MOCK_SELECT_ENTITY_ID, |
| 102 | + ATTR_OPTION: EcoSmartMode.FULL_SOLAR, |
| 103 | + }, |
| 104 | + blocking=True, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +async def test_wallbox_select_class_authentication_error( |
| 109 | + hass: HomeAssistant, entry: MockConfigEntry |
| 110 | +) -> None: |
| 111 | + """Test wallbox select class authentication error.""" |
| 112 | + |
| 113 | + await setup_integration(hass, entry) |
| 114 | + |
| 115 | + with requests_mock.Mocker() as mock_request: |
| 116 | + mock_request.get( |
| 117 | + "https://user-api.wall-box.com/users/signin", |
| 118 | + json=authorisation_response, |
| 119 | + status_code=200, |
| 120 | + ) |
| 121 | + mock_request.put( |
| 122 | + "https://api.wall-box.com/v4/chargers/12345/eco-smart", |
| 123 | + json={CHARGER_STATUS_ID_KEY: 193}, |
| 124 | + status_code=403, |
| 125 | + ) |
| 126 | + |
| 127 | + with pytest.raises(ConfigEntryAuthFailed): |
| 128 | + await hass.services.async_call( |
| 129 | + SELECT_DOMAIN, |
| 130 | + SERVICE_SELECT_OPTION, |
| 131 | + { |
| 132 | + ATTR_ENTITY_ID: MOCK_SELECT_ENTITY_ID, |
| 133 | + ATTR_OPTION: EcoSmartMode.ECO_MODE, |
| 134 | + }, |
| 135 | + blocking=True, |
| 136 | + ) |
| 137 | + |
| 138 | + with pytest.raises(ConfigEntryAuthFailed): |
| 139 | + await hass.services.async_call( |
| 140 | + SELECT_DOMAIN, |
| 141 | + SERVICE_SELECT_OPTION, |
| 142 | + { |
| 143 | + ATTR_ENTITY_ID: MOCK_SELECT_ENTITY_ID, |
| 144 | + ATTR_OPTION: EcoSmartMode.FULL_SOLAR, |
| 145 | + }, |
| 146 | + blocking=True, |
| 147 | + ) |
0 commit comments