SL public transport information (Sweden)

I’m in the process of setting up my home assistant and something that would tremendously good for me would be able to se when my metro leaves.

So something similar as Västtrafik Public Transport but for SL.
There are API’s at trafiklab.se, in swedish thou, so it should be doable but me myself is not good enough at programming yet.

There seems to be a command-line tool. Could be used perhaps.

I have been working on a way to query trafikverket api that should include every train in Sweden…

As this is the first time im writing in python the progress is slow, i can successfully query and get information but i need to work some more on it before i can release it… (its a horrible spaghetti mess!)

After some muddling around i guess its in a good enough state, https://github.com/AnderssonPeter/pytrafikverket
If someone who knows python could look at it and check if i did anything totally stupid, as i said i have never written any python code before.
Once it has been reviewed it would be nice if it where converted into a component!

1 Like

Great work! My python is not up to the task unfortunately but hopefully it will be turned into a component!

Added a python package https://pypi.python.org/pypi?name=pytrafikverket&:action=display

Removed better solution can be found further down!

1 Like

Thanks for all your work @AnderssonPeter!
I’m trying to install pytrafikverket but gettting some errors.

Here is the output in my terminal after running:

> sudo pip3 install pytrafikverket

Downloading/unpacking pytrafikverket
  Downloading pytrafikverket-0.1.4-py3-none-any.whl
Downloading/unpacking aiohttp (from pytrafikverket)
  Downloading aiohttp-2.0.7.tar.gz (739kB): 739kB downloaded
  Running setup.py (path:/tmp/pip-build-es5g1i6l/aiohttp/setup.py) egg_info for package aiohttp

    warning: no previously-included files matching '*.pyc' found anywhere in distribution
    warning: no previously-included files found matching 'aiohttp/_multidict.html'
    warning: no previously-included files found matching 'aiohttp/_multidict.*.so'
    warning: no previously-included files found matching 'aiohttp/_multidict.pyd'
    warning: no previously-included files found matching 'aiohttp/_multidict.*.pyd'
    warning: no previously-included files found matching 'aiohttp/_websocket.html'
    warning: no previously-included files found matching 'aiohttp/_websocket.*.so'
    warning: no previously-included files found matching 'aiohttp/_websocket.pyd'
    warning: no previously-included files found matching 'aiohttp/_websocket.*.pyd'
    no previously-included directories found matching 'docs/_build'
Downloading/unpacking async-timeout (from pytrafikverket)
  Downloading async_timeout-1.2.1-py3-none-any.whl
Downloading/unpacking lxml (from pytrafikverket)
  Downloading lxml-3.7.3.tar.gz (3.8MB): 3.8MB downloaded
  Running setup.py (path:/tmp/pip-build-es5g1i6l/lxml/setup.py) egg_info for package lxml
    Building lxml version 3.7.3.
    Building without Cython.
    ERROR: b'/bin/sh: 1: xslt-config: not found\n'
    ** make sure the development packages of libxml2 and libxslt are installed **

    Using build configuration of libxslt

    warning: no previously-included files found matching '*.py'
Requirement already satisfied (use --upgrade to upgrade): chardet in /usr/lib/python3/dist-packages (from aiohttp->pytrafikverket)
Downloading/unpacking multidict>=2.1.4 (from aiohttp->pytrafikverket)
  Downloading multidict-2.1.5.tar.gz (101kB): 101kB downloaded
  Running setup.py (path:/tmp/pip-build-es5g1i6l/multidict/setup.py) egg_info for package multidict

    warning: no previously-included files matching '*.pyc' found anywhere in distribution
    warning: no previously-included files found matching 'multidict/_multidict.html'
    warning: no previously-included files found matching 'multidict/_multidict.*.so'
    warning: no previously-included files found matching 'multidict/_multidict.pyd'
    warning: no previously-included files found matching 'multidict/_multidict.*.pyd'
    no previously-included directories found matching 'docs/_build'
Downloading/unpacking yarl>=0.10.0,<0.11 (from aiohttp->pytrafikverket)
  Downloading yarl-0.10.2.tar.gz (127kB): 127kB downloaded
  Running setup.py (path:/tmp/pip-build-es5g1i6l/yarl/setup.py) egg_info for package yarl

    warning: no previously-included files matching '*.pyc' found anywhere in distribution
    warning: no previously-included files matching '*.cache' found anywhere in distribution
    warning: no previously-included files found matching 'yarl/_quoting.html'
    warning: no previously-included files found matching 'yarl/_quoting.*.so'
    warning: no previously-included files found matching 'yarl/_quoting.pyd'
    warning: no previously-included files found matching 'yarl/_quoting.*.pyd'
    no previously-included directories found matching 'docs/_build'
Installing collected packages: pytrafikverket, aiohttp, async-timeout, lxml, multidict, yarl
*** Error compiling '/tmp/pip-build-es5g1i6l/pytrafikverket/pytrafikverket/__main__.py'...
  File "/tmp/pip-build-es5g1i6l/pytrafikverket/pytrafikverket/__main__.py", line 13
    async def async_main(loop):
            ^
SyntaxError: invalid syntax

*** Error compiling '/tmp/pip-build-es5g1i6l/pytrafikverket/pytrafikverket/trafikverket.py'...
  File "/tmp/pip-build-es5g1i6l/pytrafikverket/pytrafikverket/trafikverket.py", line 114
    async def make_request(self, objecttype:str,
            ^
SyntaxError: invalid syntax

*** Error compiling '/tmp/pip-build-es5g1i6l/pytrafikverket/pytrafikverket/trafikverket_train.py'...
  File "/tmp/pip-build-es5g1i6l/pytrafikverket/pytrafikverket/trafikverket_train.py", line 98
    async def get_train_station(self, location_name: str) -> StationInfo:
            ^
SyntaxError: invalid syntax

  Running setup.py install for aiohttp

    warning: no previously-included files matching '*.pyc' found anywhere in distribution
    warning: no previously-included files found matching 'aiohttp/_multidict.html'
    warning: no previously-included files found matching 'aiohttp/_multidict.*.so'
    warning: no previously-included files found matching 'aiohttp/_multidict.pyd'
    warning: no previously-included files found matching 'aiohttp/_multidict.*.pyd'
    warning: no previously-included files found matching 'aiohttp/_websocket.html'
    warning: no previously-included files found matching 'aiohttp/_websocket.*.so'
    warning: no previously-included files found matching 'aiohttp/_websocket.pyd'
    warning: no previously-included files found matching 'aiohttp/_websocket.*.pyd'
    no previously-included directories found matching 'docs/_build'
    building 'aiohttp._websocket' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.4m -c aiohttp/_websocket.c -o build/temp.linux-armv7l-3.4/aiohttp/_websocket.o
    arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 build/temp.linux-armv7l-3.4/aiohttp/_websocket.o -o build/lib.linux-armv7l-3.4/aiohttp/_websocket.cpython-34m.so
    building 'aiohttp._http_parser' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.4m -c aiohttp/_http_parser.c -o build/temp.linux-armv7l-3.4/aiohttp/_http_parser.o
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.4m -c vendor/http-parser/http_parser.c -o build/temp.linux-armv7l-3.4/vendor/http-parser/http_parser.o
    arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 build/temp.linux-armv7l-3.4/aiohttp/_http_parser.o build/temp.linux-armv7l-3.4/vendor/http-parser/http_parser.o -o build/lib.linux-armv7l-3.4/aiohttp/_http_parser.cpython-34m.so
  Running setup.py install for lxml
    Building lxml version 3.7.3.
    Building without Cython.
    ERROR: b'/bin/sh: 1: xslt-config: not found\n'
    ** make sure the development packages of libxml2 and libxslt are installed **

    Using build configuration of libxslt
    building 'lxml.etree' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -Isrc/lxml/includes -I/usr/include/python3.4m -c src/lxml/lxml.etree.c -o build/temp.linux-armv7l-3.4/src/lxml/lxml.etree.o -w
    In file included from src/lxml/lxml.etree.c:515:0:
    src/lxml/includes/etree_defs.h:14:31: fatal error: libxml/xmlversion.h: No such file or directory
     #include "libxml/xmlversion.h"
                                   ^
    compilation terminated.
    Compile failed: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
    cc -I/usr/include/libxml2 -c /tmp/xmlXPathInit026iqbts.c -o tmp/xmlXPathInit026iqbts.o
    /tmp/xmlXPathInit026iqbts.c:1:26: fatal error: libxml/xpath.h: No such file or directory
     #include "libxml/xpath.h"
                              ^
    compilation terminated.
    error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
    *********************************************************************************
    Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
    *********************************************************************************
    Complete output from command /usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-es5g1i6l/lxml/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-s3s88v19-record/install-record.txt --single-version-externally-managed --compile:
    Building lxml version 3.7.3.

Building without Cython.

ERROR: b'/bin/sh: 1: xslt-config: not found\n'

** make sure the development packages of libxml2 and libxslt are installed **



Using build configuration of libxslt

running install

running build

running build_py

creating build

creating build/lib.linux-armv7l-3.4

creating build/lib.linux-armv7l-3.4/lxml

copying src/lxml/doctestcompare.py -> build/lib.linux-armv7l-3.4/lxml

copying src/lxml/cssselect.py -> build/lib.linux-armv7l-3.4/lxml

copying src/lxml/sax.py -> build/lib.linux-armv7l-3.4/lxml

copying src/lxml/usedoctest.py -> build/lib.linux-armv7l-3.4/lxml

copying src/lxml/builder.py -> build/lib.linux-armv7l-3.4/lxml

copying src/lxml/ElementInclude.py -> build/lib.linux-armv7l-3.4/lxml

copying src/lxml/_elementpath.py -> build/lib.linux-armv7l-3.4/lxml

copying src/lxml/pyclasslookup.py -> build/lib.linux-armv7l-3.4/lxml

copying src/lxml/__init__.py -> build/lib.linux-armv7l-3.4/lxml

creating build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/__init__.py -> build/lib.linux-armv7l-3.4/lxml/includes

creating build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/_html5builder.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/html5parser.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/usedoctest.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/clean.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/_setmixin.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/ElementSoup.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/soupparser.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/builder.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/defs.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/diff.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/formfill.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/__init__.py -> build/lib.linux-armv7l-3.4/lxml/html

copying src/lxml/html/_diffcommand.py -> build/lib.linux-armv7l-3.4/lxml/html

creating build/lib.linux-armv7l-3.4/lxml/isoschematron

copying src/lxml/isoschematron/__init__.py -> build/lib.linux-armv7l-3.4/lxml/isoschematron

copying src/lxml/lxml.etree.h -> build/lib.linux-armv7l-3.4/lxml

copying src/lxml/lxml.etree_api.h -> build/lib.linux-armv7l-3.4/lxml

copying src/lxml/includes/xslt.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/etreepublic.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/xmlparser.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/htmlparser.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/config.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/xmlerror.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/schematron.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/relaxng.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/tree.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/uri.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/xinclude.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/xpath.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/xmlschema.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/dtdvalid.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/c14n.pxd -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/etree_defs.h -> build/lib.linux-armv7l-3.4/lxml/includes

copying src/lxml/includes/lxml-version.h -> build/lib.linux-armv7l-3.4/lxml/includes

creating build/lib.linux-armv7l-3.4/lxml/isoschematron/resources

creating build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/rng

copying src/lxml/isoschematron/resources/rng/iso-schematron.rng -> build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/rng

creating build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/xsl

copying src/lxml/isoschematron/resources/xsl/XSD2Schtrn.xsl -> build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/xsl

copying src/lxml/isoschematron/resources/xsl/RNG2Schtrn.xsl -> build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/xsl

creating build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/xsl/iso-schematron-xslt1

copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_abstract_expand.xsl -> build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/xsl/iso-schematron-xslt1

copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_dsdl_include.xsl -> build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/xsl/iso-schematron-xslt1

copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_svrl_for_xslt1.xsl -> build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/xsl/iso-schematron-xslt1

copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_schematron_skeleton_for_xslt1.xsl -> build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/xsl/iso-schematron-xslt1

copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/iso_schematron_message.xsl -> build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/xsl/iso-schematron-xslt1

copying src/lxml/isoschematron/resources/xsl/iso-schematron-xslt1/readme.txt -> build/lib.linux-armv7l-3.4/lxml/isoschematron/resources/xsl/iso-schematron-xslt1

running build_ext

building 'lxml.etree' extension

creating build/temp.linux-armv7l-3.4

creating build/temp.linux-armv7l-3.4/src

creating build/temp.linux-armv7l-3.4/src/lxml

arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -Isrc/lxml/includes -I/usr/include/python3.4m -c src/lxml/lxml.etree.c -o build/temp.linux-armv7l-3.4/src/lxml/lxml.etree.o -w

In file included from src/lxml/lxml.etree.c:515:0:

src/lxml/includes/etree_defs.h:14:31: fatal error: libxml/xmlversion.h: No such file or directory

 #include "libxml/xmlversion.h"

                               ^

compilation terminated.

Compile failed: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

creating tmp

cc -I/usr/include/libxml2 -c /tmp/xmlXPathInit026iqbts.c -o tmp/xmlXPathInit026iqbts.o

/tmp/xmlXPathInit026iqbts.c:1:26: fatal error: libxml/xpath.h: No such file or directory

 #include "libxml/xpath.h"

                          ^

compilation terminated.

error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

*********************************************************************************

Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?

*********************************************************************************

----------------------------------------
Cleaning up...
Command /usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-es5g1i6l/lxml/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-s3s88v19-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip-build-es5g1i6l/lxml
Storing debug log for failure in /root/.pip/pip.log

My linux knowledge and experiance is very limited, but it seems to be some problem with libxml2 and libxslt?
libxml2 is installed and updated, not sure about libxlst thou.

Any ideas?

What version of Python are you using?
The current code uses async and await and support for that was added 3.5 i think, i will rewrite the library to coroutines code later when i get past the installation issue.
(So far no one has responded to my github issue :confused:)

I have a early version that you guys could test out


now i have only tested on 3.6 but im hoping it will work on 3.4 as im not using the async/await syntax any more…
Note that this was just a quick writeup and might contain bugs and/or styling issues.
All feedback is welcome!

Update: 2017-05-30 Added attributes and examples for notifications.
1 Like

I think I at last managed to install pytrafikverket by first running:

sudo apt-get install python3-lxml

At least I didn’t get any errors in the installation. Now I’m going to try to get the custom_component working, haven’t had much luck with other custom components but we will see. :slight_smile:
Thanks for all the work @AnderssonPeter

Since I have started using this there has not been a single delay or cancelation so I don’t know how good it works.
So please come with feedback once you have got it working.

I may have spoken too soon. I couldn’t get it to work and then tried to reinstall pytrafikverket with:

sudo pip3 install pytrafikverket

My terminal then outputs:

Downloading/unpacking pytrafikverket
  Downloading pytrafikverket-0.1.5.5-py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): aiohttp in /usr/local/lib/python3.4/dist-packages (from pytrafikverket)
Requirement already satisfied (use --upgrade to upgrade): lxml in /usr/lib/python3/dist-packages (from pytrafikverket)
Requirement already satisfied (use --upgrade to upgrade): async-timeout in /usr/local/lib/python3.4/dist-packages (from pytrafikverket)
Requirement already satisfied (use --upgrade to upgrade): chardet in /usr/lib/python3/dist-packages (from aiohttp->pytrafikverket)
Requirement already satisfied (use --upgrade to upgrade): multidict>=2.1.4 in /usr/local/lib/python3.4/dist-packages (from aiohttp->pytrafikverket)
Requirement already satisfied (use --upgrade to upgrade): yarl>=0.10.0,<0.11 in /usr/local/lib/python3.4/dist-packages (from aiohttp->pytrafikverket)
Installing collected packages: pytrafikverket
*** Error compiling '/tmp/pip-build-wyquk3ed/pytrafikverket/pytrafikverket/__main__.py'...
  File "/tmp/pip-build-wyquk3ed/pytrafikverket/pytrafikverket/__main__.py", line 13
    async def async_main(loop):
            ^
SyntaxError: invalid syntax

Successfully installed pytrafikverket
Cleaning up...

I’m at a loss…

Sorry for the late reply, i guess i missed one file when i converted to non async code.
Ill see if i get some free time to fix this the coming days.

I’m completly new to the home automation scene. Can I just import your repo to my home assistant?

I have seen your library before but not the component. Seems like a very good job to me. Why haven’t you created a PR to get an offical HA component or am I missing something?

Br Qxlkdr

Hi Qxlkdr, I wanted to do some refactoring of the library before I create a PR, but I haven’t managed to get any IDE to work correctly (or it might just be that I have laid out the files incorrectly) , and after trying a few times I just gave up.

I apparently have no idea what I’m doing :stuck_out_tongue:.

Hi Andersson! Fantastic idea, Since you never can trust vasttrafik to inform you of late train arrivals. Never installed a custom python script before. I tried to install this on my hassio installation with no luck so far. Is this just working on a normal HA installation?

@johbe i had so many issues trying to use the xml library python uses, so i just gave up (when you import my library, it tries to compile the xml library and it uses huge amounts of ram, so if you have anything else running on the pi it crashes, i don’t understand how compiling something can use so much ram).
I’m currently working on writing it in c# and using mqtt to get the data into home assistant, but i hit a minor roadblock Format mqtt json fields once that is done i should be able to create a docker container, that some one could convert into a hassio plugin… (i don’t use hassio my self, last time i tried it stopped me from using my pi for other stuff)

Alright I see. BIG THANKS for developing and I hope to see it as a hassio plugin in the future. It would be so cool to have automations created based on the input from trafikverket. Keep up the good work man! :slight_smile: