MrAsterisco / homebridge-hisense-tv

A Homebridge plugin to control RemoteNow-compatible HiSense TVs.
MIT License
16 stars 8 forks source link

Off Status Not Reflecting & Input's Not Changing #14

Closed ken-driscoll closed 3 years ago

ken-driscoll commented 3 years ago

First off, thank you for developing this! I recently purchased a Hisense 55" U8G and it works fine with the RemoteNow app on my iPhone. Yesterday I tried setting up this Homebridge plugin, and got the TV added to my Home app. However I am running into a few issues that I wanted to make you aware of.

  1. When I turn off the TV using my remote, the Home app still says the TV is on. When I turn off the TV using the Home app (which does work), the TV shows as off for about 10 seconds, and then reverts to displays as on - even though the TV is indeed still off.
  2. I'm unable to switch inputs using the Home app. Also the current input I'm on doesn't seem to reflect in the Home app.

Here is my config snippet for this platform:

{
    "ifname": "eth0",
    "devices": [
        {
            "id": "officetvu8g",
            "name": "Office TV",
            "ipaddress": "192.168.4.58",
            "macaddress": "30:32:35:b0:30:8c",
            "sslmode": "default"
        }
    ],
    "platform": "HiSenseTV"
}

Also I see this error in my Homebridge console repeating sometimes: [23/06/2021, 18:04:18] [HiSenseTV] An error occurred while fetching the current input: SyntaxError: Unexpected token b in JSON at position 0

I'm running Homebridge on a Raspberry Pi 4 if that matters.

Please let me know if there is any additional information I can provide! I'm also a developer, but don't have experience with Homebridge plugins other than as a consumer - but I'm happy to get my feet wet if you're able to point me in the right direction!

MrAsterisco commented 3 years ago

Hi @ken-driscoll , thanks for reporting this!

All the issues you described point to something wrong/unexpected being returned by the TV. This plugin uses a Python tool under the hood: the first thing to debug would be that.

You should be able to run the plugin by moving into the folder where the plugin is installed and looking for the bin folder. In there, you'll find a hisensetv.py. You can run it with python3.8 hisensetv.py and that will give you the list of commands.

Specifically, it'd be great to see the output of running the following:

python3.8 hisensetv.py --ifname eth0 --get sources 192.168.4.58

If this works, then you might have found a bug in the Homebridge plugin. If it doesn't work, it could mean one of three things: there's a bug in the Python script, there's something wrong in the configuration, the TV works differently than the models that I tested.

Once we identify the root cause, I'm happy to guide you through the code or even fix it myself!

As an additional note: it is kind of expected to see a little bit of delay if you turn on or off the TV outside of Home (i.e. with the remote or with the HiSense app), especially if you are looking at the Home app at the same time. Unfortunately, I haven't found a solution for that yet: basically, I'm periodically pinging the TV to see if it's turned on... so yeah, you can imagine there might be some delay.

ken-driscoll commented 3 years ago

Hi @MrAsterisco, thanks for getting back to me so quickly! I ran the command you specified and got the following response:

[2021-06-24 15:03:12,750] [INFO    ] Unverified SSL context created.
[
    {
        "displayname": "Channels",
        "is_signal": "1",
        "sourceid": "1",
        "sourcename": "Tuner"
    },
    {
        "displayname": "Apple TV",
        "is_signal": "1",
        "sourceid": "2",
        "sourcename": "HDMI 4"
    },
    {
        "displayname": "HDMI 1",
        "is_signal": "0",
        "sourceid": "3",
        "sourcename": "HDMI 1"
    },
    {
        "displayname": "HDMI 2",
        "is_signal": "0",
        "sourceid": "4",
        "sourcename": "HDMI 2"
    },
    {
        "displayname": "Sound Bar",
        "is_signal": "1",
        "sourceid": "6",
        "sourcename": "HDMI 3"
    }
]

Let me know if this helps! Also let me know if there is another command I could run to see what Hisense says the TV On/Off state is...

Also to clarify the first issue from the list in my original post, the delay in the state update in the Home app is totally understandable. The issue I'm running into is the state doesn't stay off when the TV is off. Like the TV has been off all day, but it displays as "On" in the Home app.

MrAsterisco commented 3 years ago

@ken-driscoll Mmmm... So the output command seems to be working fine. You can run this to get the state of the tv:

python3.8 hisensetv.py --ifname eth0 --get state 192.168.4.58

However, this is not the actual command that the plugin runs to know whether your TV is on or not, because it sometimes idles indefinitely when the TV is off. So, instead, the plugin runs this code, which basically:

If you always see your TV as turned on, this might indicate that the TV is actually accepting connections even when in stand-by. You can quickly test this by using telnet to connect to the TV IP address and the port I wrote above. If the connection is established successfully, this confirms my theory.

To be honest, if this is the problem, I wouldn't how to fix it. My TV, which is a different model than yours, when in stand-by doesn't allow connections; it only allows the magic packet to do the Wake-on-LAN and nothing else. Also, if I try to use the RemoteNow app with the TV off, the app will just tell me that I have to turn it on to do anything.

ken-driscoll commented 3 years ago

Welp, I've identified part of the problem - at least with the On/Off state. My U8G has a setting in Settings > Advanced System > Screenless Mode, which was on by default. When I toggle it off, it displays a warning message that states "It may cause your mobile devices on your network to turn on TV failure" (the English in the message is a little off). When I toggle that off, the On/Off state of the TV in the Home app displays correctly - however when the TV is in the off state, I can't turn it on from the Home app.

So basically, it seems like this "Screenless Mode" may be Hisense's new way of basically just turning off the screen but leaving the rest of the system on, so things like the RemoteNow app can connect. Unfortunately, it sounds like it also leaves on the 36669 port :(

Side note: At this point, I was about to say "this is good enough for my use case" because all I really want to do is create an automation to turn on an LED strip when the TV is turned on, but then I found that the TV's CEC functionality doesn't quite work the same with "Screenless Mode" turned off - my sound bar wont turn on with the TV 👎

Anyway, to provide some additional output, here is is what I found when I ran python3.8 hisensetv.py --ifname eth0 --get state 192.168.4.58 in the various modes and on/off states:

"Screenless Mode" Off and TV State On:

[2021-06-25 11:19:07,003] [INFO    ] Unverified SSL context created.
b''

"Screenless Mode" Off and TV State Off (times out):

[2021-06-25 11:19:23,105] [INFO    ] Unverified SSL context created.
Traceback (most recent call last):
  File "hisensetv.py", line 6, in <module>
    __main__.main()
  File "/usr/local/lib/node_modules/homebridge-hisense-tv-remotenow/bin/hisensetv/__main__.py", line 128, in main
    with tv:
  File "/usr/local/lib/node_modules/homebridge-hisense-tv-remotenow/bin/hisensetv/__init__.py", line 137, in __enter__
    self._mqtt_client.connect(self.hostname, self.port)
  File "/usr/local/lib/node_modules/homebridge-hisense-tv-remotenow/bin/paho/mqtt/client.py", line 941, in connect
    return self.reconnect()
  File "/usr/local/lib/node_modules/homebridge-hisense-tv-remotenow/bin/paho/mqtt/client.py", line 1075, in reconnect
    sock = self._create_socket_connection()
  File "/usr/local/lib/node_modules/homebridge-hisense-tv-remotenow/bin/paho/mqtt/client.py", line 3546, in _create_socket_connection
    return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
  File "/usr/lib/python3.7/socket.py", line 727, in create_connection
    raise err
  File "/usr/lib/python3.7/socket.py", line 716, in create_connection
    sock.connect(sa)
socket.timeout: timed out

"Screenless Mode" On and TV On:

[2021-06-25 11:22:40,603] [INFO    ] Unverified SSL context created.
{
    "endtime": 0,
    "starttime": 0,
    "statetype": "livetv"
}

"Screenless Mode" On and TV Off (no change):

[2021-06-25 11:23:11,305] [INFO    ] Unverified SSL context created.
{
    "endtime": 0,
    "starttime": 0,
    "statetype": "livetv"
}

The RemoteNow app is also a bit funky for me...it shows when the TV is on/off correctly (so there has to be a way!), but when the TV is off, and I tap the "Turn On" button, it just spins and does nothing. 🤷 At least your Homebridge app lets me turn on the TV!

MrAsterisco commented 3 years ago

Wow, this is super weird. So, the TV seems to remain on with this "Screenless mode", but it doesn't respond. I'm not sure what that b'' means...

At this point, I think it'd be good if you could just debug the plugin yourself. I think it'd just be a matter of adding a new stand-by/on detection mode, so that we don't break the currently supported models, but we have free space to experiment with yours. Unfortunately, I won't be able to help you directly by writing the code myself, because I don't have access to your TV; however, I'm happy to provide support.

Do you have a machine where you can install a development version of Homebridge?

This is what I usually do: I have a mini-server running with the production instance of Homebridge and that is what I use for my own home. But I also have a VM on my MacBook with Ubuntu that runs another instance of Homebridge: this is what I use to develop.

Then, the plugin is a normal npm package: you just fork the repo, run npm install and you're good to go to start developing. When you make changes, assuming you have a Homebridge instance running locally, you just need to run sudo npm link to install the plugin and then restart Homebridge. Also, make sure to turn on the debug mode in the Homebridge settings.

Let me know if you encounter any issue or if you need further assistance from me.

ken-driscoll commented 3 years ago

Sorry for the delay in my response! I spent some time trying to figure out another way to get the TV status through Hisense's APIs but didn't get anywhere. I ended up shifting to using an adb homebridge plugin (https://github.com/dwaan/homebridge-adb) to tap into the Android TV part of the TV to get the state and that has been working well as an alternative! Thanks for all your help, but I'm going to close this issue for now.

MrAsterisco commented 3 years ago

@ken-driscoll no worries, I'm always happy to help.

This might be an interesting upgrade for the plugin: we could add support for the same thing that the ADB plugin does, but I wouldn't know how to test it, as my TV doesn't run Android. If you have some time in the future, feel free to look into it: I'm happy to guide you through the necessary steps.