Hi Rob,
I have a proposal that I’ve cobbled together and I could do with some advice. I’m proposing to use the jinja2 python library to create an xml file. jinja2 is available on pypi so we should be able to use it in HA.
I would not need to know the format of an RSS file in my proposal. The user (you) could create a template file which jinja2 fills in for you.
So what happens. The user (you) creates an jinja2 template file such as this:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<author>Author's name</author>
<title>Feed title</title>
{%for entry in entries %}
<entry>
<title>Partition {{entry.partition}}</title>
<link href="{{entry.current}}"/>
<content type="html">{{entry.date}}</content>
</entry>
{%endfor%}
</feed>
This is the content of feedtemplate.xml
in the example code below.
jinja2 has variables, for example {{entry.partition}}
so I provide the data for entries
and each entry has the alarm panel attributes: current, total, partition, date, time, zone and event
My test code looks like this:
import jinja2
from jinja2 import Environment, FileSystemLoader
alarmstate = [
{ "partition":"1", "current":"1", "total":"250", "date":"10/01/2020", "time":"13:00", "zone":"Fob 01", "event":"Disarm"},
{ "partition":"1", "current":"2", "total":"250", "date":"10/01/2020", "time":"07:43", "zone":"Fob 02", "event":"Arm Away"},
{ "partition":"1", "current":"3", "total":"250", "date":"09/01/2020", "time":"20:21", "zone":"User 01", "event":"Disarm"}
]
file_loader = FileSystemLoader('.')
env = Environment(loader=file_loader)
template = env.get_template('feedtemplate.xml')
output = template.render(entries=alarmstate)
print(output)
When I execute the python code it produces this as its output:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<author>Author's name</author>
<title>Feed title</title>
<entry>
<title>Partition 1</title>
<link href="1"/>
<content type="html">10/01/2020</content>
</entry>
<entry>
<title>Partition 1</title>
<link href="2"/>
<content type="html">10/01/2020</content>
</entry>
<entry>
<title>Partition 1</title>
<link href="3"/>
<content type="html">09/01/2020</content>
</entry>
</feed>
I know that the output content
isn’t correct html but I’m just showing you an example of how it could be achieved.
In this way I don’t need to know the structure of the xml output file, that structure is in the template that you create.
I hope this makes sense