mirror of
https://github.com/fmtr/haco.git
synced 2026-10-25 18:40:15 -04:00
This commit is contained in:
parent
b75b8c60fc
commit
b243c74c90
5 changed files with 3 additions and 197 deletions
2
.github/workflows/main.yml
vendored
2
.github/workflows/main.yml
vendored
|
|
@ -53,7 +53,7 @@ jobs:
|
|||
with:
|
||||
tag_name: v${{ env.VERSION }}
|
||||
generate_release_notes: true
|
||||
prerelease: true
|
||||
#prerelease: true
|
||||
|
||||
- name: Build Wheel
|
||||
run: |
|
||||
|
|
|
|||
86
haco/sb1.py
86
haco/sb1.py
|
|
@ -1,86 +0,0 @@
|
|||
import asyncio
|
||||
import json
|
||||
|
||||
import aiomqtt
|
||||
|
||||
from haco.constants import MQTT_HOST, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD
|
||||
|
||||
DEVICE_ID = "haco_sandbox"
|
||||
ENTITY_ID = "demo_switch"
|
||||
DEVICE_NAME = "Haco Sandbox Device"
|
||||
ENTITY_NAME = "Demo Switch"
|
||||
|
||||
# Topics
|
||||
CONFIG_TOPIC = f"homeassistant/switch/{ENTITY_ID}/config"
|
||||
STATE_TOPIC = f"haco/{DEVICE_ID}/switch/state"
|
||||
COMMAND_TOPIC = f"haco/{DEVICE_ID}/switch/set"
|
||||
AVAIL_TOPIC = f"haco/{DEVICE_ID}/status"
|
||||
|
||||
|
||||
async def publish_discovery(client):
|
||||
"""Announce the entity to Home Assistant (retained)."""
|
||||
config = {
|
||||
"name": ENTITY_NAME,
|
||||
"unique_id": ENTITY_ID,
|
||||
"state_topic": STATE_TOPIC,
|
||||
"command_topic": COMMAND_TOPIC,
|
||||
"availability_topic": AVAIL_TOPIC,
|
||||
"payload_on": "ON",
|
||||
"payload_off": "OFF",
|
||||
"payload_available": "online",
|
||||
"payload_not_available": "offline",
|
||||
"device": {
|
||||
"identifiers": [DEVICE_ID],
|
||||
"name": DEVICE_NAME,
|
||||
"manufacturer": "Demo",
|
||||
"model": "Python MQTT Example"
|
||||
}
|
||||
}
|
||||
|
||||
await client.publish(CONFIG_TOPIC, json.dumps(config), retain=True)
|
||||
print(f"📢 Announced entity via {CONFIG_TOPIC}")
|
||||
|
||||
|
||||
async def handle_commands(client):
|
||||
"""Listen for command messages from HA."""
|
||||
async with client.messages() as messages:
|
||||
await client.subscribe(COMMAND_TOPIC)
|
||||
async for message in messages:
|
||||
payload = message.payload.decode()
|
||||
print(f"📥 Received command: {payload}")
|
||||
|
||||
# Echo back as new state
|
||||
await client.publish(STATE_TOPIC, payload, retain=True)
|
||||
|
||||
|
||||
async def main():
|
||||
while True:
|
||||
try:
|
||||
async with aiomqtt.Client(
|
||||
hostname=MQTT_HOST,
|
||||
port=MQTT_PORT,
|
||||
username=MQTT_USERNAME,
|
||||
password=MQTT_PASSWORD,
|
||||
will=aiomqtt.Will(
|
||||
topic=AVAIL_TOPIC,
|
||||
payload="offline",
|
||||
retain=True,
|
||||
qos=1,
|
||||
),
|
||||
) as client:
|
||||
|
||||
# Online now
|
||||
await client.publish(AVAIL_TOPIC, "online", retain=True)
|
||||
await publish_discovery(client)
|
||||
await client.publish(STATE_TOPIC, "OFF", retain=True)
|
||||
|
||||
# Wait for commands
|
||||
await handle_commands(client)
|
||||
|
||||
except aiomqtt.MqttError as e:
|
||||
print(f"⚠️ MQTT disconnected: {e}; retrying in 5s")
|
||||
await asyncio.sleep(5)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
108
haco/sb2.py
108
haco/sb2.py
|
|
@ -1,108 +0,0 @@
|
|||
import asyncio
|
||||
import random
|
||||
|
||||
import aiomqtt
|
||||
|
||||
from fmtr.tools import Constants, mqtt
|
||||
from fmtr.tools.json_tools import to_json
|
||||
from haco import constants
|
||||
|
||||
|
||||
async def handle_commands(client, control):
|
||||
"""Listen for command messages from HA."""
|
||||
|
||||
subs = control.subscriptions
|
||||
|
||||
async with client.messages() as messages:
|
||||
|
||||
for topic_sub in subs.keys():
|
||||
await client.subscribe(topic_sub)
|
||||
|
||||
async for message in messages:
|
||||
payload = message.payload.decode()
|
||||
print(f"{message.topic.value}{Constants.ARROW}{payload}")
|
||||
method = subs[message.topic.value]
|
||||
echo_val = method(payload)
|
||||
echo_val
|
||||
|
||||
# Echo back as new state
|
||||
# await client.publish(STATE_TOPIC, payload, retain=True)
|
||||
|
||||
|
||||
async def background_task(client, control):
|
||||
while True:
|
||||
await asyncio.sleep(10)
|
||||
cap = control.capabilities[1]
|
||||
value = random.randint(18, 24)
|
||||
print(f'human is setting temperature to {value}')
|
||||
await client.publish(cap.state.topic, payload=to_json(value), retain=True)
|
||||
|
||||
|
||||
class ClientHaco(mqtt.Client):
|
||||
|
||||
def __init__(self, *args, device=None, will=None, client_id=None, **kwargs):
|
||||
self.will = will or mqtt.Will(topic=str(constants.TOPIC_AVAIL), payload="offline", retain=True, qos=1, )
|
||||
client_id = client_id or constants.CLIENT_ID
|
||||
self.device = device
|
||||
# self.device.client = self
|
||||
super().__init__(*args, will=self.will, client_id=client_id, **kwargs)
|
||||
|
||||
|
||||
async def main():
|
||||
# Define capabilities
|
||||
|
||||
# device = Device(
|
||||
# name="Development Device",
|
||||
# controls=[
|
||||
# Climate(
|
||||
# #id="climate_living_room",
|
||||
# name="Climate Control",
|
||||
# fan_modes=['a','b','c'],
|
||||
# )
|
||||
# ]
|
||||
#
|
||||
# )
|
||||
|
||||
# announce=device.announce
|
||||
# subs=device.subscriptions
|
||||
|
||||
MQTT_HOST = "mqtt.service"
|
||||
MQTT_PORT = 1883
|
||||
MQTT_USERNAME = "user"
|
||||
MQTT_PASSWORD = None
|
||||
|
||||
while True:
|
||||
try:
|
||||
async with ClientHaco(
|
||||
hostname=MQTT_HOST,
|
||||
port=MQTT_PORT,
|
||||
username=MQTT_USERNAME,
|
||||
password=MQTT_PASSWORD,
|
||||
|
||||
client_id=constants.CLIENT_ID,
|
||||
# device=device,
|
||||
) as client:
|
||||
|
||||
# for topic, data in announce.items():
|
||||
# data_json = to_json(data)
|
||||
# print(f'Announcing {topic} with {data_json}')
|
||||
# await client.publish(topic, payload=data_json, retain=True)
|
||||
|
||||
# Online now
|
||||
await client.publish(client.will.topic, "online", retain=True)
|
||||
|
||||
# await publish_discovery(client)
|
||||
# await client.publish(STATE_TOPIC, "OFF", retain=True)
|
||||
|
||||
# Wait for commands
|
||||
# asyncio.create_task(background_task(client,control=climate))
|
||||
# await handle_commands(client,control=climate)
|
||||
client
|
||||
|
||||
except aiomqtt.MqttError as e:
|
||||
print(f"⚠️ MQTT disconnected: {e}")
|
||||
# await asyncio.sleep(5)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
|
@ -7,7 +7,7 @@ from haco.climate import Climate
|
|||
from haco.constants import MQTT_HOST
|
||||
from haco.device import Device
|
||||
from haco.obs import logger
|
||||
from haco.pulldown import Select
|
||||
from haco.select import Select
|
||||
from haco.switch import Switch
|
||||
|
||||
handler = logger.LogfireLoggingHandler()
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
0.1.0-alpha005
|
||||
0.1.0
|
||||
Loading…
Add table
Add a link
Reference in a new issue