i dont modify the host system… you can do it thouugh directly in HassOS, but its not needed
I just launch my python script as a custom component, so upon boot HA,it loads the modules in kernel and then i do a candump to a template sensor
see here, do this as test, place below in your sensor.yaml file , its all hardcoded, so chang the names afterwards :
- platform: dobiss
- platform: template
sensors:
dobiss:
friendly_name: "dobiss state sensor"
value_template: "dobiss"
shows up as picture below , for me those are all can messages
then in your custom_component folder, create a folder “dobiss”
2 files need to be there…
- manifest.json
{
"domain": "dobiss",
"name": "Dobiss integration",
"documentation": "https://github.com/pergolafabio",
"issue_tracker": "https://github.com/pergolafabio",
"config_flow": false,
"version": "0.0.1",
"codeowners": [
"@pergolafabio"
],
"requirements": ["python-can"]
}
2 sensor.py
- uncomment line 36 if you want , then you see data in your home assistant log file
- line 49 , i install there canutils, download the canutils and make sure its in your folder, its needed, canutils is not part of the container, also dont change to “apk add canutils” it wont download it, you need to place it there manually
- line 57 , make sure you create long lived token, i do a rest call to update the template sensor with curl
you probably need to change data offcourse, i do some substrings, its for my personal use… but the logger should give you already something normally?
import logging
import voluptuous as vol
import can
import subprocess
import os
import threading
import asyncio
import requests
import json
from homeassistant.components.sensor import (SensorEntity)
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
_LOGGER.debug("Setting up Dobiss sensor.")
def set_attribute(sensor_name, attribute, value):
response = requests.get(url_states + sensor_name, headers=headers)
msg = json.loads(response.text)
msg['attributes'][attribute] = value
payload = json.dumps({'state': msg['state'], 'attributes': msg['attributes']})
requests.post(url_states + sensor_name, headers=headers, data=payload)
def background():
bus = can.interface.Bus(bustype='socketcan', channel='can0', bitrate=125000)
while True:
message = bus.recv()
#data = "[{}]".format(message.dlc)
data = ""
for i in range(0,message.dlc):
data += " {:02X}".format(message.data[i])
data = str(data.strip())
#_LOGGER.warning(data)
if not message.data.hex()[2:4] == "99": ## 57 programma
if not message.data.hex()[2:4] == "58":
if not message.data.hex()[2:4] == "5b":
try:
set_attribute(sensor_name, message.data.hex()[2:6], str(message.data.hex()[6:8]))
except:
pass
subprocess.call(['modprobe', 'can'])
subprocess.call(['modprobe', 'can_raw'])
subprocess.call(['modprobe', 'slcan'])
os.system("apk add /config/python_scripts/canutils.apk")
os.system("slcand -o -c -s4 /dev/ttyACM0 can0")
os.system("ifconfig can0 txqueuelen 1000")
os.system("ifconfig can0 up")
url_states = "http://localhost:8123/api/states/"
sensor_name = "sensor.dobiss"
headers = {
'Authorization': 'YOURTOKENHERE',
'content-type': 'application/json',
}
t = threading.Thread(name='background', target=background)
t.start()