@@ -73,66 +73,8 @@ def mock_image():
7373
7474async def test_setup_platform (hass ):
7575 """Setup platform with one entity."""
76- with requests_mock .Mocker () as mock_req :
77- url = 'http://{}:{}/{}/models' .format (
78- MOCK_IP ,
79- MOCK_PORT ,
80- cb .CLASSIFIER )
81- mock_req .post (url , json = MOCK_MODELS )
76+ with patch ('homeassistant.components.image_processing.classificationbox.get_models' ,
77+ return_value = MOCK_MODELS ):
8278 await async_setup_component (hass , ip .DOMAIN , VALID_CONFIG )
83- assert hass .states .get (VALID_ENTITY_ID )
84-
85-
86- async def test_process_image (hass , mock_image ):
87- """Test processing of an image."""
88- await async_setup_component (hass , ip .DOMAIN , VALID_CONFIG )
89- assert hass .states .get (VALID_ENTITY_ID )
90-
91- classification_events = []
92-
93- @callback
94- def mock_classification_event (event ):
95- """Mock event."""
96- classification_events .append (event )
97-
98- hass .bus .async_listen ('image_processing.image_classification' ,
99- mock_classification_event )
100-
101- with requests_mock .Mocker () as mock_req :
102- url = 'http://{}:{}/{}/models/{}/predict' .format (
103- MOCK_IP ,
104- MOCK_PORT ,
105- cb .CLASSIFIER ,
106- MOCK_MODEL_ID )
107- mock_req .post (url , json = MOCK_JSON )
108- data = {ATTR_ENTITY_ID : VALID_ENTITY_ID }
109- await hass .services .async_call (ip .DOMAIN ,
110- ip .SERVICE_SCAN ,
111- service_data = data )
11279 await hass .async_block_till_done ()
113-
114- state = hass .states .get (VALID_ENTITY_ID )
115- assert state .state == 'bird'
116- assert state .attributes .get (ip .ATTR_CONFIDENCE ) == ip .DEFAULT_CONFIDENCE
117-
118- assert state .attributes .get (cb .ATTR_MODEL_ID ) == MOCK_MODEL_ID
119- assert (state .attributes .get (CONF_FRIENDLY_NAME ) ==
120- 'classificationbox demo_camera 12345' )
121-
122- assert len (classification_events ) == 1
123- assert classification_events [0 ].data [ATTR_ID ] == 'birds'
124- assert classification_events [0 ].data [ip .ATTR_CONFIDENCE ] == 91.59
125-
126-
127- async def test_setup_platform_with_name (hass ):
128- """Setup platform with one entity and a name."""
129- MOCK_NAME = 'mock_name'
130- NAMED_ENTITY_ID = 'image_processing.{}' .format (MOCK_NAME )
131-
132- VALID_CONFIG_NAMED = VALID_CONFIG .copy ()
133- VALID_CONFIG_NAMED [ip .DOMAIN ][ip .CONF_SOURCE ][ip .CONF_NAME ] = MOCK_NAME
134-
135- await async_setup_component (hass , ip .DOMAIN , VALID_CONFIG_NAMED )
136- assert hass .states .get (NAMED_ENTITY_ID )
137- state = hass .states .get (NAMED_ENTITY_ID )
138- assert state .attributes .get (CONF_FRIENDLY_NAME ) == MOCK_NAME
80+ assert hass .states .get (VALID_ENTITY_ID )
0 commit comments