core
core copied to clipboard
Migrate BraviaTV to new async backend
Proposed change
This PR will swap the Sony Bravia TV integration backend from unmaintained bravia-tv to new pybravia. The new library has been completely rewritten using asynchronous calls and aiohttp. This is my first experience of creating a python library and if there are any comments regarding it, I have created a separate issue in the library repository.
Speed comparison (integration startup time/Apple M1):
Old backend: 3.6s New backend: 1.6s (1.09s if PSK auth used)
Todo list for integration in the future:
- [ ] Add new autorization method with PSK (Pre-Shared-Key) in
config_flow(already supported by backend) - [ ] Add
Buttonplatform for reboot TV and terminate apps - [ ] Add
play_mediasupport - [ ] Add support
media_browserand separate channels fromsource_list(related issue) - [ ] Add
ssdpdiscovery.
Latest changelog of pybravia: https://github.com/Drafteed/pybravia/commits/v0.2.0
Type of change
- [x] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [ ] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
Additional information
- This PR fixes or closes issue: fixes #72345 #74658
- This PR is related to issue: #70886
- Link to documentation pull request:
Checklist
- [x] The code change is tested and works locally.
- [x] Local tests pass. Your PR cannot be merged unless tests pass
- [x] There is no commented out code in this PR.
- [x] I have followed the development checklist
- [x] The code has been formatted using Black (
black --fast homeassistant tests) - [ ] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated for www.home-assistant.io
If the code communicates with devices, web services, or third-party tools:
- [x] The manifest file has all fields filled out correctly.
Updated and included derived files by running:python3 -m script.hassfest. - [x] New or updated dependencies have been added to
requirements_all.txt.
Updated by runningpython3 -m script.gen_requirements_all. - [x] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
- [ ] Untested files have been added to
.coveragerc.
The integration reached or maintains the following Integration Quality Scale:
- [ ] No score or internal
- [ ] 🥈 Silver
- [ ] 🥇 Gold
- [ ] 🏆 Platinum
To help with the load of incoming pull requests:
- [ ] I have reviewed two other open pull requests in this repository.
Hey there @bieniu, mind taking a look at this pull request as it has been labeled with an integration (braviatv) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)
I started messing around with this change and I'm having trouble connecting using PIN, investigating.
2022-08-02 10:40:54.992 DEBUG (MainThread) [pybravia.braviatv] Connect with pin: 4823, psk: False, clientid: HomeAssistant, nickname: Home Assistant
2022-08-02 10:40:54.992 DEBUG (MainThread) [pybravia.braviatv] Request http://192.168.2.45/sony/accessControl, data: {'method': 'actRegister', 'params': [{'clientid': 'HomeAssistant', 'nickname': 'Home Assistant', 'level': 'private'}, [{'value': 'yes', 'function': 'WOL'}]], 'id': 1, 'version': '1.0'}, headers: {'Authorization': 'Basic OjAwMDA=', 'Connection': 'keep-alive'}
2022-08-02 10:40:55.553 DEBUG (MainThread) [pybravia.braviatv] Response status: 200, result: {'result': [], 'id': 1}
2022-08-02 10:40:55.553 DEBUG (MainThread) [pybravia.braviatv] Request http://192.168.2.45/sony/system, data: {'method': 'setWolMode', 'params': [{'enabled': True}], 'id': 1, 'version': '1.0'}, headers: {}
2022-08-02 10:40:55.563 DEBUG (MainThread) [pybravia.braviatv] Response status: 403, result: {}
2022-08-02 10:40:55.563 DEBUG (MainThread) [pybravia.braviatv] Connect status: False
Martin, thanks for the tips! I changed the structure of the library - it now can throws exceptions on all client requests. As you said, I wrapped all the client methods of the coordinator with a decorator + refactored some part of the code on both sides. In addition, I added basic type annotations to the library.
I tested everything for a couple of days - everything works properly with my XH90.
Thanks, @bieniu 👍 Thanks, @MartinHjelmare 👍
@MartinHjelmare, what do you think about move host validation helper into /homeassistant/util/network.py: https://github.com/home-assistant/core/blob/19295d33ba60bad0a9dfbc4d512e156a09a010fc/homeassistant/components/braviatv/config_flow.py#L33-L39 https://github.com/home-assistant/core/blob/19295d33ba60bad0a9dfbc4d512e156a09a010fc/homeassistant/components/brother/config_flow.py#L27-L35 https://github.com/home-assistant/core/blob/19295d33ba60bad0a9dfbc4d512e156a09a010fc/homeassistant/components/dunehd/config_flow.py#L18-L28 https://github.com/home-assistant/core/blob/19295d33ba60bad0a9dfbc4d512e156a09a010fc/homeassistant/components/vilfo/config_flow.py#L32-L39
Sounds good. When would the regex check apply?
Sounds good. When would the regex check apply?
I think it should be like this:
def is_host_valid(host: str) -> bool:
"""Check if a given string is an IP address or valid hostname."""
if is_ip_address(host):
return True
if host[-1] == ".":
host = host[:-1]
allowed = re.compile(r"(?!-)[A-Z\d\-]{1,63}(?<!-)$", re.IGNORECASE)
return all(allowed.match(x) for x in host.split("."))
is_ip_address refers to:
https://github.com/home-assistant/core/blob/19295d33ba60bad0a9dfbc4d512e156a09a010fc/homeassistant/util/network.py#L59-L66