Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Make Yi platform async #14944

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

Merged
merged 5 commits into from
Jun 13, 2018
Merged

Make Yi platform async #14944

merged 5 commits into from
Jun 13, 2018

Conversation

bachya
Copy link
Contributor

@bachya bachya commented Jun 12, 2018

Description:

This PR makes the Yi camera platform async.

Related issue (if applicable): N/A

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

Example entry for configuration.yaml (if applicable):

camera:
  - platform: yi
    name: Kitchen Camera
    host: 192.168.1.100
    username: root
    password: password123
    ffmpeg_arguments: -s 800x450

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated in home-assistant.github.io

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

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New or updated dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
    - [ ] New files were added to .coveragerc.

Copy link
Member

@OttoWinter OttoWinter left a comment

Choose a reason for hiding this comment

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

Looks pretty much good to me 👍. Just a few quick comments

latest_dir, videos[-1])
except (ConnectionRefusedError, StatusCodeError) as err:
print('There was an issue: {0}'.format(err))
return
Copy link
Member

Choose a reason for hiding this comment

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

Inconsistent return - once you're returning False, once a string and here None. It does work but I would suggest switching both error cases to return None.

self.user, self.passwd, self.host, self.port, self.path,
latest_dir, videos[-1])
except (ConnectionRefusedError, StatusCodeError) as err:
print('There was an issue: {0}'.format(err))
Copy link
Member

Choose a reason for hiding this comment

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

Use _LOGGER.error(). Also, I don't think "there was an issue ..." Is not that great of a issue message :P

Maybe something like: "Error while fetching video URL: ..."

return 'ftp://{0}:{1}@{2}:{3}{4}/{5}/{6}'.format(
self.user, self.passwd, self.host, self.port, self.path,
latest_dir, videos[-1])
await ftp.connect(self.host)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe For a future PR if it's possible: Opening a whole new FTP session with a new socket for each new image is ... a bit inefficient I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great feedback; I think I should be able to address this easily. Hang tight.

Copy link
Member

Choose a reason for hiding this comment

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

Ah just realized that ffmpeg/whatever is passing on the video stream is opening a new socket for each image anyway... So this is probably not that big of a speedup in the end. Sorry about that!

@dgomes
Copy link
Contributor

dgomes commented Jun 13, 2018

I would just like to point out #14244 existence

@bachya
Copy link
Contributor Author

bachya commented Jun 13, 2018

@dgomes To what end? Yi Technologies has split off from Xiaomi, so blending them together makes no sense. That PR looks like a near-exact copy of my original one (prior to this PR’s async work).

EDIT: I’ve taken a deeper look at that PR. The connection between the Xiaofang and Yi platforms – if there is one – is how the videos are accessed: via FTP. There’s no overarching similarity between them beyond that.

So, there are some options:

  1. Leave each to themselves (as it is now).
  2. Attempt to incorporate the changes in Xiaomi Cameras - multiple models #14244 into this (so that the Yi platform just happens to accommodate Xiaofang, too).
  3. Create an FtpCamera component and make each of these cameras a platform under it.

I honestly don’t think 3 makes sense: that’s consolidation for consolidation’s sake. 2 doesn’t feel quite right either. I’d recommend 1 and if there’s heartburn about code replication, we should move the FTP video-access logic into a helper via a separate PR.

Lastly, no matter what, I’d still suggest the async changes: since we’re at a minimum of Python 3.5.3 and have async/await, there’s no reason not to use aioftp.

@dgomes
Copy link
Contributor

dgomes commented Jun 13, 2018

First aioftp is very much welcomed!

Now in relation to #14244:

It looks almost equal to the original Yi Camera because that was the initial PR submitted. It was a blant copy&paste of the Yi Camera code as Xiaomi Camera with minor tweaks to accommodate Xiaofang.

IMHO: If the platforms share >90% of the code they should be the same platform.

To me what makes no sense is that the same code does not exist in two separate platforms. An FTPcamera actually makes more sense to me, not through a new component (#3) but through your proposal #2 (just renaming it Yi Camera -> FTP Camera and adding configuration options to accommodate various cameras).
I see many other cameras beyond Yi and Xiaofang using this platform.

@balloob balloob merged commit d549e26 into home-assistant:dev Jun 13, 2018
@ghost ghost removed the in progress label Jun 13, 2018
@balloob
Copy link
Member

balloob commented Jun 13, 2018

IMHO: If the platforms share >90% of the code they should be the same platform.

I disagree with this. If they are two different brands, they should be different platforms with their own code. Just like light and switch are very similar but still have their own code.

Eventually these platforms will evolve and they will no longer have 90% overlap, splitting them up then is going to be a pain.

@dgomes
Copy link
Contributor

dgomes commented Jun 13, 2018

On the other hand, if a bug/change is made to one of the platforms, the others will not get updated. Very hard to maintain code this way :(

@balloob
Copy link
Member

balloob commented Jun 13, 2018

If you merge 2 platforms in one piece of code, each feature that is only supported by some models, will create more permutations that need to be tested and will be easier to have bugs. So if you have 2 simple implementations, it's easier to find and fix a bug.

@dgomes
Copy link
Contributor

dgomes commented Jun 13, 2018

The only difference is the FTP path

And my point is that the 90% of code shared is common to any camera with an FTP interface, therefore why I said an FTP Camera made more sense than 20+ different cameras where only the path changes.

@bachya
Copy link
Contributor Author

bachya commented Jun 13, 2018

I don’t think that was the only change – didn’t he also mentioned that at that path, he couldn’t grab the latest file, but had to grab the one right before it?

And any rate, combining these would require an architectural discussion – some platforms might point at a single folder, some platforms my point at folders of folders, various platforms will need to choose files with in those folders based on certain criteria, etc.

awarecan pushed a commit to awarecan/home-assistant that referenced this pull request Jun 13, 2018
* Conversion complete

* Updated requirements

* Got rid of 3.6-specific syntax

* Removed more 3.6-specific syntax

* Contributor-requested changes
@bachya bachya deleted the yi-async branch June 13, 2018 16:47
@dgomes dgomes mentioned this pull request Jun 16, 2018
8 tasks
@balloob balloob mentioned this pull request Jun 22, 2018
girlpunk pushed a commit to girlpunk/home-assistant that referenced this pull request Sep 4, 2018
* Conversion complete

* Updated requirements

* Got rid of 3.6-specific syntax

* Removed more 3.6-specific syntax

* Contributor-requested changes
@home-assistant home-assistant locked and limited conversation to collaborators Dec 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants