Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File Path fixes for RPi Camera #12338

Merged
merged 5 commits into from
Feb 14, 2018
Merged

Conversation

FrederikBolding
Copy link
Contributor

@FrederikBolding FrederikBolding commented Feb 12, 2018

Description:

Related issue (if applicable): fixes #10360

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#4650

Example entry for configuration.yaml (if applicable):

Checklist:

  • The code change is tested and works locally.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass

@homeassistant
Copy link
Contributor

Hi @FrederikBolding,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@FrederikBolding FrederikBolding changed the title Checked file path with is_allowed_path() for RPi Camera Checked file path with is_allowed_path() for RPi Camera (WIP) Feb 12, 2018
Copy link
Member

@fabaff fabaff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also need a documentation update because it's a breaking change.

@@ -26,6 +26,7 @@
CONF_TIMELAPSE = 'timelapse'
CONF_VERTICAL_FLIP = 'vertical_flip'

DEFAULT_FILE_PATH = ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it prematurely. It was supposed to point somewhere else to prevent the files from being saved in the camera components folder.

@@ -36,7 +37,7 @@
DEFAULT_VERTICAL_FLIP = 0

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_FILE_PATH): cv.string,
vol.Optional(CONF_FILE_PATH, default=DEFAULT_FILE_PATH): cv.string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that with cv.isfile instead of cv.string we could get rid of the checks in setup_platform().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, seems like it! Will change in a few 😄

@FrederikBolding FrederikBolding changed the title Checked file path with is_allowed_path() for RPi Camera (WIP) File Path fixes for RPi Camera (WIP) Feb 12, 2018
@FrederikBolding FrederikBolding changed the title File Path fixes for RPi Camera (WIP) File Path fixes for RPi Camera Feb 12, 2018
@FrederikBolding
Copy link
Contributor Author

There we go! Let me know if there are any issues! 😄

Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, noticed an improvement.

@@ -78,24 +77,17 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
CONF_HORIZONTAL_FLIP: config.get(CONF_HORIZONTAL_FLIP),
CONF_VERTICAL_FLIP: config.get(CONF_VERTICAL_FLIP),
CONF_FILE_PATH: config.get(CONF_FILE_PATH,
os.path.join(os.path.dirname(__file__),
'image.jpg'))
hass.config.path('image.jpg'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename it to camera.rpi_camera.jpg ? Makes it clear where the image is from.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, why do we even do this, wouldn't it make more sense to just use a temporary file when none is given? No reason to put it in the config dir.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For creating temp files and dirs https://docs.python.org/3/library/tempfile.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CONF_FILE_PATH: config.get(CONF_FILE_PATH,
    tempfile.NamedTemporaryFile(suffix='.jpg', delete=False).name)

Are you talking about something like this?
I'm a bit unfamiliar with the Home Assistant code-base still, but wouldn't we run into issues with multiple images being saved as Home Assistant is restarted? Because we aren't deleting the used image.
Or is the default generated temp file path saved somehow?
Otherwise, we would have to handle deletion of the temp file when Home Assistant is restarted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't instantiate it as default param, because that way you would still create a temp file even when CONF_FILE_PATH is specified.

You can listen to EVENT_HOME_ASSISTANT_STOP to run code when Home Assistant stops.

hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, delete_temp_file)

# Check whether the file path has been whitelisted
if not hass.config.is_allowed_path(file_path):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to check file path to be allowed if we use a temp file.

@balloob
Copy link
Member

balloob commented Feb 14, 2018

🎉 🎉

@balloob balloob merged commit 6500cb7 into home-assistant:dev Feb 14, 2018
@balloob balloob mentioned this pull request Feb 22, 2018
@home-assistant home-assistant locked and limited conversation to collaborators May 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement is_allowed_path() for Raspberry Pi Camera
4 participants