fix camera.push API overwrite (#15334)
* fix camera.push API overwrite * dont search in the component dictionary, but in hour own * remove error message * hound
This commit is contained in:
parent
1ff329d9d6
commit
ec3d2e97e8
1 changed files with 17 additions and 9 deletions
|
@ -29,6 +29,8 @@ DEFAULT_NAME = "Push Camera"
|
|||
ATTR_FILENAME = 'filename'
|
||||
ATTR_LAST_TRIP = 'last_trip'
|
||||
|
||||
PUSH_CAMERA_DATA = 'push_camera'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_BUFFER_SIZE, default=1): cv.positive_int,
|
||||
|
@ -41,11 +43,14 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||
async def async_setup_platform(hass, config, async_add_devices,
|
||||
discovery_info=None):
|
||||
"""Set up the Push Camera platform."""
|
||||
if PUSH_CAMERA_DATA not in hass.data:
|
||||
hass.data[PUSH_CAMERA_DATA] = {}
|
||||
|
||||
cameras = [PushCamera(config[CONF_NAME],
|
||||
config[CONF_BUFFER_SIZE],
|
||||
config[CONF_TIMEOUT])]
|
||||
|
||||
hass.http.register_view(CameraPushReceiver(cameras,
|
||||
hass.http.register_view(CameraPushReceiver(hass,
|
||||
config[CONF_IMAGE_FIELD]))
|
||||
|
||||
async_add_devices(cameras)
|
||||
|
@ -57,19 +62,18 @@ class CameraPushReceiver(HomeAssistantView):
|
|||
url = "/api/camera_push/{entity_id}"
|
||||
name = 'api:camera_push:camera_entity'
|
||||
|
||||
def __init__(self, cameras, image_field):
|
||||
def __init__(self, hass, image_field):
|
||||
"""Initialize CameraPushReceiver with camera entity."""
|
||||
self._cameras = cameras
|
||||
self._cameras = hass.data[PUSH_CAMERA_DATA]
|
||||
self._image = image_field
|
||||
|
||||
async def post(self, request, entity_id):
|
||||
"""Accept the POST from Camera."""
|
||||
try:
|
||||
(_camera,) = [camera for camera in self._cameras
|
||||
if camera.entity_id == entity_id]
|
||||
except ValueError:
|
||||
_LOGGER.error("Unknown push camera %s", entity_id)
|
||||
return self.json_message('Unknown Push Camera',
|
||||
_camera = self._cameras.get(entity_id)
|
||||
|
||||
if _camera is None:
|
||||
_LOGGER.error("Unknown %s", entity_id)
|
||||
return self.json_message('Unknown {}'.format(entity_id),
|
||||
HTTP_BAD_REQUEST)
|
||||
|
||||
try:
|
||||
|
@ -101,6 +105,10 @@ class PushCamera(Camera):
|
|||
self.queue = deque([], buffer_size)
|
||||
self._current_image = None
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Call when entity is added to hass."""
|
||||
self.hass.data[PUSH_CAMERA_DATA][self.entity_id] = self
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Current state of the camera."""
|
||||
|
|
Loading…
Add table
Reference in a new issue