33
44import pytest
55
6+ import pysmaev .core
7+ import pysmaev .exceptions
8+
69from homeassistant import config_entries , data_entry_flow
710from homeassistant .const import CONF_HOST , CONF_BASE
811from homeassistant .core import HomeAssistant
912
1013from custom_components import smaev
1114from custom_components .smaev .config_flow import SmaEvChargerConfigFlow , validate_input
1215
13- from .conftest import CONFIG_DATA
16+ from .conftest import CONFIG_DATA , MockSmaEvCharger , DEVICE_INFO
1417
1518
1619async def test_show_form (hass : HomeAssistant ) -> None :
@@ -24,44 +27,41 @@ async def test_show_form(hass: HomeAssistant) -> None:
2427 assert result ["step_id" ] == "user"
2528
2629
27- async def test_step_user (hass , device_info ):
30+ @patch .object (pysmaev .core , "SmaEvCharger" , MockSmaEvCharger )
31+ async def test_step_user (hass ):
2832 """Test for user step."""
29- with (
30- patch ("pysmaev.core.SmaEvCharger.open" ),
31- patch ("pysmaev.core.SmaEvCharger.device_info" , return_value = device_info ),
32- ):
33- data = CONFIG_DATA .copy ()
34- result = await hass .config_entries .flow .async_init (
35- smaev .DOMAIN , context = {"source" : config_entries .SOURCE_USER }, data = data
36- )
33+ data = CONFIG_DATA .copy ()
34+ result = await hass .config_entries .flow .async_init (
35+ smaev .DOMAIN , context = {"source" : config_entries .SOURCE_USER }, data = data
36+ )
3737
38- assert result ["type" ] == data_entry_flow .FlowResultType .CREATE_ENTRY
39- assert result ["title" ] == CONFIG_DATA [CONF_HOST ]
40- assert result ["data" ] == CONFIG_DATA
38+ assert result ["type" ] == data_entry_flow .FlowResultType .CREATE_ENTRY
39+ assert result ["title" ] == CONFIG_DATA [CONF_HOST ]
40+ assert result ["data" ] == CONFIG_DATA
4141
4242
43- async def test_step_user_existing_host (hass , entry , device_info ):
43+ @patch .object (pysmaev .core , "SmaEvCharger" , MockSmaEvCharger )
44+ async def test_step_user_existing_host (hass , entry ):
4445 """Test for user defined host already exists."""
4546 entry .add_to_hass (hass )
4647
47- with (
48- patch ("pysmaev.core.SmaEvCharger.open" ),
49- patch ("pysmaev.core.SmaEvCharger.device_info" , return_value = device_info ),
50- ):
51- data = entry .data .copy ()
52- result = await hass .config_entries .flow .async_init (
53- smaev .DOMAIN , context = {"source" : config_entries .SOURCE_USER }, data = data
54- )
48+ data = entry .data .copy ()
49+ result = await hass .config_entries .flow .async_init (
50+ smaev .DOMAIN , context = {"source" : config_entries .SOURCE_USER }, data = data
51+ )
5552
56- assert result ["type" ] == data_entry_flow .FlowResultType .ABORT
57- assert result ["reason" ] == "already_configured"
53+ assert result ["type" ] == data_entry_flow .FlowResultType .ABORT
54+ assert result ["reason" ] == "already_configured"
5855
5956
6057@pytest .mark .parametrize (
6158 ("error" , "errors" ),
6259 [
63- (smaev .SmaEvChargerConnectionError , {CONF_BASE : "cannot_connect" }),
64- (smaev .SmaEvChargerAuthenticationError , {CONF_BASE : "invalid_auth" }),
60+ (pysmaev .exceptions .SmaEvChargerConnectionError , {CONF_BASE : "cannot_connect" }),
61+ (
62+ pysmaev .exceptions .SmaEvChargerAuthenticationError ,
63+ {CONF_BASE : "invalid_auth" },
64+ ),
6565 (Exception , {CONF_BASE : "unknown" }),
6666 ],
6767)
@@ -77,7 +77,8 @@ async def test_step_user_error(hass, error, errors):
7777 assert result ["errors" ] == errors
7878
7979
80- async def test_options_flow (hass , entry , device_info ):
80+ @patch .object (pysmaev .core , "SmaEvCharger" , MockSmaEvCharger )
81+ async def test_options_flow (hass , entry ):
8182 """Test config flow options."""
8283 entry .add_to_hass (hass )
8384 result = await hass .config_entries .options .async_init (entry .entry_id )
@@ -87,13 +88,9 @@ async def test_options_flow(hass, entry, device_info):
8788
8889 user_input = CONFIG_DATA .copy ()
8990
90- with (
91- patch ("pysmaev.core.SmaEvCharger.open" ),
92- patch ("pysmaev.core.SmaEvCharger.device_info" , return_value = device_info ),
93- ):
94- result = await hass .config_entries .options .async_configure (
95- result ["flow_id" ], user_input = user_input
96- )
91+ result = await hass .config_entries .options .async_configure (
92+ result ["flow_id" ], user_input = user_input
93+ )
9794
9895 assert result ["type" ] == data_entry_flow .FlowResultType .CREATE_ENTRY
9996 assert all (value == entry .data [key ] for key , value in user_input .items ())
@@ -102,8 +99,11 @@ async def test_options_flow(hass, entry, device_info):
10299@pytest .mark .parametrize (
103100 ("error" , "errors" ),
104101 [
105- (smaev .SmaEvChargerConnectionError , {CONF_BASE : "cannot_connect" }),
106- (smaev .SmaEvChargerAuthenticationError , {CONF_BASE : "invalid_auth" }),
102+ (pysmaev .exceptions .SmaEvChargerConnectionError , {CONF_BASE : "cannot_connect" }),
103+ (
104+ pysmaev .exceptions .SmaEvChargerAuthenticationError ,
105+ {CONF_BASE : "invalid_auth" },
106+ ),
107107 (Exception , {CONF_BASE : "unknown" }),
108108 ],
109109)
@@ -126,28 +126,26 @@ async def test_options_flow_errors(hass, entry, error, errors):
126126 assert result ["errors" ] == errors
127127
128128
129- async def test_validate_connection (hass : HomeAssistant , device_info ):
129+ async def test_validate_connection (hass : HomeAssistant ):
130130 """Test the connection validation."""
131131 data = CONFIG_DATA .copy ()
132132
133- with (
134- patch ("pysmaev.core.SmaEvCharger.open" ) as mock_open ,
135- patch (
136- "pysmaev.core.SmaEvCharger.device_info" , return_value = device_info
137- ) as mock_device_info ,
138- ):
133+ with patch ("pysmaev.core.SmaEvCharger" , MockSmaEvCharger ) as mock :
139134 result = await validate_input (hass , data = data )
140135
141- assert mock_open .is_called
142- assert mock_device_info .is_called
143- assert result == (device_info , {})
136+ assert mock . open .is_called
137+ assert mock . device_info .is_called
138+ assert result == (DEVICE_INFO , {})
144139
145140
146141@pytest .mark .parametrize (
147142 ("error" , "errors" ),
148143 [
149- (smaev .SmaEvChargerConnectionError , {CONF_BASE : "cannot_connect" }),
150- (smaev .SmaEvChargerAuthenticationError , {CONF_BASE : "invalid_auth" }),
144+ (pysmaev .exceptions .SmaEvChargerConnectionError , {CONF_BASE : "cannot_connect" }),
145+ (
146+ pysmaev .exceptions .SmaEvChargerAuthenticationError ,
147+ {CONF_BASE : "invalid_auth" },
148+ ),
151149 (Exception , {CONF_BASE : "unknown" }),
152150 ],
153151)
0 commit comments