Unable to use media_player.FireTV until HA has been rebooted after connection loss

Hello

I’ve got the FireTV component running, pointed at my Fire Stick. I’m hoping to use this, combined with an ICMP Ping sensor, to tell if my TV non-smart is on or off, because the Fire Stick is powered from my TV’s USB port.

Anyway. If the Fire Stick is on when the HA boots, it’s connected and I can manipulate it to my hearts content.

But - as soon as the Fire Stick is turned off (with the TV), my HA does not see it even when it comes back on, and therefore I can not manipulate it any more.

The state is whatever it was when the Fire Stick was turned off, and I get the following error about once per minute:

```
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 221, in async_update_ha_state
    await self.async_device_update()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 349, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/media_player/firetv.py", line 105, in _adb_wrapper
    returns = func(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/media_player/firetv.py", line 201, in update
    if not self.firetv.screen_on:
  File "/usr/local/lib/python3.6/site-packages/firetv/__init__.py", line 197, in screen_on
    return self._dump_has('power', 'Display Power', 'state=ON')
  File "/usr/local/lib/python3.6/site-packages/firetv/__init__.py", line 245, in _dump_has
    return self._dump(service, grep=grep).strip().find(search) > -1
  File "/usr/local/lib/python3.6/site-packages/firetv/__init__.py", line 234, in _dump
    return self._adb.Shell('dumpsys {0} | grep "{1}"'.format(service, grep))
  File "/usr/local/lib/python3.6/site-packages/adb/adb_commands.py", line 376, in Shell
    timeout_ms=timeout_ms)
  File "/usr/local/lib/python3.6/site-packages/adb/adb_protocol.py", line 411, in Command
    return ''.join(cls.StreamingCommand(usb, service, command, timeout_ms))
  File "/usr/local/lib/python3.6/site-packages/adb/adb_protocol.py", line 438, in StreamingCommand
    timeout_ms=timeout_ms)
  File "/usr/local/lib/python3.6/site-packages/adb/adb_protocol.py", line 372, in Open
    msg.Send(usb, timeout_ms)
  File "/usr/local/lib/python3.6/site-packages/adb/adb_protocol.py", line 233, in Send
    usb.BulkWrite(self.Pack(), timeout_ms)
  File "/usr/local/lib/python3.6/site-packages/adb/common.py", line 332, in BulkWrite
    return self._connection.send(data)
BrokenPipeError: [Errno 32] Broken pipe    
```

This said, my ICMP ping sensor works very well at detecting when the Fire Stick is on or off. So my question is this:

Can I run an automation to “reset” or reload the FireTV component within HA without having to reboot it?

Please try this custom component and let me know if it fixes the issue.

Hi

That’s worked, however if the FireTV is off when the HA restarts, it is never picked up unless the HA is rebooted and the FireTV is on.

That’s intended. I think most (all?) HA components work that way. After all, if it can’t connect to it when it starts up, how should it know that it’s a valid configuration and that the device actually exists?

That’s good that it fixed the reconnect issue! If more people report that it fixed it for them, I’ll submit the change to the official component.

I didn’t mean to sound dense by the way with that comment, you’re quite right.

My setup for example, runs an ICMP ping sensor to the IP of the Fire Stick, and therefore knows when it’s powered or not. Is there a way to re-initialise the firetv.py component once I know for certain it’s powered or am I out of luck on that front for the time being?

Thanks very much for your modification, for the time being I’ll keep that one running.

I don’t know of a way to re-initialize a component. But I think it will allow you to setup the media player even when it’s offline if you change line 95 to:

if not ftv.available and ping(config[CONF_HOST]):

That’s a massive help. Thanks again and thanks for your work.