86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
from json import dumps
|
|
from os.path import join
|
|
|
|
class ha_mqtt_formatter():
|
|
def __init__(
|
|
self,
|
|
manufacturer : str,
|
|
model : str,
|
|
name : str,
|
|
identifiers : list,
|
|
availability_topic : list,
|
|
device_class : str
|
|
):
|
|
self.base = {
|
|
'device' : {
|
|
'manufacturer': manufacturer,
|
|
'model' : model,
|
|
'name' : name,
|
|
'identifiers' : identifiers,
|
|
},
|
|
'device_class' : device_class,
|
|
'availability' : [
|
|
{"topic" : x,
|
|
"value_template": "{{ value_json.state }}"
|
|
} for x in availability_topic
|
|
]
|
|
}
|
|
|
|
def binary_sensor(self, name, unique_id, payload_on, payload_off, state_topic, entity_category='diagnostic'):
|
|
update = self.base.copy()
|
|
update.update(
|
|
{
|
|
'name' : name,
|
|
'unique_id' : unique_id,
|
|
'payload_on' : payload_on,
|
|
'payload_off' : payload_off,
|
|
'state_topic' : state_topic,
|
|
'entity_category' : entity_category,
|
|
#"value_template": "{{ value_json.state }}"
|
|
}
|
|
)
|
|
return update
|
|
|
|
def switch(self, unique_id, name, payload_on, payload_off, state_topic, entity_category='config'):
|
|
update = self.base.copy()
|
|
update.update(
|
|
{
|
|
'name' : name,
|
|
'unique_id' : unique_id,
|
|
'payload_on' : payload_on,
|
|
'payload_off' : payload_off,
|
|
'state_topic' : state_topic,
|
|
"command_toppic" : join(self.base["base_topic", 'name', 'set']),
|
|
'entity_category' : entity_category,
|
|
# "value_template": "{{ value_json.state }}"
|
|
}
|
|
)
|
|
return update
|
|
|
|
def sensor(self, name, unique_id, state_class, state_topic, unit, entity_category='diagnostic'):
|
|
update = self.base.copy()
|
|
update.update(
|
|
{
|
|
'name' : name,
|
|
'unique_id' : unique_id,
|
|
'unit' : unit,
|
|
'state_topic' : state_topic,
|
|
'entity_category' : entity_category,
|
|
'state_class' : state_class,
|
|
}
|
|
)
|
|
return update
|
|
|
|
def number(self, unique_id, name, state_topic, unit, entity_category='config'):
|
|
update = self.base.copy()
|
|
update.update(
|
|
{
|
|
'name' : name,
|
|
'unique_id' : unique_id,
|
|
'unit' : unit,
|
|
'entity_category' : entity_category,
|
|
'state_topic' : state_topic,
|
|
# "value_template": f"{{ value_json.{name} }}"
|
|
}
|
|
)
|
|
return update |