jaruba / ha-samsungtv-tizen

📺 HomeAssistant - For Samsung TVs 2016+, Includes SmartThings API and Channel List Support
Apache License 2.0
285 stars 64 forks source link

Adding support for turn_on_action #76

Open ghost opened 4 years ago

ghost commented 4 years ago

I've added my Samsung TV to Home Assistant using your custom module and everything seems to work except turning it on when it has been off for ~60 seconds. As far as I understand that's what the mac: xx:xx:xx:xx:xx:xx is for. However, I can't get it to work since my Raspberry Pi isn't on the same network as the TV. For my LG TV's I'm using the turn_on_action to send a REST command to a server that is on the right network and thus turning on the TV/device.

I've tried setting broadcast_address: 192.168.1.255 (my Raspberry Pi is on 192.168.2.X, but that didn't seem to work either. It works great with my LGTV's, HA sees them on 192.168.1.71 and WOL works using my custom REST-service.

webostv:
  - name: TV
    host: 192.168.1.71
    turn_on_action:
      service: rest_command.wake_on_lan_tv

Current workaround is to create an automation that triggers when the state changes from "off" to "on". It work, but feels like a hack/workaround:

automation:
  - alias: 'Wake TV'
    initial_state: true
    trigger:
      - platform: state
        entity_id: media_player.tv
        from: 'off'
        to: 'on'
    action:
      - service: rest_command.wake_on_lan_tv
jaruba commented 4 years ago

As far as I understand that's what the mac: xx:xx:xx:xx:xx:xx is for.

This is correct

I've tried setting broadcast_address: 192.168.1.255 (my Raspberry Pi is on 192.168.2.X, but that didn't seem to work either.

broadcast_address should work, can't explain why it does not in your case, maybe you're not using it correctly. It's based on the official wake_on_lan integration (see here), have you tried setting it to the exact IP of the TV that you are trying to wake?

It works great with my LGTV's, HA sees them on 192.168.1.71 and WOL works using my custom REST-service.

I'm not entirely sure I understand the way you use this LG solution..

jaruba commented 4 years ago

scratch that, I don't think setting broadcast_address to the exact IP will work either.. this setting is confusing for me, although I added it in, and it definitely works, I still fail to understand the way that it should be set as I myself never had a use for it before, tinkering with it might solve your issues though, as WOL between different networks is possible

ghost commented 4 years ago

One issue with my workaround is that the TV, despite being connected by an Ethernet cable, seems to switch a lot between being ON and OFF. That means the automation is triggered way too many times.

image

jaruba commented 4 years ago

does it actually switch between on / off (as in, does the tv actually turn on / off) or are the states just showing it that way? if only the states show it that way, you might want to try changing the update_method in the config to something else that might fit your scenario better

ghost commented 4 years ago

@jaruba Apart from those longer green bars, the TV has been off the whole time. All those shorts green bars are false-positives. I've tried setting update_method: websockets and the issue is still present.

I've not yet tried setting it to smartthings yet as I'm unable to login to my Samsung account and getting the required token. Seems like Samsung is having some issues sending out their 2FA emails. However (if I understand the API correctly) I would prefer being able to control the TV locally without requiring Internet access.

Edit: I wonder if the TV does something every few minutes. When executing this in a Python shell while the TV is off:

requests.get('http://192.168.1.70:8001/api/v2/', timeout=1.0)

it usually gives me a ConnectTimeout exception which you catch and handle. But somethimes the code returns <Response [200]> with the actual data from the API. Almost like if the TV turns on (without the screen turning on) for checking for updates or other crap. Myabe it tries to update the SmartThing endpoint every now and then and turns on the API during that time?

So I wrote this:

import time
import datetime
import requests

while True:
    try:
        now = datetime.datetime.now()
        current_time = now.strftime("%H:%M:%S")
        requests.get('http://192.168.1.70:8001/api/v2/', timeout=1.0)
        print("Current Time =", current_time)
        time.sleep(10)
    except requests.exceptions.ConnectTimeout:
        pass

Output:

Current Time = 10:18:33
Current Time = 10:18:43
Current Time = 10:23:00
Current Time = 10:27:59
Current Time = 10:28:10
Current Time = 10:28:20
Current Time = 10:28:30
....

I'm using the HA companion app so I'm using this automation for testing. It sends me a notification every time the state changes:

automation:
  - alias: 'Wake Main TV'
    initial_state: true
    trigger:
      - platform: state
        entity_id: media_player.main_tv
        from: 'off'
        to: 'on'
    action:
      - service: notify.mobile_app_sm_n960f
        data:
          message: 'Main TV status changed!'
ghost commented 4 years ago

It's seems that update_method: smartthings works for now even though this isn't my preferred solution.

jaruba commented 4 years ago

read this: https://github.com/jaruba/ha-samsungtv-tizen/issues/77

lutierigb commented 3 years ago

it usually gives me a ConnectTimeout exception which you catch and handle. But somethimes the code returns <Response [200]> with the actual data from the API. Almost like if the TV turns on (without the screen turning on) for checking for updates or other crap. Myabe it tries to update the SmartThing endpoint every now and then and turns on the API during that time?

yeah i think it's something like that. i noticed the same in the past. a bit annoying, specially if you have automations triggered depending on the status of the TV.

I guess we are left with using smartthings to check if the TV is on or off.

jaruba commented 3 years ago

As far as I can tell there's no sure fire way to ensure the TV is either on or off. The component currently supports "ping", "websockets" and "smartthings", they all seem to fail in their own way sometimes. I wonder if it would still show as on sometimes if it took all the possible on / off sources and show as "on" only if all 2 (or 3 if smartthings is enabled) sources are "on" at the same time.

But it would probably still happen, as many mentioned that both "ping" and "websockets" have the same issue. So yeah, it's highly likely that the TV is actually on in the short time that it shows as being on.

Another (although absurd way) of doing this would be to delay changing to the "on" state until it's been on for a specific amount of time. I just don't think there's any good solution for this though, just bad solutions that might have unforeseen consequences.

fsteccanella commented 3 years ago

I d like to have too the turn_on_action feature. In my setup, with HA in a container, the WOL is not easy to manage.

jaruba commented 3 years ago

@fsteccanella I'm not against adding a turn_on_action, but i'd need to dig in the LG TV component to figure out how it's implemented..

fsteccanella commented 3 years ago

Thanks @jaruba indeed we don't have to reach out the LG implementation, also the official Samsung one has it: https://www.home-assistant.io/integrations/samsungtv/

Screwdgeh commented 3 years ago

Seems like an advanced feature that could be helpful to support people with:

Then again, a 'universal media player' could be used for all of this as well: https://www.home-assistant.io/integrations/universal/. Since it will pick the behavior of a first child and then you can override the specific actions you want different, it wouldn't be considerably more configuration work either.

@fsteccanella - could you have a look at using this universal media player and let us know if it could be sufficient for you?

jaruba commented 3 years ago

@Screwdgeh

I did look into implementing this at one point, but got stuck in a lot of work tasks.

Here are some code hints if you want to look into implementing it too:

fsteccanella commented 3 years ago

Seems like an advanced feature that could be helpful to support people with:

  • Volume control to directly set soundbars/external audio equipment instead of via the TV
  • Turning on/off in difficult network conditions, or turning on/off multiple devices at the same time

Then again, a 'universal media player' could be used for all of this as well: https://www.home-assistant.io/integrations/universal/. Since it will pick the behavior of a first child and then you can override the specific actions you want different, it wouldn't be considerably more configuration work either.

@fsteccanella - could you have a look at using this universal media player and let us know if it could be sufficient for you?

Thanks @Screwdgeh I'll give it a try