Skip to content

Instantly share code, notes, and snippets.

@linuxkidd
Created September 30, 2020 01:32
Show Gist options
  • Save linuxkidd/994db5309dce848de4d72036c3ed3e9b to your computer and use it in GitHub Desktop.
Save linuxkidd/994db5309dce848de4d72036c3ed3e9b to your computer and use it in GitHub Desktop.
cat features.json | ./populate_coachproxy.py
#!/usr/bin/python3
import array,json,paho.mqtt.client as mqtt,re,sys,time
mqttc=mqtt.Client("cpimporter")
mqttc.connect("172.16.22.2", port=1883)
debugout=0
cleartopic=0
publishtopic=1
obj=json.load(sys.stdin)
mybase=""
groups=obj['Comments']['Light and Shade Groups']
device={ "identifiers":"CoachProxy", "name":"CoachProxy", "manufacturer":"CoachProxy", "model":"3.0", "sw_version":"3.1.3", "connections": [["mac", "b8:27:eb:96:a6:71"]] }
#device={ "identifiers":"CoachProxy", "name":"CoachProxy", "manufacturer":"CoachProxy", "model":"3.0", "sw_version":"3.1.3", "via_device":"coachproxy" }
dimbtemplate={ 'name':'', 'state_topic': 'RVC/DC_DIMMER_STATUS_3/', 'command_topic': 'CPCOMMAND/DC_DIMMER_COMMAND_2/', 'brightness_state_topic': 'RVC/DC_DIMMER_STATUS_3/', 'brightness_command_topic': 'CPCOMMAND/DC_DIMMER_COMMAND_2/', 'brightness_scale': 100, 'on_command_type': 'brightness', 'payload_on':100, 'payload_off': 0, 'state_value_template':'{% if value!="0" %} 100 {% else %} 0 {% endif %}', 'unique_id':'cpdcdimmer' }
dimtemplate={ 'name':'', 'state_topic': 'RVC/DC_DIMMER_STATUS_3/', 'command_topic': 'CPCOMMAND/DC_DIMMER_COMMAND_2/', 'payload_on':100, 'payload_off':0, 'unique_id':'cpdcdimmer' }
panels={
"126":"All",
"129":"Entry",
"130":"Driver",
"131":"Passenger",
"132":"Kitchen",
"136":"Mid Bath",
"138":"Hall",
"139":"Bedroom",
"140":"Rear Bath"
}
paneltemplate={ 'name':'', 'state_topic': 'CP/PANEL/', 'command_topic': 'CPCOMMAND/PANEL/', 'brightness_state_topic': 'CP/PANEL/', 'brightness_command_topic': 'CPCOMMAND/PANEL/', 'brightness_scale': 100, 'on_command_type': 'brightness', 'payload_on':100, 'payload_off': 0, 'state_value_template':'{% if value!="0" %} 100 {% else %} 0 {% endif %}', 'unique_id':'cppanel' }
others=[
# Ceiling Fan
{ "type":"fan", "name":"bedroom", "definition":{"name":"Bedroom Fan",
"state_topic":"CP/ceiling_fan", "command_topic":"CPCOMMAND/ceiling_fan",
"speeds":["off","low","high"], "speed_state_topic":"CP/ceiling_fan", "speed_command_topic":"CPCOMMAND/ceiling_fan",
"payload_on":"on","payload_off":"off",
"state_value_template":"{% if value==\"off\" %} off {% else %} on {% endif %}"}},
# Heated Floor
{ "type":"climate","name":"floorheatfront", "definition": {"name":"Front Floor","modes":["off","heat"], "mode_state_topic": "CP/FLOORPOWER/FRONT", "mode_command_topic":"CPCOMMAND/FLOORPOWER/FRONT","temperature_command_topic":"CPCOMMAND/FLOORSETPOINT/FRONT","temperature_state_topic":"CP/FLOORSETPOINT/FRONT", "current_temperature_topic":"CP/FLOORTEMP/FRONT", "send_if_off":"false", "max_temp":104.0, "min_temp":32.0, "temp_step":1, "precision":0.1}},
{ "type":"climate","name":"floorheatrear", "definition": {"name":"Rear Floor","modes":["off","heat"], "mode_state_topic": "CP/FLOORPOWER/REAR", "mode_command_topic":"CPCOMMAND/FLOORPOWER/REAR","temperature_command_topic":"CPCOMMAND/FLOORSETPOINT/REAR","temperature_state_topic":"CP/FLOORSETPOINT/REAR", "current_temperature_topic":"CP/FLOORTEMP/REAR", "send_if_off":"false", "max_temp":104.0, "min_temp":32.0, "temp_step":1, "precision":0.1}},
# Shades
{ "type":"cover", "name":"dayshade3", "definition": { "name":"Driver Day Shade", "command_topic": "CPCOMMAND/SHADE/day/3", "state_topic": "CP/DAYSHADE/3", "device_class":"shade", "payload_open": "up", "payload_close":"down", "state_open":"0", "payload_stop":"", "state_closed":"1", "optimistic":"true"}},
{ "type":"cover", "name":"nightshade3", "definition": { "name":"Driver Night Shade", "command_topic": "CPCOMMAND/SHADE/night/3", "state_topic": "CP/NIGHTSHADE/3", "device_class":"shade", "payload_open": "up", "payload_close":"down", "state_open":"0", "payload_stop":"", "state_closed":"1", "optimistic":"true"}},
{ "type":"cover", "name":"dayshade7", "definition": { "name":"Passenger Day Shade", "command_topic": "CPCOMMAND/SHADE/day/7", "state_topic": "CP/DAYSHADE/7", "device_class":"shade", "payload_open": "up", "payload_close":"down", "state_open":"0", "payload_stop":"", "state_closed":"1", "optimistic":"true"}},
{ "type":"cover", "name":"nightshade7", "definition": { "name":"Passenger Night Shade", "command_topic": "CPCOMMAND/SHADE/night/7", "state_topic": "CP/NIGHTSHADE/7", "device_class":"shade", "payload_open": "up", "payload_close":"down", "state_open":"0", "payload_stop":"", "state_closed":"1", "optimistic":"true"}},
{ "type":"cover", "name":"dayshade8", "definition": {"name":"Dining Day Shade", "command_topic": "CPCOMMAND/SHADE/day/8", "state_topic": "CP/DAYSHADE/8", "device_class":"shade", "payload_open": "up", "payload_close":"down", "state_open":"0", "payload_stop":"", "state_closed":"1", "optimistic":"true"}},
{ "type":"cover", "name":"nightshade8", "definition": {"name":"Dining Night Shade", "command_topic": "CPCOMMAND/SHADE/night/8", "state_topic": "CP/NIGHTSHADE/8", "device_class":"shade", "payload_open": "up", "payload_close":"down", "state_open":"0", "payload_stop":"", "state_closed":"1", "optimistic":"true"}},
{ "type":"cover", "name":"nightshade4", "definition": { "name":"Door Shade", "command_topic": "CPCOMMAND/SHADE/night/4", "state_topic": "CP/NIGHTSHADE/4", "device_class":"shade", "payload_open": "up", "payload_close":"down", "state_open":"0", "payload_stop":"", "state_closed":"1", "optimistic":"true"}},
# Locks
{ "type":"lock", "name":"all", "definition": {"name":"All Locks","state_topic":"RVC/LOCK_COMMAND/0","command_topic":"CPCOMMAND/LOCK/0","payload_lock":"1","payload_unlock":"0","optimistic":"true"}},
{ "type":"lock", "name":"door", "definition": {"name":"Door Lock","state_topic":"RVC/LOCK_COMMAND/1","command_topic":"CPCOMMAND/LOCK/1","payload_lock":"1","payload_unlock":"0","optimistic":"true"}},
{ "type":"lock", "name":"cargo", "definition": {"name":"Cargo Locks","state_topic":"RVC/LOCK_COMMAND/6","command_topic":"CPCOMMAND/LOCK/6","payload_lock":"1","payload_unlock":"0","optimistic":"true"}},
# Vent Lids
{ "type":"cover", "name":"ventlidgalley", "definition": { "name":"Kitchen Vent", "command_topic": "CPCOMMAND/VENT/0", "state_topic": "CP/VENT/0", "device_class":"damper", "payload_open": "open", "payload_close":"closed", "state_open":"open", "payload_stop":"", "state_closed":"closed", "optimistic":"false"}},
{ "type":"cover", "name":"ventlidmidbath", "definition": { "name":"Mid Bath Vent", "command_topic": "CPCOMMAND/VENT/1", "state_topic": "CP/VENT/1", "device_class":"damper", "payload_open": "open", "payload_close":"closed", "state_open":"open", "payload_stop":"", "state_closed":"closed", "optimistic":"false"}},
{ "type":"cover", "name":"ventlidrearbath", "definition": { "name":"Rear Bath Vent", "command_topic": "CPCOMMAND/VENT/2", "state_topic": "CP/VENT/2", "device_class":"damper", "payload_open": "open", "payload_close":"closed", "state_open":"open", "payload_stop":"", "state_closed":"closed", "optimistic":"false"}},
# Vent Fans
{ "type":"fan", "name":"kitchen", "definition":{"name":"Kitchen Fan","state_topic":"RVC/DC_DIMMER_STATUS_3/25", "command_topic":"CPCOMMAND/DC_DIMMER_COMMAND_2/25", "speeds":["off","on"], "payload_on":"on","payload_off":"off"}},
{ "type":"fan", "name":"midbath", "definition":{"name":"Mid Bath Fan","state_topic":"RVC/DC_DIMMER_STATUS_3/29", "command_topic":"CPCOMMAND/DC_DIMMER_COMMAND_2/29", "speeds":["off","on"], "payload_on":"on","payload_off":"off"}},
{ "type":"fan", "name":"rearbath", "definition":{"name":"Rear Bath Fan","state_topic":"RVC/DC_DIMMER_STATUS_3/32", "command_topic":"CPCOMMAND/DC_DIMMER_COMMAND_2/32", "speeds":["off","on"], "payload_on":"on","payload_off":"off"}},
# Tanks
{ "type":"sensor", "name":"freshtank", "definition":{"name":"Fresh Tank", "device_class":"humidity", "icon":"mdi:car-coolant-level", "state_topic":"CP/TANK_STATUS/Fresh", "unit_of_measurement":"%"}},
{ "type":"sensor", "name":"greytank", "definition":{"name":"Grey Tank", "device_class":"humidity", "icon":"mdi:car-coolant-level", "state_topic":"CP/TANK_STATUS/Grey", "unit_of_measurement":"%"}},
{ "type":"sensor", "name":"blacktank", "definition":{"name":"Black Tank", "device_class":"humidity", "icon":"mdi:car-coolant-level", "state_topic":"CP/TANK_STATUS/Black", "unit_of_measurement":"%"}},
# Ignition
{ "type":"binary_sensor", "name":"ignition", "definition":{"name":"Ignition", "device_class": "moving", "state_topic":"CP/IGNITION", "payload_on":"on", "payload_off":"off"}},
# Generator
{ "type":"binary_sensor", "name":"generator", "definition":{"name":"Generator", "device_class": "moving", "state_topic":"CP/GENERATOR", "payload_on":"running", "payload_off":"stopped"}},
# Batteries
{ "type":"sensor", "name":"housebattery", "definition":{"name":"House Battery", "icon":"mdi:battery", "state_topic":"RVC/DC_SOURCE_STATUS_1/1", "unit_of_measurement":"v"}},
{ "type":"sensor", "name":"chassisbattery", "definition":{"name":"Chassis Battery", "icon":"mdi:battery", "state_topic":"RVC/DC_SOURCE_STATUS_1/2", "unit_of_measurement":"v"}}
]
switches={
"93":{"name":"Water Pump","icon":"mdi:water-pump"},
"23":{"name":"Electric Aquahot","icon":"mdi:water-boiler"},
"24":{"name":"Diesel Aquahot","icon":"mdi:water-boiler-alert"},
"92":{"name":"Engine Preheat","icon":"mdi:engine"}
}
switchtemplate={ 'name':'', 'state_topic': 'RVC/DC_DIMMER_STATUS_3/', 'command_topic': 'CPCOMMAND/DC_DIMMER_COMMAND_2/', 'payload_on':100, 'payload_off':0, 'unique_id':'cpdcdimmer', 'icon':"" }
for lightid in obj['2016']['Allegro_Bus']['Default']['Lights']:
light=obj['2016']['Allegro_Bus']['Default']['Lights'][lightid]
del mybase
if int(lightid)<13:
mybase=dimbtemplate
else:
mybase=dimtemplate
mybase['device']=device
mybase['unique_id']=re.sub(r'[0-9]*$','',mybase['unique_id'])+lightid
if groups[light['location']]!="":
mybase['name']=" ".join([groups[light['location']],light['name']])
else:
mybase['name']=light['name']
for field in mybase:
if re.search(r'_topic$',field):
mybase[field]=re.sub(r'[0-9]*$','',mybase[field])+lightid
print("homeassistant/light/coachproxy/{0:s}/config".format(lightid),end=" ",flush=True)
if not debugout:
print()
else:
print(json.dumps(mybase))
if cleartopic:
mqttc.publish("homeassistant/light/coachproxy/{0:s}/config".format(lightid),'')
mqttc.publish("homeassistant/light/coachproxy/{0:s}/config".format(lightid),'',retain=True)
if publishtopic:
time.sleep(0.5)
if publishtopic:
mqttc.publish("homeassistant/light/coachproxy/{0:s}/config".format(lightid),json.dumps(mybase),retain=True)
time.sleep(0.1)
for panelid in panels:
del mybase
mybase=paneltemplate
mybase['device']=device
mybase['unique_id']=re.sub(r'[0-9]*$','',mybase['unique_id'])+panelid
mybase['name']="{0:s} Panel".format(panels[panelid])
if panels[panelid]=="All":
mybase['name']="All Panels"
for field in mybase:
if re.search(r'_topic$',field):
mybase[field]=re.sub(r'[0-9]*$','',mybase[field])+panelid
print("homeassistant/light/coachproxy/panel{0:s}/config".format(panelid),end=" ",flush=True)
if not debugout:
print()
else:
print(json.dumps(mybase))
if cleartopic:
mqttc.publish("homeassistant/light/coachproxy/panel{0:s}/config".format(panelid),'')
mqttc.publish("homeassistant/light/coachproxy/panel{0:s}/config".format(panelid),'',retain=True)
if publishtopic:
time.sleep(0.5)
if publishtopic:
mqttc.publish("homeassistant/light/coachproxy/panel{0:s}/config".format(panelid),json.dumps(mybase),retain=True)
time.sleep(0.1)
for switchid in switches:
del mybase
mybase=switchtemplate
mybase['device']=device
mybase['unique_id']=re.sub(r'[0-9]*$','',mybase['unique_id'])+switchid
mybase['name']=switches[switchid]["name"]
mybase['icon']=switches[switchid]["icon"]
for field in mybase:
if re.search(r'_topic$',field):
mybase[field]=re.sub(r'[0-9]*$','',mybase[field])+switchid
print("homeassistant/switch/coachproxy/switch{0:s}/config".format(switchid),end=" ",flush=True)
if not debugout:
print()
else:
print(json.dumps(mybase))
if cleartopic:
mqttc.publish("homeassistant/switch/coachproxy/switch{0:s}/config".format(switchid),'')
mqttc.publish("homeassistant/switch/coachproxy/switch{0:s}/config".format(switchid),'',retain=True)
if publishtopic:
time.sleep(0.5)
if publishtopic:
mqttc.publish("homeassistant/switch/coachproxy/switch{0:s}/config".format(switchid),json.dumps(mybase),retain=True)
time.sleep(0.1)
for other in others:
del mybase
mybase=other['definition']
mybase['device']=device
print("homeassistant/{0:s}/coachproxy/{1:s}/config".format(other['type'],other['name']),end=" ",flush=True)
if not debugout:
print()
else:
print(json.dumps(other['definition']))
if cleartopic:
mqttc.publish("homeassistant/{0:s}/coachproxy/{1:s}/config".format(other['type'],other['name']), '')
mqttc.publish("homeassistant/{0:s}/coachproxy/{1:s}/config".format(other['type'],other['name']), '',retain=True)
if publishtopic:
time.sleep(0.5)
if publishtopic:
mqttc.publish("homeassistant/{0:s}/coachproxy/{1:s}/config".format(other['type'],other['name']), json.dumps(other['definition']),retain=True)
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment