First working implementation
This commit is contained in:
parent
1dc7cae911
commit
5c58be3aa8
12 changed files with 114 additions and 28 deletions
30
Dockerfile
Normal file
30
Dockerfile
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Use an official Python runtime as a parent image
|
||||
FROM python:3.11
|
||||
|
||||
# Install system dependencies
|
||||
RUN apt-get update && apt-get install -y \
|
||||
libddcutil-dev \
|
||||
libddcutil4 \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Set the working directory in the container
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
# Copy the current directory contents into the container at /usr/src/app
|
||||
COPY . .
|
||||
|
||||
# Build the extension module
|
||||
RUN (cd simpleddc-extension && python setup.py build_ext --inplace)
|
||||
|
||||
# Install Python dependencies for the ddc-mqtt project
|
||||
RUN pip install --no-cache-dir -r ./ddc-mqtt/requirements.txt
|
||||
|
||||
# Ensure the built module is available to the project
|
||||
ENV PYTHONPATH "${PYTHONPATH}:/usr/src/app/simpleddc"
|
||||
|
||||
# Change the working directory to run the ddc-mqtt project
|
||||
WORKDIR /usr/src/app/ddc-mqtt
|
||||
|
||||
# Run start.py when the container launches
|
||||
CMD ["python", "start.py"]
|
||||
|
||||
7
LICENSE.md
Normal file
7
LICENSE.md
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Copyright (c) 2022 Moises Martinez
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
7
README.md
Normal file
7
README.md
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# ddc-mqtt
|
||||
|
||||
Setup:
|
||||
|
||||
0. Connect a device where to run this to a DDC enabled port of your monitor— I use a raspberry pi with a GN95C Samsung display
|
||||
1. create a config.yml (copy it from rename_to_config.yml) for your mqtt credentials and monitor setup (Use ddcutil to get inputs and codes)
|
||||
2. docker-compose up -d
|
||||
|
|
@ -7,8 +7,8 @@ device = {
|
|||
|
||||
|
||||
display_device = {
|
||||
"identifiers": ["Kid"],
|
||||
"name": "Display # KVM",
|
||||
"identifiers": ["Input"],
|
||||
"name": "Display Input KVM",
|
||||
"model": "Kikkei-display-kvm",
|
||||
"manufacturer": "Kikkei Labs",
|
||||
}
|
||||
|
|
@ -16,13 +16,13 @@ display_device = {
|
|||
display_input_entity = {
|
||||
"generic_switch": 'homeassistant/switch/display-kvm-#/?-switch/config',
|
||||
"generic_switch_config": {
|
||||
"availability_topic": "kikkei/display-kvm/garbage",
|
||||
"state_topic": "kikkei/display-kvm/kids/#/?/state",
|
||||
"availability_topic": "kikkei/display-kvm/availability",
|
||||
"state_topic": "kikkei/display-kvm/#/?/state",
|
||||
"name": "",
|
||||
"unique_id": "",
|
||||
"object_id": "",
|
||||
"payload_available": "ON",
|
||||
"payload_not_available": "OFF",
|
||||
"payload_available": "online",
|
||||
"payload_not_available": "offline",
|
||||
#"json_attributes_topic": "kikkei/household/#/%/attributes",
|
||||
"state_on": "true",
|
||||
"state_off": "false",
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
from devices import garbage_config
|
||||
import paho.mqtt.client as mqtt
|
||||
import json
|
||||
from timer import Timer
|
||||
|
|
|
|||
2
ddc-mqtt/requirements.txt
Normal file
2
ddc-mqtt/requirements.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pyyaml>=6.0
|
||||
paho-mqtt>=1.6.1
|
||||
|
|
@ -4,17 +4,18 @@ from timeit import default_timer as timer
|
|||
import yaml
|
||||
import os
|
||||
import json
|
||||
from devices import display_device, display_input_entity, hass_display_sensor
|
||||
from devices import display_device, display_input_entity
|
||||
import simpleddc
|
||||
|
||||
class Service:
|
||||
def __init__(self):
|
||||
self.dt = 0
|
||||
|
||||
try:
|
||||
with open("config.yaml","r") as config:
|
||||
with open("config.yml","r") as config:
|
||||
config = yaml.safe_load(config)
|
||||
except Exception as e:
|
||||
with open("rename_to_config.yaml","r") as config:
|
||||
with open("rename_to_config.yml","r") as config:
|
||||
config = yaml.safe_load(config)
|
||||
|
||||
self.mqtt = MQTTClient(config["mqtt"]["username"],
|
||||
|
|
@ -22,38 +23,66 @@ class Service:
|
|||
config["mqtt"]["host"],
|
||||
config["mqtt"]["port"])
|
||||
|
||||
self.mqtt.delegate = self
|
||||
|
||||
self.inputs = {}
|
||||
display_data = config['display']
|
||||
|
||||
print(display_data)
|
||||
|
||||
self.inputs[display_data['id']]['switches'] = []
|
||||
self.inputs = {
|
||||
display_data['id']: {
|
||||
"switches": []
|
||||
}
|
||||
}
|
||||
|
||||
for input_name, input_code in display_data['inputs'].items():
|
||||
self.create_display_switch(display_data['id'],input_name,input_code)
|
||||
|
||||
self.timer = Timer(30, self)
|
||||
self.update_inputs_states()
|
||||
self.timer = Timer(120, self)
|
||||
|
||||
def update_inputs_states(self):
|
||||
#check what input is on by getting the code from simpleddc
|
||||
#change the state of only that input
|
||||
pass
|
||||
input_code = simpleddc.show_input()
|
||||
|
||||
print(f"Input code is {input_code}")
|
||||
|
||||
for display_id in self.inputs.keys():
|
||||
for entry in self.inputs[display_id]["switches"]:
|
||||
if entry["code"] == input_code:
|
||||
print("Found {}".format(entry["topic"]))
|
||||
self.mqtt.client.publish(entry["topic"], "true")
|
||||
entry["state"] = True
|
||||
else:
|
||||
self.mqtt.client.publish(entry["topic"], "false")
|
||||
entry["state"] = False
|
||||
|
||||
def on_message(self, topic, payload):
|
||||
if payload =='OFF':
|
||||
return #do nothing
|
||||
|
||||
if "command" in topic:
|
||||
self.activate_input_deactivate_rest(self.inputs[topic.split("/")[2]],topic.split("/")[3])
|
||||
self.activate_input_deactivate_rest(int(topic.split("/")[2]),topic.split("/")[3])
|
||||
|
||||
def activate_input_deactivate_rest(self, display_id,display_input):
|
||||
print(f"Display {display_id} name {display_input}")
|
||||
|
||||
for entry in self.inputs[display_id]["switches"]:
|
||||
if entry["id"] == display_input:
|
||||
entry["state"] = True
|
||||
self.mqtt.client.publish(entry["topic"], "true")
|
||||
print(f'switching to {entry["code"]}')
|
||||
simpleddc.switch_to_input(display_id, int(entry["code"]))
|
||||
|
||||
else:
|
||||
entry["state"] = False
|
||||
self.mqtt.client.publish(entry["topic"], "false")
|
||||
|
||||
def activate_input_deactivate_rest(display_id,display_name):
|
||||
#check if this input is already on. if so, return
|
||||
#if not, switch display to this input, set rest of inputs to off
|
||||
pass
|
||||
|
||||
def on_timer(self, timer, elapsed):
|
||||
#check input states
|
||||
self.timer.reset()
|
||||
self.timer.active = True
|
||||
self.update_inputs_states()
|
||||
|
||||
def step(self, dt):
|
||||
self.timer.step(dt)
|
||||
|
|
@ -61,30 +90,31 @@ class Service:
|
|||
|
||||
def create_display_switch(self, display_id, input_name, input_code):
|
||||
topic = display_input_entity["generic_switch"]
|
||||
topic = topic.replace("#", display_id)
|
||||
topic = topic.replace("#", str(display_id))
|
||||
topic = topic.replace("?", input_name)
|
||||
|
||||
config = display_input_entity["generic_switch_config"].copy()
|
||||
config["unique_id"] = "{}_{}_switch".format(display_id,input_name)
|
||||
config["object_id"] = "{}_{}_switch".format(display_id,input_name)
|
||||
config["name"] = input_name
|
||||
config["state_topic"] = config["state_topic"].replace("#", id)
|
||||
config["state_topic"] = config["state_topic"].replace("#", str(display_id))
|
||||
config["state_topic"] = config["state_topic"].replace("?", input_name)
|
||||
|
||||
device = display_device.copy()
|
||||
device["name"] = device["name"].replace("#", input_name)
|
||||
device["model"] = "{}-{}".format(device["model"],id)
|
||||
device["model"] = "{}-{}".format(device["model"],display_id)
|
||||
config["device"] = device
|
||||
|
||||
config["command_topic"] = config["command_topic"].replace("#", id)
|
||||
config["command_topic"] = config["command_topic"].replace("#", str(display_id))
|
||||
config["command_topic"] = config["command_topic"].replace("?", input_name)
|
||||
|
||||
self.mqtt.client.publish(config["availability_topic"],"online", retain=True)
|
||||
self.mqtt.client.publish(config["state_topic"], "off")
|
||||
self.mqtt.client.publish(topic, json.dumps(config), retain=True)
|
||||
|
||||
self.mqtt.client.subscribe(config["command_topic"])
|
||||
|
||||
self.inputs[display_id]["switches"].append({"id": input_name, "topic": topic, "config": config, "code": input_code})
|
||||
self.inputs[display_id]["switches"].append({"id": input_name, "topic": config["state_topic"], "config": config, "code": input_code, "state": False})
|
||||
|
||||
def start(self):
|
||||
while True:
|
||||
|
|
@ -93,4 +123,5 @@ class Service:
|
|||
t1 = timer()
|
||||
self.dt = t1 - t0
|
||||
|
||||
|
||||
service = Service()
|
||||
service.start()
|
||||
10
docker-compose.yml
Normal file
10
docker-compose.yml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
version: '3.8'
|
||||
services:
|
||||
app:
|
||||
build: .
|
||||
volumes:
|
||||
- .:/usr/src/app
|
||||
ports:
|
||||
- "8000:8000" # Adjust the left side to the port you want to expose on the host
|
||||
environment:
|
||||
- PYTHONUNBUFFERED=1
|
||||
|
|
@ -6,6 +6,6 @@ module = Extension('simpleddc',
|
|||
library_dirs=['/usr/lib/aarch64-linux-gnu/libddcutil.so.4'],
|
||||
include_dirs=['/usr/include'])
|
||||
|
||||
setup(name='example',
|
||||
setup(name='simpleddc',
|
||||
version='1.0',
|
||||
ext_modules=[module])
|
||||
Loading…
Reference in a new issue