unixorn / ha-mqtt-discoverable

Python module to create MQTT entities that are automatically discovered by Home Assistant
Apache License 2.0
102 stars 24 forks source link

Device does not get created #268

Open Tsjippy opened 4 weeks ago

Tsjippy commented 4 weeks ago

Describe the bug I run the test script external. I don't know what to use for host. The ip? The ip:1883?

To Reproduce Steps to reproduce the behavior: Run this script on a device which is not the home assistant server. But who is on the same network.

from ha_mqtt_discoverable import Settings, DeviceInfo
from ha_mqtt_discoverable.sensors import BinarySensor, BinarySensorInfo
host        = '192.168.0.216'
mqtt_settings = Settings.MQTT(host=host, username='someusername', password='somepassword')
device_info = DeviceInfo(name="Batteries", identifiers="unique_battery_id")
voltage_sensor_info = BinarySensorInfo(name="Voltage", device_class="voltage", unique_id="battery_voltage_1", device=device_info)
voltage_settings = Settings(mqtt=mqtt_settings, entity=voltage_sensor_info)
voltage_sensor = BinarySensor(voltage_settings)
voltage_sensor.update_state('49.3')

Expected behavior Batteries device should show up in home assistant but it doesnt.

If there is an connection or other error it should show me. But I don’t get any output at all in my python console. No succes message no error message.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

Tsjippy commented 4 weeks ago

Some more tests reveal that the script bbelow works:

#!/usr/bin/env python3
from struct import *
from datetime import datetime
import time
import paho.mqtt.client as mqtt
import json

mqtt_username = "some user"
mqtt_password =  "some password"
mqtt_host = "192.168.0.216"
mqtt_port = 1883

def on_connect(client, userdata, flags, reason_code):
    print(f"Connected with result code {reason_code}")
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("$SYS/#")

    config_payload = {
        "name": "Power Use General",
        "state_topic": "homeassistant/sensor/house/power_use1/state",
        "state_class": "measurement",
        "unit_of_measurement": "kWh",
        "device_class": "energy",
        "value_template": "{{ value }}",
        "unique_id": "power_use",
        "device": {
            "identifiers": [
                "thesensor"
            ],
            "name": "Power Use Sensors",
            "model": "None",
            "manufacturer": "None"
        },
        "icon": "mdi:home-lightning-bolt-outline",
        "platform": "mqtt"
    }
    result=client.publish(topic="homeassistant/sensor/house/power_use1/config", payload=json.dumps(config_payload), qos=0, retain=False)

    print(result)

    print('Device created')

    time.sleep(10)

    item1 = 1000
    item2 = 2000

    # Publish State1
    topic1 = "homeassistant/sensor/house/power_use1/state"
    client.publish(topic=topic1, payload=json.dumps(item1), qos=0, retain=False)

    # Publish State2
    topic2 = "homeassistant/sensor/house/power_use1/state"
    client.publish(topic=topic2, payload=json.dumps(item2), qos=0, retain=False)
    print("Published    '{0}' to '{1}'          Published '{2}' to '{3}'".format(str(item1), topic1, str(item2), topic2))

    time.sleep(6)

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.username_pw_set(mqtt_username, mqtt_password)
client.on_connect = on_connect
client.on_message = on_message
client.connect(mqtt_host, mqtt_port)

client.loop_forever()