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

Add samsungtv.turn_off trigger #91903

Closed
wants to merge 9 commits into from

Conversation

felipecrs
Copy link
Contributor

Breaking change

Not a breaking change.

Proposed change

This adds the samsungtv.turn_off trigger, so that users can specify their own automations to turn off the Samsung TV, which is useful when the default does not work.

Type of change

  • 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

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • 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:

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

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • 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.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @chemelli74, @epenet, mind taking a look at this pull request as it has been labeled with an integration (samsungtv) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of samsungtv can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign samsungtv Removes the current integration label and assignees on the pull request, add the integration domain after the command.

@felipecrs
Copy link
Contributor Author

This is fully working on my local, testing against my Samsung TV device.

I will wait until I receive a positive feedback before start working on the documentation PR.

@felipecrs felipecrs changed the title Add samsungtv.turn_off trigger Add samsungtv.turn_off trigger Apr 23, 2023
homeassistant/components/samsungtv/device_trigger.py Outdated Show resolved Hide resolved
homeassistant/components/samsungtv/device_trigger.py Outdated Show resolved Hide resolved
@@ -0,0 +1,108 @@
"""Samsung TV device turn off trigger."""
Copy link
Contributor

Choose a reason for hiding this comment

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

This file is 99.7% similar to turn_on.py. You should reuse the code not duplicate it.

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 think I'm too dumb to do this. I got little to no Python experience. I really tried though. Would you mind doing it yourself? I can give you the needed permissions.

Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Instead of adding a new module turn_off.py do something like this:

  • rename turn_on.py to turn_on_off.py
  • rename async_get_turn_on_trigger to async_get_turn_on_off_trigger and make it return a list with both the triggers
  • update async_attach_trigger to check config[CONF_TYPE] and set the description accordingly

Comment on lines 61 to 64
triggers = [
async_get_turn_on_trigger(device_id),
async_get_turn_off_trigger(device_id),
]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
triggers = [
async_get_turn_on_trigger(device_id),
async_get_turn_off_trigger(device_id),
]
triggers = async_get_turn_on_off_triggers(device_id),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion but it's a lot more complicated than that. For example, I have to refactor attach_trigger, media_player.py and triggers.py. I just spent 2 hours trying to do it, and it ended up not working.

I'm pushing my draft changes if you want to review them.

@home-assistant home-assistant bot marked this pull request as draft May 24, 2023 13:07
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

Copy link
Contributor

@MichaelMraka MichaelMraka left a comment

Choose a reason for hiding this comment

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

return await platform.async_attach_trigger(hass, config, action, trigger_info)

platform_type does not have default value now so you have to define it:
+ return await platform.async_attach_trigger(hass, config, action, trigger_info, platform_type=config[CONF_PLATFORM])

This fixes all but 3 errors in turn_off tests. I don't know fix these.

@felipecrs
Copy link
Contributor Author

felipecrs commented May 26, 2023

@MichaelMraka thanks for the hint. The fix ended up being a little different though, but around the same place.

I'll investigate the rest of the issues but I believe it may have something to do with this:

https://github.com/home-assistant/core/pull/91903/files#diff-a0781812482f5a259a2d8830825092609c3331101aa8ba76df95cfb5b6c4be71R17-R21

@MartinHjelmare
Copy link
Member

I'd start by investigating more about the issue first before going ahead with this PR. Why doesn't turn off work for the device?

@felipecrs
Copy link
Contributor Author

felipecrs commented May 28, 2023

I'd start by investigating more about the issue first before going ahead with this PR. Why doesn't turn off work for the device?

Me too. And I did that:

But there is no solution. And considering that https://github.com/Ape/samsungctl is unmaintained since 2019, I believe there is virtually no chance of having a fix for it in the library.

@MartinHjelmare
Copy link
Member

We don't know what's wrong yet so we don't know where to fix it. I suggest continuing troubleshooting until there's some lead on what's the problem. Codeowners may have suggestions for troubleshooting steps.

@felipecrs
Copy link
Contributor Author

Ok. My understanding was that my device is simply unable to receive such command and thus this PR would make sense.

But it's true that's my understanding only, which may be wrong.

Any help is appreciated.

@MartinHjelmare
Copy link
Member

It may be that in the end we will conclude that the device doesn't support turn off but I don't think that's confirmed yet.

@felipecrs
Copy link
Contributor Author

@chemelli74 @epenet maybe you can recommend any troubleshooting step, or advise if you believe this PR is the best we can do about it.

I don't think anyone else will be able to help us otherwise.

@felipecrs
Copy link
Contributor Author

I was able to work around this issue by building an automation which achieves the same result:

alias: Turn off action for home theater
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.home_theater_do_escritorio
    from: "on"
    to: "off"
condition:
  - condition: template
    value_template: >-
      {{ trigger.to_state.context.user_id is not none or
      trigger.to_state.context.parent_id is not none }}
    alias: Confirm is triggered by turn_off service
action:
  - service: remote.send_command
    data:
      device: home_theater_do_escritorio
      command: power
    target:
      entity_id: remote.rm4_pro_remote
mode: single

Thus I'm closing this PR.

@felipecrs felipecrs closed this Jun 8, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jun 9, 2023
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.

Can't power off Samsung Home Theater
6 participants