I2C Relay PiHat Switch Configuration

Hi all,
Short time reader, first time writer. I purchased a few RPi 4 channel relay hats to control my radiant heating system. Here’s the link: https://wiki.52pi.com/index.php/DockerPi_4_Channel_Relay_SKU:_EP-0099
They are controlled through I2C. I have installed Hassio. I realize I need to enable I2C.

I originally had Raspbian running on the Pi and was able to control the state of the relays directly using the command: i2cset -y 1 0x10 0x01 0xFF which would turn on Relay 1 of on I2C address 1.

How/can I edit my configuration.yaml file to properly setup this switch. I don’t see any existing Integration or Platform, but I’m clearly very new to all of this.

Many thanks!

Hi,
I’ve just written a service for this, I start too learn python 3 and HA.

import time as t
from .smbus2 import SMBus

DOMAIN = “relay_switch”
DEVICE_ADDR = 0x10
DEVICE_BUS = 1

def setup(hass, config):
def demo(call):
bus = SMBus(DEVICE_BUS)

    for i in range(1,5):
        bus.write_byte_data(DEVICE_ADDR, i, 0xFF)
        t.sleep(1)
        bus.write_byte_data(DEVICE_ADDR, i, 0x00)
        t.sleep(1)

# Register our service with Home Assistant.
hass.services.register(DOMAIN, 'demo', demo)

def option_yokis(call):
    DEFAULT_RELAY = 1
    DEFAULT_REPEAT = 1
    relay = call.data.get("relay", DEFAULT_RELAY)
    repeat = call.data.get("repeat", DEFAULT_REPEAT)
    bus = SMBus(DEVICE_BUS)
    for i in range(0,repeat):
        bus.write_byte_data(DEVICE_ADDR, relay, 0xFF)
        t.sleep(0.1)
        bus.write_byte_data(DEVICE_ADDR, relay, 0x00)
        t.sleep(0.2)

# Register our service with Home Assistant.
hass.services.register(DOMAIN, 'option_yokis', option_yokis)

def switch(call):
    DEFAULT_RELAY = 1
    relay = call.data.get("relay", DEFAULT_RELAY)
    bus = SMBus(DEVICE_BUS)
    bus.write_byte_data(DEVICE_ADDR, relay, 0xFF)
    t.sleep(0.5)
    bus.write_byte_data(DEVICE_ADDR, relay, 0x00)
    t.sleep(0.5)

# Register our service with Home Assistant.
hass.services.register(DOMAIN, 'switch', switch)

def turn_on_parking(call):
    bus = SMBus(DEVICE_BUS)
    bus.write_byte_data(DEVICE_ADDR, 1, 0xFF)
    t.sleep(0.5)
    bus.write_byte_data(DEVICE_ADDR, 1, 0x00)
    t.sleep(0.5)

# Register our service with Home Assistant.
hass.services.register(DOMAIN, 'turn_on_parking', turn_on_parking)

def turn_off_parking(call):
    bus = SMBus(DEVICE_BUS)
    bus.write_byte_data(DEVICE_ADDR, 2, 0xFF)
    t.sleep(0.5)
    bus.write_byte_data(DEVICE_ADDR, 2, 0x00)
    t.sleep(0.5)

# Register our service with Home Assistant.
hass.services.register(DOMAIN, 'turn_off_parking', turn_off_parking)

def turn_on_chemin(call):
    bus = SMBus(DEVICE_BUS)
    bus.write_byte_data(DEVICE_ADDR, 3, 0xFF)
    t.sleep(0.5)
    bus.write_byte_data(DEVICE_ADDR, 3, 0x00)
    t.sleep(0.5)

# Register our service with Home Assistant.
hass.services.register(DOMAIN, 'turn_on_chemin', turn_on_chemin)

def turn_off_chemin(call):
    bus = SMBus(DEVICE_BUS)
    bus.write_byte_data(DEVICE_ADDR, 4, 0xFF)
    t.sleep(0.5)
    bus.write_byte_data(DEVICE_ADDR, 4, 0x00)
    t.sleep(0.5)

# Register our service with Home Assistant.
hass.services.register(DOMAIN, 'turn_off_chemin', turn_off_chemin)

# Return boolean to indicate that initialization was successfully.
return True

I get permission denied with haos while issuing commands in terminal. If I boot rasbiain with i2c enabled I can control the relays. Any hints?

I have installed and run i2c configurator. Rebooted etc

(berks) ➜ berks l
total 32K
drwxr-xr-x 6 root root 4.0K Nov 19 13:16 .
drwxr-xr-x 12 root root 4.0K Dec 1 20:03 …
drwxr-xr-x 5 root root 4.0K Nov 19 13:16 berks
drwxr-xr-x 2 root root 4.0K Nov 16 07:34 bin
drwxr-xr-x 2 root root 4.0K Nov 16 07:31 include
drwxr-xr-x 3 root root 4.0K Nov 16 07:31 lib
lrwxrwxrwx 1 root root 3 Nov 16 07:31 lib64 → lib
-rw-r–r-- 1 root root 70 Nov 19 13:15 pyvenv.cfg
-rw-r–r-- 1 root root 56 Nov 16 07:29 test.py

(berks) ➜ berks cat test.py
import smbus2
bus = smbus2.SMBus(0)
print('it worked!’)

(berks) ➜ berks python3 test.py
Traceback (most recent call last):
File “/config/berks/test.py”, line 2, in
bus = smbus2.SMBus(0)
File “/config/berks/berks/lib/python3.10/site-packages/smbus2/smbus2.py”, line 280, in init
self.open(bus)
File “/config/berks/berks/lib/python3.10/site-packages/smbus2/smbus2.py”, line 310, in open
self.fd = os.open(filepath, os.O_RDWR)
PermissionError: [Errno 1] Operation not permitted: ‘/dev/i2c-0’

(berks) ➜ berks l /dev/i*
crw------- 1 root root 89, 0 Jun 2 2022 /dev/i2c-0
crw------- 1 root root 89, 1 Jun 2 2022 /dev/i2c-1
crw------- 1 root root 89, 10 Jun 2 2022 /dev/i2c-10
crw------- 1 root root 89, 22 Jun 2 2022 /dev/i2c-22