Move part of image_processing tests (#5634)

* Move part of image_processing tests

* fix lint
This commit is contained in:
Pascal Vizeli 2017-01-29 23:40:37 +01:00 committed by Paulus Schoutsen
parent 24f828d7eb
commit 261ffbbfea
3 changed files with 62 additions and 48 deletions

View file

@ -5,7 +5,6 @@ For more details about this component, please refer to the documentation at
https://home-assistant.io/components/demo/ https://home-assistant.io/components/demo/
""" """
from homeassistant.components.image_processing import ImageProcessingEntity
from homeassistant.components.image_processing.openalpr_local import ( from homeassistant.components.image_processing.openalpr_local import (
ImageProcessingAlprEntity) ImageProcessingAlprEntity)
from homeassistant.components.image_processing.microsoft_face_identify import ( from homeassistant.components.image_processing.microsoft_face_identify import (
@ -15,42 +14,12 @@ from homeassistant.components.image_processing.microsoft_face_identify import (
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the demo image_processing platform.""" """Setup the demo image_processing platform."""
add_devices([ add_devices([
DemoImageProcessing('camera.demo_camera', "Demo"),
DemoImageProcessingAlpr('camera.demo_camera', "Demo Alpr"), DemoImageProcessingAlpr('camera.demo_camera', "Demo Alpr"),
DemoImageProcessingFaceIdentify( DemoImageProcessingFaceIdentify(
'camera.demo_camera', "Demo Face Identify") 'camera.demo_camera', "Demo Face Identify")
]) ])
class DemoImageProcessing(ImageProcessingEntity):
"""Demo alpr image processing entity."""
def __init__(self, camera_entity, name):
"""Initialize demo alpr."""
self._name = name
self._camera = camera_entity
self._count = 0
@property
def camera_entity(self):
"""Return camera entity id from process pictures."""
return self._camera
@property
def name(self):
"""Return the name of the entity."""
return self._name
@property
def state(self):
"""Return the state of the entity."""
return self._count
def process_image(self, image):
"""Process image."""
self._count += 1
class DemoImageProcessingAlpr(ImageProcessingAlprEntity): class DemoImageProcessingAlpr(ImageProcessingAlprEntity):
"""Demo alpr image processing entity.""" """Demo alpr image processing entity."""

View file

@ -61,16 +61,13 @@ class TestImageProcessing(object):
config = { config = {
ip.DOMAIN: { ip.DOMAIN: {
'platform': 'demo' 'platform': 'test'
}, },
'camera': { 'camera': {
'platform': 'demo' 'platform': 'demo'
}, },
} }
with patch('homeassistant.components.image_processing.demo.'
'DemoImageProcessing.should_poll',
new_callable=PropertyMock(return_value=False)):
setup_component(self.hass, ip.DOMAIN, config) setup_component(self.hass, ip.DOMAIN, config)
state = self.hass.states.get('camera.demo_camera') state = self.hass.states.get('camera.demo_camera')
@ -84,33 +81,32 @@ class TestImageProcessing(object):
@patch('homeassistant.components.camera.demo.DemoCamera.camera_image', @patch('homeassistant.components.camera.demo.DemoCamera.camera_image',
autospec=True, return_value=b'Test') autospec=True, return_value=b'Test')
@patch('homeassistant.components.image_processing.demo.' def test_get_image_from_camera(self, mock_camera):
'DemoImageProcessing.process_image', autospec=True)
def test_get_image_from_camera(self, mock_process, mock_camera):
"""Grab a image from camera entity.""" """Grab a image from camera entity."""
self.hass.start() self.hass.start()
ip.scan(self.hass, entity_id='image_processing.demo') ip.scan(self.hass, entity_id='image_processing.test')
self.hass.block_till_done() self.hass.block_till_done()
assert mock_camera.called state = self.hass.states.get('image_processing.test')
assert mock_process.called
assert mock_process.call_args[0][1] == b'Test' assert mock_camera.called
assert state.state == '1'
assert state.attributes['image'] == b'Test'
@patch('homeassistant.components.camera.async_get_image', @patch('homeassistant.components.camera.async_get_image',
side_effect=HomeAssistantError()) side_effect=HomeAssistantError())
@patch('homeassistant.components.image_processing.demo.' def test_get_image_without_exists_camera(self, mock_image):
'DemoImageProcessing.process_image', autospec=True)
def test_get_image_without_exists_camera(self, mock_process, mock_image):
"""Try to get image without exists camera.""" """Try to get image without exists camera."""
self.hass.states.remove('camera.demo_camera') self.hass.states.remove('camera.demo_camera')
ip.scan(self.hass, entity_id='image_processing.demo') ip.scan(self.hass, entity_id='image_processing.test')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('image_processing.test')
assert mock_image.called assert mock_image.called
assert not mock_process.called assert state.state == '0'
class TestImageProcessingAlpr(object): class TestImageProcessingAlpr(object):

View file

@ -0,0 +1,49 @@
"""Provide a mock image processing."""
from homeassistant.components.image_processing import ImageProcessingEntity
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the test image_processing platform."""
add_devices([TestImageProcessing('camera.demo_camera', "Test")])
class TestImageProcessing(ImageProcessingEntity):
"""Test image processing entity."""
def __init__(self, camera_entity, name):
"""Initialize test image processing."""
self._name = name
self._camera = camera_entity
self._count = 0
self._image = ""
@property
def should_poll(self):
"""Return True if entity has to be polled for state."""
return False
@property
def camera_entity(self):
"""Return camera entity id from process pictures."""
return self._camera
@property
def name(self):
"""Return the name of the entity."""
return self._name
@property
def state(self):
"""Return the state of the entity."""
return self._count
@property
def device_state_attributes(self):
"""Return device specific state attributes."""
return {'image': self._image}
def process_image(self, image):
"""Process image."""
self._image = image
self._count += 1