first version discovery message
This commit is contained in:
Vendored
+5
-1
@@ -56,7 +56,11 @@
|
||||
"LOG_CLI_LVL": "DEBUG",
|
||||
"PROJECTOR_IP":"192.168.31.10",
|
||||
"PROJECTOR_USER":"alberto",
|
||||
"PROJECTOR_PASSWORD":"alberto"
|
||||
"PROJECTOR_PASSWORD":"alberto",
|
||||
"MQTT_USER":"projmon",
|
||||
"MQTT_PASSWORD":"projmon",
|
||||
"MQTT_HOST":"192.168.31.26",
|
||||
"MQTT_PORT":"1883"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
|
||||
HA_CONFIG_TEMPLATE = {
|
||||
"name": "{device_friendly_name}",
|
||||
"state_topic": "homeassistant/{sensor_type}/{device_name}/{device_state}/state",
|
||||
"state_topic": "{device_name}/state",
|
||||
"unique_id": "{device_name}_{device_state}",
|
||||
"icon": "{device_icon}",
|
||||
"device": {
|
||||
|
||||
+81
-29
@@ -2,14 +2,15 @@ import os
|
||||
import sys
|
||||
import time
|
||||
import logging
|
||||
import slugify
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
import projrequest as projrequest
|
||||
from commands import SMSMessage, ShowStatus, PowerStatus, DCPImportJobList, DCPInfoList, StorageInfo, SPLList
|
||||
from utils import *
|
||||
from haconfig import HA_CONFIG_TEMPLATE
|
||||
from copy import copy
|
||||
from influxdb_client_3 import InfluxDBClient3, Point
|
||||
from paho.mqtt import client as mqtt_client
|
||||
from paho.mqtt.enums import CallbackAPIVersion
|
||||
|
||||
from typing import TypeVar, Type, List
|
||||
T = TypeVar("T", bound=SMSMessage)
|
||||
@@ -18,31 +19,43 @@ T = TypeVar("T", bound=SMSMessage)
|
||||
env = dict(os.environ)
|
||||
LOGGER: logging.Logger
|
||||
|
||||
DISCOVERY_PREFIX = "homeassistant"
|
||||
|
||||
class HAentity():
|
||||
conf: dict = copy(HA_CONFIG_TEMPLATE)
|
||||
device_name: str
|
||||
device_friendly_name: str
|
||||
class HAdevice(BaseModel):
|
||||
identifiers: List[str]
|
||||
name: str
|
||||
manufacturer: str
|
||||
model: str
|
||||
short_name: str
|
||||
|
||||
def __init__(self, device_name: str, friendly_name: str, icon: str) -> None:
|
||||
self.conf['name'] = self.conf['name'].format(device_friendly_name=friendly_name)
|
||||
self.conf['device']['name'] = self.conf['name']
|
||||
self.conf['icon'] = self.conf['icon'].format(device_icon=icon)
|
||||
self.device_name = device_name
|
||||
self.device_friendly_name = friendly_name
|
||||
class HAsensor(BaseModel):
|
||||
name: str
|
||||
unique_id: str
|
||||
state_topic: str
|
||||
value_template: str | None = None
|
||||
unit_of_measurement: str | None = None
|
||||
icon: str | None = None
|
||||
device: HAdevice
|
||||
|
||||
class HAShowStatusEntities(HAentity):
|
||||
def __init__(self, device_name: str, friendly_name: str, icon: str) -> None:
|
||||
super().__init__(device_name, friendly_name, icon)
|
||||
# {
|
||||
# "name": "Lamp Hours",
|
||||
# "unique_id": "sony_projector_lamp_hours",
|
||||
|
||||
def build_config(self, tt: Type[T]) -> List[dict]:
|
||||
cfg: List[dict] = [{}]
|
||||
for f in [tt]:
|
||||
this_entity = copy(self.conf)
|
||||
this_entity['state_topic'] = this_entity['state_topic'].format(device_state = slugify.slugify(str(f)), sensor_type = "sensor", device_name=self.device_name)
|
||||
this_entity['unique_id'] = this_entity['unique_id'].format(device_name = self.device_name, device_state = slugify.slugify(str(f)))
|
||||
cfg.append(self.conf)
|
||||
return cfg
|
||||
# "state_topic": STATE_TOPIC,
|
||||
# "value_template": "{{ value_json.lamp_hours }}",
|
||||
|
||||
# "unit_of_measurement": "h",
|
||||
# "icon": "mdi:lightbulb",
|
||||
|
||||
# "device": {
|
||||
# "identifiers": [
|
||||
# "sony_dcinema_projector"
|
||||
# ],
|
||||
# "name": "Sony Digital Cinema Projector",
|
||||
# "manufacturer": "Sony",
|
||||
# "model": "Digital Cinema Projector"
|
||||
# }
|
||||
# }
|
||||
|
||||
##################
|
||||
###### MAIN ######
|
||||
@@ -54,6 +67,9 @@ def main() -> int:
|
||||
write_client = InfluxDBClient3(host=env['INFLUXDB_URL'],
|
||||
token=env['INFLUXDB_TOKEN'],
|
||||
database=env['INFLUXDB_DATABASE'])
|
||||
mqtt = mqtt_client.Client(CallbackAPIVersion.VERSION2)
|
||||
mqtt.username_pw_set(username=env['MQTT_USER'], password=env['MQTT_PASSWORD'])
|
||||
mqtt.connect(host=env['MQTT_HOST'], port=int(env['MQTT_PORT']))
|
||||
except Exception as e:
|
||||
LOGGER.error(e)
|
||||
return 1
|
||||
@@ -71,14 +87,50 @@ def main() -> int:
|
||||
password=env['PROJECTOR_PASSWORD']
|
||||
)
|
||||
|
||||
sony_device: HAdevice = HAdevice (
|
||||
identifiers=["sony_digitalcinema_projector"],
|
||||
name="Sony Digital Cinema Projector",
|
||||
manufacturer="Sony",
|
||||
model="SRX-R510P",
|
||||
short_name="sony_projector"
|
||||
)
|
||||
|
||||
storage_free_sensor: HAsensor = HAsensor (
|
||||
name="Free DCP Storage",
|
||||
unique_id=f"{sony_device.short_name}_free_storage",
|
||||
state_topic=f"{sony_device.short_name}/storage_info",
|
||||
value_template="{{ FreeSpace }}",
|
||||
icon="mdi:harddisk",
|
||||
unit_of_measurement="B",
|
||||
device=sony_device
|
||||
)
|
||||
|
||||
storage_used_sensor: HAsensor = HAsensor (
|
||||
name="Used DCP Storage",
|
||||
unique_id=f"{sony_device.short_name}_used_storage",
|
||||
state_topic=f"{sony_device.short_name}/storage_info",
|
||||
value_template="{{ SpaceInUse }}",
|
||||
icon="mdi:harddisk",
|
||||
unit_of_measurement="B",
|
||||
device=sony_device
|
||||
)
|
||||
|
||||
mqtt.publish(topic=f"{DISCOVERY_PREFIX}/sensor/{storage_free_sensor.unique_id}/config",
|
||||
payload=storage_free_sensor.model_dump_json(indent=2),
|
||||
qos=1,
|
||||
retain=True)
|
||||
|
||||
mqtt.publish(topic=f"{DISCOVERY_PREFIX}/sensor/{storage_used_sensor.unique_id}/config",
|
||||
payload=storage_used_sensor.model_dump_json(indent=2),
|
||||
qos=1,
|
||||
retain=True)
|
||||
|
||||
while handler.running:
|
||||
try:
|
||||
now:float = time.time()
|
||||
ret = projector.request(SPLList)
|
||||
print(ret)
|
||||
# cfg = HAShowStatusEntities("sony_dcp", "SonyDigitalCinema", "mdi:alarm")
|
||||
# if ret:
|
||||
# cfg.build_config(ret)
|
||||
ret = projector.request(StorageInfo)
|
||||
if ret:
|
||||
LOGGER.info(ret.model_dump_json(indent=2))
|
||||
last: float = time.time()
|
||||
cycle_time: float = last - now
|
||||
LOGGER.debug(f"Cycle Time: {cycle_time:4.3f}")
|
||||
|
||||
@@ -57,6 +57,6 @@ class ProjectorConnection():
|
||||
rv.update(**rv['MessageBody'][type])
|
||||
del rv['MessageBody']
|
||||
del rv['@xmlns']
|
||||
print(json.dumps(rv, indent = 2))
|
||||
#print(json.dumps(rv, indent = 2))
|
||||
return cls.model_validate(rv)
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user