test sensors working
This commit is contained in:
Vendored
+1
-1
@@ -47,7 +47,7 @@
|
||||
"cwd": "${workspaceFolder}",
|
||||
"console": "integratedTerminal",
|
||||
"env": {
|
||||
"INTERVAL" : "5",
|
||||
"INTERVAL" : "15",
|
||||
"INFLUXDB_TOKEN":"apiv3_tbEpA8JmIRTfr8Wbw9npD79BcMlFsnV4_jhdt-CdUP53Mos61KBODGaggl2g5oKZZvZrZu3e6mpob6zorhEdbg",
|
||||
"INFLUXDB_URL" : "http://edelweiss-srv:8181",
|
||||
"INFLUXDB_DATABASE" : "edelweiss30d",
|
||||
|
||||
+2
-1
@@ -55,6 +55,7 @@ class ShowStatusDetailWrapper(BaseModel):
|
||||
RemainingTime: int
|
||||
ElapsedTime: int
|
||||
TotalDuration: int
|
||||
CurrentEventTitle: Optional[str] = None
|
||||
CurrentEventId: UUID
|
||||
CurrentEventType: str
|
||||
IsStoppedByMalfunction: bool
|
||||
@@ -140,7 +141,7 @@ class SPLLanguage(BaseModel):
|
||||
Subtitle: str | None
|
||||
|
||||
class SPLMetadata(BaseModel):
|
||||
Id: UUID = Field(alias='UUID')
|
||||
Id: UUID = Field(alias='UUID')
|
||||
Title: str
|
||||
Type: str | None
|
||||
Duration: time
|
||||
|
||||
+115
-61
@@ -1,18 +1,17 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import logging
|
||||
from typing import Any, List, Optional, Type, TypeVar
|
||||
|
||||
from pydantic import BaseModel
|
||||
import projrequest
|
||||
from commands import ShowStatus, SMSMessage, SPLList, StorageInfo
|
||||
|
||||
import projrequest as projrequest
|
||||
from commands import SMSMessage, ShowStatus, PowerStatus, DCPImportJobList, DCPInfoList, StorageInfo, SPLList
|
||||
from utils import *
|
||||
from influxdb_client_3 import InfluxDBClient3, Point
|
||||
from paho.mqtt import client as mqtt_client
|
||||
from paho.mqtt.enums import CallbackAPIVersion
|
||||
from pydantic import BaseModel, Field
|
||||
from utils import SignalHandler
|
||||
|
||||
from typing import TypeVar, Type, List
|
||||
T = TypeVar("T", bound=SMSMessage)
|
||||
|
||||
# Get environment variables
|
||||
@@ -26,36 +25,19 @@ class HAdevice(BaseModel):
|
||||
name: str
|
||||
manufacturer: str
|
||||
model: str
|
||||
short_name: str
|
||||
short_name: str = Field(exclude=True)
|
||||
|
||||
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
|
||||
value_template: Optional[str] = None
|
||||
unit_of_measurement: Optional[str] = None
|
||||
device_class: Optional[str] = None
|
||||
state_class: Optional[str] = None
|
||||
icon: Optional[str] = None
|
||||
device: HAdevice
|
||||
|
||||
# {
|
||||
# "name": "Lamp Hours",
|
||||
# "unique_id": "sony_projector_lamp_hours",
|
||||
|
||||
# "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"
|
||||
# }
|
||||
# }
|
||||
request: Type = Field(exclude=True)
|
||||
|
||||
##################
|
||||
###### MAIN ######
|
||||
@@ -63,18 +45,15 @@ class HAsensor(BaseModel):
|
||||
def main() -> int:
|
||||
INTERVAL = int(env['INTERVAL'])
|
||||
try:
|
||||
# Init InfluxDB-v3 Client
|
||||
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']))
|
||||
mqtt.loop_start()
|
||||
except Exception as e:
|
||||
LOGGER.error(e)
|
||||
return 1
|
||||
finally:
|
||||
LOGGER.info(f"InfluxDB Connected: [{env['INFLUXDB_URL']}/{env['INFLUXDB_DATABASE']}]")
|
||||
LOGGER.info(f"MQTT Connected: [{env['MQTT_HOST']}:{env['MQTT_PORT']}]")
|
||||
|
||||
##############################
|
||||
########## MAIN LOOP #########
|
||||
@@ -95,53 +74,128 @@ def main() -> int:
|
||||
short_name="sony_projector"
|
||||
)
|
||||
|
||||
storage_free_sensor: HAsensor = HAsensor (
|
||||
sensors: List[HAsensor] = []
|
||||
|
||||
sensors.append(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 }}",
|
||||
state_topic=f"({sony_device.short_name})/storage_info",
|
||||
value_template="{{ value_json.FreeSpace }}",
|
||||
device_class="data_size",
|
||||
state_class="measurement",
|
||||
icon="mdi:harddisk",
|
||||
unit_of_measurement="B",
|
||||
device=sony_device
|
||||
)
|
||||
device=sony_device,
|
||||
request=StorageInfo
|
||||
))
|
||||
|
||||
storage_used_sensor: HAsensor = HAsensor (
|
||||
sensors.append(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 }}",
|
||||
state_topic=f"({sony_device.short_name})/storage_info",
|
||||
value_template="{{ value_json.SpaceInUse }}",
|
||||
device_class="data_size",
|
||||
state_class="measurement",
|
||||
icon="mdi:harddisk",
|
||||
unit_of_measurement="B",
|
||||
device=sony_device
|
||||
)
|
||||
device=sony_device,
|
||||
request=StorageInfo
|
||||
))
|
||||
|
||||
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)
|
||||
sensors.append(HAsensor (
|
||||
name="Play State",
|
||||
unique_id=f"{sony_device.short_name}_play_state",
|
||||
state_topic=f"({sony_device.short_name})/show_info",
|
||||
value_template="{{ value_json.PlayState }}",
|
||||
icon="mdi:play-pause",
|
||||
device=sony_device,
|
||||
request=ShowStatus
|
||||
))
|
||||
|
||||
sensors.append(HAsensor (
|
||||
name="Playback Mode",
|
||||
unique_id=f"{sony_device.short_name}_playback_mode",
|
||||
state_topic=f"({sony_device.short_name})/show_info",
|
||||
value_template="{{ value_json.PlayBackMode }}",
|
||||
icon="mdi:play-circle",
|
||||
device=sony_device,
|
||||
request=ShowStatus
|
||||
))
|
||||
|
||||
sensors.append(HAsensor (
|
||||
name="Total Duration",
|
||||
unique_id=f"{sony_device.short_name}_total_duration",
|
||||
state_topic=f"({sony_device.short_name})/show_info",
|
||||
value_template="{{ value_json.ShowStatusDetail.TotalDuration }}",
|
||||
unit_of_measurement="s",
|
||||
icon="mdi:timer",
|
||||
device=sony_device,
|
||||
request=ShowStatus
|
||||
))
|
||||
|
||||
sensors.append(HAsensor (
|
||||
name="Elapsed Time",
|
||||
unique_id=f"{sony_device.short_name}_elapsed_time",
|
||||
state_topic=f"({sony_device.short_name})/show_info",
|
||||
value_template="{{ value_json.ShowStatusDetail.ElapsedTime }}",
|
||||
unit_of_measurement="s",
|
||||
icon="mdi:timer",
|
||||
device=sony_device,
|
||||
request=ShowStatus
|
||||
))
|
||||
|
||||
sensors.append(HAsensor (
|
||||
name="Remaining Time",
|
||||
unique_id=f"{sony_device.short_name}_remaining_time",
|
||||
state_topic=f"({sony_device.short_name})/show_info",
|
||||
value_template="{{ value_json.ShowStatusDetail.RemainingTime }}",
|
||||
unit_of_measurement="s",
|
||||
icon="mdi:timer",
|
||||
device=sony_device,
|
||||
request=ShowStatus
|
||||
))
|
||||
|
||||
sensors.append(HAsensor (
|
||||
name="Play Title",
|
||||
unique_id=f"{sony_device.short_name}_play_title",
|
||||
state_topic=f"({sony_device.short_name})/play_title",
|
||||
value_template="{{ value_json.PlayTitle }}",
|
||||
icon="mdi:movie",
|
||||
device=sony_device,
|
||||
request=ShowStatus
|
||||
))
|
||||
|
||||
for s in sensors:
|
||||
mqtt.publish(topic=f"{DISCOVERY_PREFIX}/sensor/{s.unique_id}/config",
|
||||
payload=s.model_dump_json(indent=2),
|
||||
retain=True)
|
||||
|
||||
while handler.running:
|
||||
try:
|
||||
now:float = time.time()
|
||||
ret = projector.request(StorageInfo)
|
||||
if ret:
|
||||
LOGGER.info(ret.model_dump_json(indent=2))
|
||||
tt: Any = None
|
||||
ret: None | SMSMessage = None
|
||||
spls: SPLList | None = projector.request(SPLList)
|
||||
for s in sensors:
|
||||
if tt != s.request:
|
||||
tt = s.request
|
||||
ret = projector.request(s.request)
|
||||
if ret:
|
||||
mqtt.publish(topic=s.state_topic,
|
||||
payload=ret.model_dump_json(
|
||||
indent=2,
|
||||
exclude={"MessageHeader"},exclude_unset=True),
|
||||
retain=True)
|
||||
|
||||
last: float = time.time()
|
||||
cycle_time: float = last - now
|
||||
LOGGER.debug(f"Cycle Time: {cycle_time:4.3f}")
|
||||
if cycle_time < INTERVAL:
|
||||
time.sleep(INTERVAL-cycle_time)
|
||||
break
|
||||
except Exception as e:
|
||||
print(f"Unexpected exception: [{e}]")
|
||||
return 1
|
||||
pass
|
||||
|
||||
mqtt.disconnect()
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user