Apple TV Integration - tvOS 15 Beta

New update is out!

Edit: I managed to not include the manifest changes (amateur hour…), but I have pushed and update now!

2 Likes

Hmm… still having issues sending commands with the remote. I removed the AppleTV and readded it.
Sometimes it works, and sometimes it doesn’t.

For example, if apple tv goes to screen saver then commands don’t work but if I wake it up with an iPhone it sorta works … …once. I can send it to sleep using (home_hold, select) and then wake it up after (wakeup). But once it wakes up no commands work anymore, even if I interact with an iphone.

The errors i get:

Unable to send commands, not connected to AppleTV

and during start-up i have this:

Logger: pyatv.core.mdns
Source: /usr/local/lib/python3.9/site-packages/pyatv/core/mdns.py:305
First occurred: 11:43:11 PM (24 occurrences)
Last logged: 11:43:11 PM

exception during data handling
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pyatv/core/mdns.py", line 303, in datagram_received
    delegate.datagram_received(data, addr)
  File "/usr/local/lib/python3.9/site-packages/pyatv/core/mdns.py", line 429, in datagram_received
    decoded_msg = DnsMessage().unpack(data)
  File "/usr/local/lib/python3.9/site-packages/pyatv/support/dns.py", line 365, in unpack
    header = DnsHeader.unpack_read(buffer)
  File "/usr/local/lib/python3.9/site-packages/pyatv/support/dns.py", line 286, in unpack_read
    return cls._make(unpack_stream(">6H", buffer))
  File "/usr/local/lib/python3.9/site-packages/pyatv/support/dns.py", line 22, in unpack_stream
    return struct.unpack(fmt, buffer.read(size))
struct.error: unpack requires a buffer of 12 bytes

The message Unable to send commands, not connected to AppleTV means that no connection exists, so it probably fails to recover the connection. There should be some other error in the log indicating this? Is the media player “available” in the UI (i.e. not greyed out)?

EDIT: NVM I see you are discussing it here https://github.com/postlund/pyatv/issues/1333#issuecomment-929017973

I get Authentication problem, it fails on the airplay one

Source: custom_components/apple_tv/config_flow.py:285
Integration: Apple TV
First occurred: 14:58:49 (1 occurrences)
Last logged: 14:58:49

Authentication problem
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pyatv/support/__init__.py", line 33, in error_handler
    return await func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/auth.py", line 54, in start_pairing
    resp = await self.protocol.exchange_opack(
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/protocol.py", line 80, in exchange_opack
    unpacked_object, _ = opack.unpack(payload)
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/opack.py", line 108, in unpack
    return _unpack(data, [])
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/opack.py", line 115, in _unpack
    if data[0] == 0x01:
IndexError: index out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/config/custom_components/apple_tv/config_flow.py", line 285, in async_pair_next_protocol
    await self.pairing.begin()
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/pairing.py", line 53, in begin
    await error_handler(
  File "/usr/local/lib/python3.9/site-packages/pyatv/support/__init__.py", line 41, in error_handler
    raise fallback(str(ex)) from ex
pyatv.exceptions.PairingError: index out of range```

@postlund Media player works, I can send actions to it like “play” or skip and it works. Just weird issues with remote service commands. No other errors that I could see

@postlund Is this backward compatible with tvOS 14? I have some ATVs on 15, but there is one that I am intentionally not updating.

Yeah, it is backwards compatible!

1 Like

I’m seeing tis in the HA log:

2021-10-03 06:32:39 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 153, in async_init
    flow, result = await task
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 179, in _async_init
    result = await self._async_handle_step(flow, flow.init_step, data, init_done)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 260, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
  File "/config/custom_components/apple_tv/config_flow.py", line 177, in async_step_zeroconf
    return await self.async_find_device_wrapper(self.async_found_zeroconf_device)
  File "/config/custom_components/apple_tv/config_flow.py", line 203, in async_find_device_wrapper
    return await next_func()
  File "/config/custom_components/apple_tv/config_flow.py", line 181, in async_found_zeroconf_device
    await self.async_set_unique_id(self.device_identifier)
  File "/config/custom_components/apple_tv/config_flow.py", line 111, in device_identifier
    if self.atv.identifier in entry.data[CONF_IDENTIFIERS]:
KeyError: 'identifiers'

apple_tv installed in custom_components, but no integration setup.

Downgrade to previous version, remove the integrations and then upgrade or remove the Apple TV entries manually from .storage/core.config_entries.

Thank you. I removed the AppleTV entry from the core.config_entries file, restarted, and am getting a New device discovered prompt. Should I attempt the integration or ignore?

Follow through, it should work fine I hope!

This is what appears when I attempt to setup the integration:

And that’s with the latest version of the component, right?

Yes. Downloaded and installed this morning.

  "codeowners": ["@postlund"],
  "iot_class": "local_push",
  "version": "1.1.0"

Gah, I thought the latest update would address this. But I do see something that could be the reason here. I will experiment a bit and push and update as soon as I get some time.

@postlund, thanks for your work. I’ve intalled your software and the integration has gone OK.

Using Apple TV 4K with tvOS 15.

I want to automate the usage of a remote (bluetooth) and all seems to work fine. I’m able to send commands to the ATV.

Are all the available commands the ones liested in the HA documentation for ATV or are more available?

Thanks,

The list should be complete I think. You can technically call a few more, like play for instance, but you should use the regular services for that. Anything you are missing?

Thank you. I’ll check for an update later.

Mainly to suspend the ATV. I send home, home_hold and select commands but not always work

That is exactly what the turn_off method does in the power interface, which isn’t callable right now. It will be part of the regular power functions (when you turn on or off the media player for instance) pretty soon I think.

1 Like