33from ipaddress import ip_address
44from unittest .mock import AsyncMock
55
6- from iometer import IOmeterConnectionError
6+ from iometer import IOmeterConnectionError , IOmeterNoReadingsError , IOmeterNoStatusError
7+ import pytest
78
89from homeassistant .components import zeroconf
910from homeassistant .components .iometer .const import DOMAIN
@@ -93,29 +94,54 @@ async def test_zeroconf_flow_abort_duplicate(
9394 assert result ["reason" ] == "already_configured"
9495
9596
96- async def test_zeroconf_flow_connection_error (
97+ @pytest .mark .parametrize (
98+ ("method_name" , "exception" , "reason" ),
99+ [
100+ ("get_current_status" , IOmeterConnectionError (), "cannot_connect" ),
101+ ("get_current_status" , IOmeterNoStatusError (), "no_status" ),
102+ ("get_current_reading" , IOmeterNoReadingsError (), "no_readings" ),
103+ ],
104+ ids = ["status-connection" , "status-missing" , "reading-missing" ],
105+ )
106+ async def test_zeroconf_flow_abort_errors (
97107 hass : HomeAssistant ,
98108 mock_iometer_client : AsyncMock ,
109+ method_name : str ,
110+ exception : Exception ,
111+ reason : str ,
99112) -> None :
100- """Test zeroconf flow."""
101- mock_iometer_client .get_current_status .side_effect = IOmeterConnectionError ()
113+ """Test zeroconf flow aborts when the client raises an exception."""
114+ getattr (mock_iometer_client , method_name ).side_effect = exception
115+
102116 result = await hass .config_entries .flow .async_init (
103117 DOMAIN ,
104118 context = {"source" : SOURCE_ZEROCONF },
105119 data = ZEROCONF_DISCOVERY ,
106120 )
107121 await hass .async_block_till_done ()
108122 assert result ["type" ] is FlowResultType .ABORT
109- assert result ["reason" ] == "cannot_connect"
123+ assert result ["reason" ] == reason
110124
111125
112- async def test_user_flow_connection_error (
126+ @pytest .mark .parametrize (
127+ ("method_name" , "exception" , "error_key" ),
128+ [
129+ ("get_current_status" , IOmeterConnectionError (), "cannot_connect" ),
130+ ("get_current_status" , IOmeterNoStatusError (), "no_status" ),
131+ ("get_current_reading" , IOmeterNoReadingsError (), "no_readings" ),
132+ ],
133+ ids = ["status-connection" , "status-missing" , "reading-missing" ],
134+ )
135+ async def test_user_flow_errors (
113136 hass : HomeAssistant ,
114137 mock_iometer_client : AsyncMock ,
115138 mock_setup_entry : AsyncMock ,
139+ method_name : str ,
140+ exception : Exception ,
141+ error_key : str ,
116142) -> None :
117- """Test flow error ."""
118- mock_iometer_client . get_current_status . side_effect = IOmeterConnectionError ()
143+ """Test user flow returns errors for client exceptions ."""
144+ getattr ( mock_iometer_client , method_name ). side_effect = exception
119145
120146 result = await hass .config_entries .flow .async_init (
121147 DOMAIN ,
@@ -130,11 +156,10 @@ async def test_user_flow_connection_error(
130156 {CONF_HOST : IP_ADDRESS },
131157 )
132158 await hass .async_block_till_done ()
133-
134159 assert result ["type" ] is FlowResultType .FORM
135- assert result ["errors" ] == {"base" : "cannot_connect" }
160+ assert result ["errors" ] == {"base" : error_key }
136161
137- mock_iometer_client . get_current_status .side_effect = None
162+ getattr ( mock_iometer_client , method_name ) .side_effect = None
138163
139164 result = await hass .config_entries .flow .async_configure (
140165 result ["flow_id" ],
0 commit comments