Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device does not get created #268

Open
Tsjippy opened this issue Oct 26, 2024 · 1 comment
Open

Device does not get created #268

Tsjippy opened this issue Oct 26, 2024 · 1 comment

Comments

@Tsjippy
Copy link

Tsjippy commented Oct 26, 2024

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):

  • running python o my windows 11 laptop

Additional context
Add any other context about the problem here.

@Tsjippy Tsjippy changed the title [BUG] Device does not get created Oct 27, 2024
@Tsjippy
Copy link
Author

Tsjippy commented Oct 27, 2024

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant