postlund / pyatv

A client library for Apple TV and AirPlay devices
https://pyatv.dev
MIT License
891 stars 100 forks source link

Having trouble with HASS script to wake up/turn on AppleTV #784

Closed zitibooth closed 3 years ago

zitibooth commented 4 years ago

I have successfully installed the beta version into HASS using HACS. I've connected to the AppleTV (entered PIN from the TV twice), and I've even been able to use the Media Control card to see what's playing and to wake up the AppleTV (along with the television).

Where I am struggling is with a script to wake up the AppleTV. I've tried the remote.send_command, media_player.turn_on, toggle, etc. etc. I can't seem to find the right command or maybe syntax. Can someone share their script to turn wake up the Apple TV? I'm trying to add it to a script with several zwave lights.

I'm happy to share any additional information, just let me know what you need. Fair warning, I'm pretty new to all of this. Thank you, thank you, thank you!

Bill

postlund commented 4 years ago

It's the media_player.turn_on service you should use (and pass the entity_id). Give that a go and let me know if it works!

zitibooth commented 4 years ago

Thanks for the quick reply! I must have something setup wrong somewhere then. This is what I put in the UI.

image

And here's the yaml that generated.

apple_tv_test:
  alias: Apple TV Test
  icon: mdi:television
  mode: single
  sequence:
  - data: {}
    entity_id: media_player.living_room_apple_tv
    service: media_player.turn_on

But when I run it, I see a "failed to call service script.apple_tv_test"

Here's what I see in the log.

Log Details (ERROR)
Logger: homeassistant.components.script.apple_tv_test
Source: custom_components/apple_tv/__init__.py:188
Integration: Script (documentation, issues)
First occurred: July 27, 2020, 2:26:59 PM (5 occurrences)
Last logged: 8:57:02 AM

Apple TV Test: Error executing script. Unexpected error for call_service at pos 1:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 181, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 370, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1265, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1300, in _execute_service
    await handler.func(service_call)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 208, in handle_service
    await self.hass.helpers.service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 454, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 595, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 485, in _handle_entity_call
    await result
  File "/config/custom_components/apple_tv/media_player.py", line 258, in async_turn_on
    await self._manager.connect()
  File "/config/custom_components/apple_tv/__init__.py", line 188, in connect
    await self.atv.power.turn_on()
  File "/usr/local/lib/python3.8/site-packages/pyatv/mrp/__init__.py", line 502, in turn_on
    await self.protocol.send_and_receive(messages.wake_device())
  File "/usr/local/lib/python3.8/site-packages/pyatv/mrp/protocol.py", line 138, in send_and_receive
    return await self._receive(identifier, timeout)
  File "/usr/local/lib/python3.8/site-packages/pyatv/mrp/protocol.py", line 146, in _receive
    await asyncio.wait_for(semaphore.acquire(), timeout)
  File "/usr/local/lib/python3.8/asyncio/tasks.py", line 490, in wait_for
    raise exceptions.TimeoutError()
asyncio.exceptions.TimeoutError
postlund commented 4 years ago

It looks ok to me. Does any button, like left or right, work in this situation? Does the state update correct? To me, it kinda looks like the case a lot of other people are seeing when the connection hangs for some strange reason. Usually after putting the device to sleep.

zitibooth commented 4 years ago

It does seem to be reporting states. But I can't get it to respond to remote.send_command or the media_player service. The ONLY thing I can get it to do right now is speak with I type something here...

image

The first night I set it up, it was reporting the movie I was wathing on Plex but now it's not even doing that.

postlund commented 4 years ago

This is probably another case of what a lot of other people are seeing, when the connection sort-of hangs. I have not figured out why yet. Is it easy for you to recreate? Also, do you by any chance have a Mac with Xcode set up? I would love to see the system log from the Apple TV when this happens, which you can get if you enable development mode on your Apple TV.

The reason why "speak" is working is because that uses AirPlay, which is an orthogonal connection to MRP.

postlund commented 4 years ago

Are you running tvOS 14 beta or tvOS 13?

donkawechico commented 4 years ago

@postlund I may be experiencing the same (or similar) problem to OP, and I have MacOS with xcode installed.

I'm not getting any errors calling the media_player.turn_on service, but I'm definitely having problems with waking the AppleTV after putting it to sleep, and I do occasionally see the same stack trace error OP pasted above.

which you can get if you enable development mode on your Apple TV.

By this do you mean going to configuration.yaml and setting log level to debug for pyatv?

E.g.

logger:
  default: error
  logs:
    pyatv: debug
    custom_components.apple_tv: debug

Or is there a debug mode in the actual Apple TV device settings that I can somehow view using XCode on my macbook?

postlund commented 4 years ago

@donkawechico Sorry about the late reply, had to takes short break from pyatv. But I'm back to some extent now. I'm referring to the development mode you can enable from Xcode which you use for app development. It allows you to see the system log via the "System Messages" app in macOS.

postlund commented 3 years ago

@donkawechico If you still have time and possibility to try the above, I'm still very interested in the result.

postlund commented 3 years ago

@donkawechico Any chance to follow up on this?

postlund commented 3 years ago

Closing due to inactivity.