Skip to content

Commit

Permalink
Add: Ability to add extra arguments for ffmpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
JurajNyiri committed Jul 3, 2021
1 parent b3340fb commit 4542548
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
10 changes: 10 additions & 0 deletions custom_components/tapo_control/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from homeassistant.components.ffmpeg import CONF_EXTRA_ARGUMENTS
from homeassistant.const import (
CONF_IP_ADDRESS,
CONF_USERNAME,
Expand Down Expand Up @@ -91,6 +92,15 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):

config_entry.version = 6

if config_entry.version == 6:

new = {**config_entry.data}
new[CONF_EXTRA_ARGUMENTS] = ""

config_entry.data = {**new}

config_entry.version = 7

LOGGER.info("Migration to version %s successful", config_entry.version)

return True
Expand Down
13 changes: 10 additions & 3 deletions custom_components/tapo_control/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
SUPPORT_STREAM,
Camera,
)
from homeassistant.components.ffmpeg import DATA_FFMPEG
from homeassistant.components.ffmpeg import CONF_EXTRA_ARGUMENTS, DATA_FFMPEG
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
from haffmpeg.camera import CameraMjpeg
Expand Down Expand Up @@ -123,6 +123,7 @@ def __init__(
self._hass = hass
self._enabled = False
self._hdstream = HDStream
self._extra_arguments = entry.data.get(CONF_EXTRA_ARGUMENTS)
self._host = entry.data.get(CONF_IP_ADDRESS)
self._username = entry.data.get(CONF_USERNAME)
self._password = entry.data.get(CONF_PASSWORD)
Expand Down Expand Up @@ -226,14 +227,20 @@ async def async_camera_image(self):
ffmpeg = ImageFrame(self._ffmpeg.binary)
streaming_url = self.getStreamSource()
image = await asyncio.shield(
ffmpeg.get_image(streaming_url, output_format=IMAGE_JPEG,)
ffmpeg.get_image(
streaming_url,
output_format=IMAGE_JPEG,
extra_cmd=self._extra_arguments,
)
)
return image

async def handle_async_mjpeg_stream(self, request):
streaming_url = self.getStreamSource()
stream = CameraMjpeg(self._ffmpeg.binary)
await stream.open_camera(streaming_url)
await stream.open_camera(
streaming_url, extra_cmd=self._extra_arguments,
)
try:
stream_reader = await stream.get_reader()
return await async_aiohttp_proxy_stream(
Expand Down
24 changes: 23 additions & 1 deletion custom_components/tapo_control/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from homeassistant import config_entries
from homeassistant.components.ffmpeg import CONF_EXTRA_ARGUMENTS
from homeassistant.const import CONF_IP_ADDRESS, CONF_USERNAME, CONF_PASSWORD
from homeassistant.core import callback
from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS, MAC_ADDRESS
Expand All @@ -22,7 +23,7 @@
class FlowHandler(config_entries.ConfigFlow):
"""Handle a config flow."""

VERSION = 6
VERSION = 7

@staticmethod
def async_get_options_flow(config_entry):
Expand Down Expand Up @@ -73,6 +74,7 @@ async def async_step_other_options(self, user_input=None):
sound_detection_peak = -50
sound_detection_duration = 1
sound_detection_reset = 10
extra_arguments = ""
if user_input is not None:
if ENABLE_MOTION_SENSOR in user_input:
enable_motion_sensor = user_input[ENABLE_MOTION_SENSOR]
Expand Down Expand Up @@ -102,6 +104,10 @@ async def async_step_other_options(self, user_input=None):
sound_detection_reset = user_input[SOUND_DETECTION_RESET]
else:
sound_detection_reset = -50
if CONF_EXTRA_ARGUMENTS in user_input:
extra_arguments = user_input[CONF_EXTRA_ARGUMENTS]
else:
extra_arguments = ""
host = self.tapoHost
cloud_password = self.tapoCloudPassword
username = self.tapoUsername
Expand All @@ -120,6 +126,7 @@ async def async_step_other_options(self, user_input=None):
SOUND_DETECTION_PEAK: sound_detection_peak,
SOUND_DETECTION_DURATION: sound_detection_duration,
SOUND_DETECTION_RESET: sound_detection_reset,
CONF_EXTRA_ARGUMENTS: extra_arguments,
},
)

Expand Down Expand Up @@ -154,6 +161,10 @@ async def async_step_other_options(self, user_input=None):
SOUND_DETECTION_RESET,
description={"suggested_value": sound_detection_reset},
): int,
vol.Optional(
CONF_EXTRA_ARGUMENTS,
description={"suggested_value": extra_arguments},
): str,
}
),
errors=errors,
Expand Down Expand Up @@ -328,6 +339,7 @@ async def async_step_auth(self, user_input=None):
sound_detection_peak = self.config_entry.data[SOUND_DETECTION_PEAK]
sound_detection_duration = self.config_entry.data[SOUND_DETECTION_DURATION]
sound_detection_reset = self.config_entry.data[SOUND_DETECTION_RESET]
extra_arguments = self.config_entry.data[CONF_EXTRA_ARGUMENTS]
if user_input is not None:
try:
host = self.config_entry.data[CONF_IP_ADDRESS]
Expand Down Expand Up @@ -381,6 +393,11 @@ async def async_step_auth(self, user_input=None):
else:
sound_detection_reset = 10

if CONF_EXTRA_ARGUMENTS in user_input:
extra_arguments = user_input[CONF_EXTRA_ARGUMENTS]
else:
extra_arguments = ""

if not (
int(sound_detection_peak) >= -100 and int(sound_detection_peak) <= 0
):
Expand Down Expand Up @@ -416,6 +433,7 @@ async def async_step_auth(self, user_input=None):
SOUND_DETECTION_PEAK: sound_detection_peak,
SOUND_DETECTION_DURATION: sound_detection_duration,
SOUND_DETECTION_RESET: sound_detection_reset,
CONF_EXTRA_ARGUMENTS: extra_arguments,
},
)
return self.async_create_entry(title="", data=None)
Expand Down Expand Up @@ -474,6 +492,10 @@ async def async_step_auth(self, user_input=None):
SOUND_DETECTION_RESET,
description={"suggested_value": sound_detection_reset},
): int,
vol.Optional(
CONF_EXTRA_ARGUMENTS,
description={"suggested_value": extra_arguments},
): str,
}
),
errors=errors,
Expand Down
10 changes: 6 additions & 4 deletions custom_components/tapo_control/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
"enable_motion_sensor": "Enable motion sensor",
"enable_time_sync": "Automatically synchronise time",
"enable_stream": "Use Stream from Home Assistant",
"enable_sound_detection": "Enable sound threshold detection",
"enable_sound_detection": "[Requires restart] Enable sound threshold detection",
"sound_detection_peak": "[Sound Detection] Peak in dB. 0 is very loud and -100 is low.",
"sound_detection_duration": "[Sound Detection] How long the noise needs to be over the peak to trigger the state.",
"sound_detection_reset": "[Sound Detection] The time to reset the state after no new noise is over the peak."
"sound_detection_reset": "[Sound Detection] The time to reset the state after no new noise is over the peak.",
"extra_arguments": "[Requires restart] Extra arguments for ffmpeg"
},
"description": "Almost there!\nJust some final options..."
}
Expand Down Expand Up @@ -61,11 +62,12 @@
"enable_motion_sensor": "Enable motion sensor",
"enable_stream": "Use Stream from Home Assistant [requires restart]",
"enable_time_sync": "Automatically synchronise time",
"enable_sound_detection": "Enable sound threshold detection",
"enable_sound_detection": "[Requires restart] Enable sound threshold detection",
"sound_detection_peak": "[Sound Detection] Peak in dB. 0 is very loud and -100 is low.",
"sound_detection_duration": "[Sound Detection] How long the noise needs to be over the peak to trigger the state.",
"sound_detection_reset": "[Sound Detection] The time to reset the state after no new noise is over the peak.",
"cloud_password": "Cloud Password (Optional)"
"cloud_password": "Cloud Password (Optional)",
"extra_arguments": "[Requires restart] Extra arguments for ffmpeg"
},
"description": "Modify settings of your Tapo Camera.\n\nUse stream from Home Assistant:\nYes - Longer playback delay, lower CPU usage, allows playback control\nNo - Very short playback delay, higher CPU usage, no playback control"
}
Expand Down
10 changes: 6 additions & 4 deletions custom_components/tapo_control/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
"enable_motion_sensor": "Enable motion sensor",
"enable_time_sync": "Automatically synchronise time",
"enable_stream": "Use Stream from Home Assistant",
"enable_sound_detection": "Enable sound threshold detection",
"enable_sound_detection": "[Requires restart] Enable sound threshold detection",
"sound_detection_peak": "[Sound Detection] Peak in dB. 0 is very loud and -100 is low.",
"sound_detection_duration": "[Sound Detection] How long the noise needs to be over the peak to trigger the state.",
"sound_detection_reset": "[Sound Detection] The time to reset the state after no new noise is over the peak."
"sound_detection_reset": "[Sound Detection] The time to reset the state after no new noise is over the peak.",
"extra_arguments": "[Requires restart] Extra arguments for ffmpeg"
},
"description": "Almost there!\nJust some final options..."
}
Expand Down Expand Up @@ -61,11 +62,12 @@
"enable_motion_sensor": "Enable motion sensor",
"enable_stream": "Use Stream from Home Assistant [requires restart]",
"enable_time_sync": "Automatically synchronise time",
"enable_sound_detection": "Enable sound threshold detection",
"enable_sound_detection": "[Requires restart] Enable sound threshold detection",
"cloud_password": "Cloud Password (Optional)",
"sound_detection_peak": "[Sound Detection] Peak in dB. 0 is very loud and -100 is low.",
"sound_detection_duration": "[Sound Detection] How long the noise needs to be over the peak to trigger the state.",
"sound_detection_reset": "[Sound Detection] The time to reset the state after no new noise is over the peak."
"sound_detection_reset": "[Sound Detection] The time to reset the state after no new noise is over the peak.",
"extra_arguments": "[Requires restart] Extra arguments for ffmpeg"
},
"description": "Modify settings of your Tapo Camera.\n\nUse stream from Home Assistant:\nYes - Longer playback delay, lower CPU usage, allows playback control\nNo - Very short playback delay, higher CPU usage, no playback control"
}
Expand Down

0 comments on commit 4542548

Please sign in to comment.