eslavnov / pylips

Control Philips TVs (2015+) and Ambilight (+ Hue) through their reverse-engineered API (+ MQTT support!)
MIT License
342 stars 60 forks source link

Home Assistant and MQTT #85

Closed XtraLarge closed 3 years ago

XtraLarge commented 3 years ago

Hello,

First of all you done a great job with pylips, so thank you very much for your efforts!

Now my situation and problem ....

I own to Philips TVs that supports pylips. With the last firmware it wasn't possibile to change the source "hdmi1" - "hdmi4" with my hormony hub. So I've looked for an other way to chnage the input channel of my tv. I've installed pylips and checked that it works. So python3 pylips.py --config /root/docker_pylips/settings-GaestezimmerTV.ini --command input_hdmi_1" chnges to hdmi1 without any problem.

So my next step was to run pylips in MQTT-mode. I've an MQTT broker "mosquitto" running on my home sssistant system.

[DEFAULT]
verbose = True
mqtt_listen = True
mqtt_update = True
num_retries = 3
update_interval = 3

[TV]
host = 10.10.6.2
port = 1926
apiv = 6
user = PbNyDWWLxwUzrk7z
pass = 2d9ca40c7d0bd7d2c3dadeef211d2eda2d757a575fkzgb4d7c927ab774620
protocol = https://

[MQTT]
host = hassio
port = 1883
user = <mqtt-broker>
pass = <password>
tls = False
cert_path =
topic_pylips = GaestezimmerTV_Command
topic_status = GaestezimmerTV_Status

On start, I received the following on the topic "GaestezimmerTV_Status":

{
    "powerstate": "On",
    "volume": 0,
    "muted": false,
    "cur_app": {
        "app": "TV",
        "channel": {
            "channel": {
                "ccid": 244,
                "preset": "9",
                "name": "kabel eins"
            },
            "channelList": {
                "id": "allsat",
                "version": "2"
            }
        }
    },
    "ambilight": {
        "styleName": "Lounge light",
        "isExpert": false,
        "menuSetting": "FRESH_NATURE",
        "stringValue": "Fresh Nature"
    },
    "ambihue": "On"
}

Now my problem, If I try to send:

"command":"powerstate"
 "body": "Off"

to the topic "GaestezimmerTV_Command" pylips stops with an error:

python3 pylips.py --config /root/docker_pylips/settings-GaestezimmerTV.ini
Connected to MQTT broker at hassio
Traceback (most recent call last):
  File "pylips.py", line 470, in <module>
    pylips = Pylips(args.config)
  File "pylips.py", line 103, in __init__
    self.start_mqtt_listener()
  File "pylips.py", line 379, in start_mqtt_listener
    self.mqtt.loop_forever()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1779, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1181, in loop
    rc = self.loop_read(max_packets)
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 1572, in loop_read
    rc = self._packet_read()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 2310, in _packet_read
    rc = self._packet_handle()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 2936, in _packet_handle
    return self._handle_publish()
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3216, in _handle_publish
    self._handle_on_message(message)
  File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3444, in _handle_on_message
    self.on_message(self, self._userdata, message)
  File "pylips.py", line 345, in on_message
    message = json.loads(msg.payload.decode('utf-8'))
  File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.7/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 10 (char 9)

Please help!!

XtraLarge commented 3 years ago

Hi, I thought I've found the problem. The JSON was malformed. The correct data for MQTT is:

{
   "command":"input_hdmi_1"
 }

So my only wish were that it would be geat, if the source would be a little bit more resistant for malformed JSONs.

eslavnov commented 3 years ago

Hi,

Good point on the malformed JSONs, thanks! I'll fix it in the next release. Also powerstate only returns the current power state, it does not allow changing it. Use {"command": "standby"} instead.