Custom Components for Microchip MCP3424 A/D converters and MCP23017 16-bit I/O expander chips

I was introduced to Home Assistant a few weeks ago as one of our customers was trying to use the IO PI Plus (which we design and manufacture) with Home Assistant

I have written some custom components to use Home Assistant to read from Microchip MCP3424 A/D converters and read and write to Microchip MCP23017 16-bit I/O expander chips.

You can download the components from https://github.com/abelectronicsuk/abelectronics-home-assistant-custom-components and there are also CLI interfaces for the MCP23017 written in C in the repository.

The IO expander the code was written for is on https://www.abelectronics.co.uk/p/54/IO-Pi-Plus and the code should work with any other board which uses the MCP23017 I2C chips.

The 8 channel ADC board is https://www.abelectronics.co.uk/p/56/ADC-Pi-Plus-Raspberry-Pi-Analogue-to-Digital-converter and the ADC code will work with any MCP3424 based board.

The repository also has example configuration, groups and sensor yaml files to display the input and output states.

3 Likes

Hi Briand,
I bought the IO Pi Plus board from you guys but I didn’t like that I had to create a whole bunch of template sensors using your binary sensor component for the IO Pi Plus board.
I ended up writing a binary sensor custom component for the MCP23017 IO expander that uses a similar structure as the rpi_gpio binary sensor in the configuration.yaml:

binary_sensor:
  - platform: mcp23017
    i2c_address: 0x20
    scan_interval: 1
    pins:
      0: Living Room Window
      1: Kitchen Window
      2: Front Door

I am using some python modules created by Adafruit. I might write a switch component later on using these same modules.
Here is my code: https://github.com/jardiamj/homeassistant/tree/master/custom_components

Hello @Jardiamj ,

Question, have you started writing for the output yet?
Since abelectronics-home-assistant-custom-components are no longer supported

Hi!

I wrote something but I haven’t tested it yet because I don’t have any practical use for it at the moment. I’ll test it out with some LEDs either today or tomorrow and I’ll upload it if it works.

Jardi.

1 Like

That’s great.
I am curious. I would like to test it.
Let me know

Hi @rubendijk,

I uploaded the switch.py file to my repository just now and added a configuration example to the README file. I just made sure it loads, but as I said I haven’t tested it yet.

EDIT: I tested the switch component with some LEDs, when I got home today, and it seems to work well.

My github repository link is posted above, the configuration.yaml section should look like this:

# Example configuration.yaml entry
switch:
  - platform: mcp23017
    i2c_address: 0x20
    pins:
      11: Fan Office 
      12: Light Desk

You can leave the i2c_address out and it will default to 0x20.

You can check it out, test it and let me know if it works.

P.S.
If anyone is interested I also found out that you can use a VGA or HDMI port to drive the MCP23017 IC. My current plan is to move my whole set up to an Intel NUC and connect the I/O expander chip to an HDMI port on it.

Hi @Jardiamj

This is super good news. I’m gonna test it tonight and let you know the results.
Thank you very much for the quick reactions.

My thoughts also go out to an Intel NUC for the future.
It seems practical to me to be able to use the HDMI port.
If there is something where the nuc doesn’t start anymore, you can still monitor it.

My preference would be to use the VGA port.
It is certainly nice that you make these possibilities. so continue :smiley:

Hello @Jardiamj

I have tested the switch.py. must add that I use the installation hassio on a raspberry pi

Hassio comes up with the following message :
2019-04-12 19:30:12 ERROR (MainThread) [homeassistant.requirements] Not initializing mcp23017.switch because could not install requirement adafruit-blinka==1.2.1 2019-04-12 19:30:12 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform mcp23017.switch: Could not install all requirements. 2019-04-12 20:28:09 ERROR (MainThread) [homeassistant.components.updater] Could not contact Home Assistant Update to check for updates

I’m going to continue testing in the course of the days.

That message is not of much help. I don’t personally run hassio but looking for information about why that requirement might not be installing I found out that the way you load the requirements for the components has changed.
You are supposed to put them in manifest.json file together with some other information. I made thoae changes to the component today.
Can you try it with the new changes and see if it still gives you that message?

EDIT: I also found this issue in github that might be related, but I don’t know: https://github.com/home-assistant/home-assistant/issues/15476

Hello @Jardiamj

I’ve tested a number of things. Now I can’t get the following module installed.

adafruit-blinka==1.2.1
the module displays the following errors

2019-05-03 18:19:55 ERROR (SyncWorker_0) [homeassistant.util.package] Unable to install package adafruit-blinka==1.2.1: Failed building wheel for sysv-ipc
Failed building wheel for rpi-ws281x
Command “/usr/local/bin/python3 -u -c “import setuptools, tokenize;file=’/tmp/pip-install-nog1nose/sysv-ipc/setup.py’;f=getattr(tokenize, ‘open’, open)(file);code=f.read().replace(’\r\n’, ‘\n’);f.close();exec(compile(code, file, ‘exec’))” install --record /tmp/pip-record-m5l25q9s/install-record.txt --single-version-externally-managed --prefix --compile --user --prefix=” failed with error code 1 in /tmp/pip-install-nog1nose/sysv-ipc/
You are using pip version 19.0.3, however version 19.1 is available.
You should consider upgrading via the ‘pip install --upgrade pip’ command.
2019-05-03 18:19:55 ERROR (MainThread) [homeassistant.requirements] Not initializing mcp23017 because could not install requirement adafruit-blinka==1.2.1
2019-05-03 18:19:55 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform mcp23017: Could not install all requirements.
2019-05-03 18:21:15 ERROR (SyncWorker_4) [homeassistant.util.package] Unable to install package adafruit-blinka==1.2.1: Failed building wheel for rpi-ws281x
Failed building wheel for sysv-ipc
Command “/usr/local/bin/python3 -u -c “import setuptools, tokenize;file=’/tmp/pip-install-_rv9eywk/rpi-ws281x/setup.py’;f=getattr(tokenize, ‘open’, open)(file);code=f.read().replace(’\r\n’, ‘\n’);f.close();exec(compile(code, file, ‘exec’))” install --record /tmp/pip-record-bua0ship/install-record.txt --single-version-externally-managed --prefix --compile --user --prefix=” failed with error code 1 in /tmp/pip-install-_rv9eywk/rpi-ws281x/
You are using pip version 19.0.3, however version 19.1 is available.
You should consider upgrading via the ‘pip install --upgrade pip’ command.
2019-05-03 18:21:15 ERROR (MainThread) [homeassistant.requirements] Not initializing mcp23017 because could not install requirement adafruit-blinka==1.2.1
2019-05-03 18:21:15 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform mcp23017: Could not install all requirements.

I have seen this type of pip install fails before and it often resolves with a restart. I tested it just now and the requirements install fine for me.

BTW, I updated my custom component just a couple of days ago with an implementation for the use of the hardware interrupt pins in the binary sensors.

I did see the update. I also saw that you did a pull request for ha integration nice job.

I think/hope it won’t take long for them to implement it. I’m just waiting for this.

I’ve restarted hassio and the error message remains.

Hello everyone!
I would love to use my mcp23xx with your component Jardiamj veeeery much, but doesn’t work for me. Configuration.yaml seems to be correct - no errors. But on my overview page "in the “bell” is the message:
"Invald config
The following components and platform could not be set up:

  • mcp23017.switch
    Pleas check your config."

It seems to me that I did everything as you wrote in the “readme” file. I copied these 4 files to the “custom_components / mcp23017” directory using the online GUI. Do you have an idea what could be wrong?
I’m just starting to play with home automation, so forgive me if I’m not too precise :slight_smile:

Hi @sokolsok!

For some reason I missed your post, so forgive my late response.
I have done quite a few changes to my custom component in the last few weeks and I also got it merged into the HA development branch about a week ago.
The binary sensor custom component and the one merged with the development branch are slightly different. The one merged with HA uses polling and the one in my custom component repo uses a hackish implementation of the hardware interrupts. I have done a PR against the Adafruit’s library so I could do a more proper implementation into the component already merged.
With all that said, I hope you figured out what the issue was but if you haven’t please post again with the block of log related to the mcp23017.switch.

Jardi.

Thaaank you for your reply. Unfortunately, I haven’t solved this problem yet.
I have just re-installed Hass.io to be sure that everything is ok. I have installed your custom component but unfortunately, still the same issue… There is a problem in importing libraries. I’ve tried everything and I don’t know what the problem is :frowning:

My log:

2019-06-02 17:34:12 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for mcp23017 which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2019-06-02 17:34:13 ERROR (MainThread) [homeassistant.components.device_tracker] Unable to load /config/known_devices.yaml: Config file not found: /config/known_devices.yaml
2019-06-02 17:35:29 ERROR (SyncWorker_13) [homeassistant.util.package] Unable to install package adafruit-blinka==1.2.1: ERROR: Complete output from command /usr/local/bin/python3 -u -c ‘import setuptools, tokenize;file=’"’"’/tmp/pip-install-koyy4lfw/rpi-ws281x/setup.py’"’"’;f=getattr(tokenize, ‘"’"‘open’"’"’, open)(file);code=f.read().replace(’"’"’\r\n’"’"’, ‘"’"’\n’"’"’);f.close();exec(compile(code, file, ‘"’"‘exec’"’"’))’ bdist_wheel -d /tmp/pip-wheel-fzyh8zzv --python-tag cp37:

  • ERROR: running bdist_wheel*
  • running build*
  • running build_py*
  • Compiling ws281x library…*
  • error: [Errno 2] No such file or directory: ‘make’: ‘make’*
  • ----------------------------------------*
  • ERROR: Failed building wheel for rpi-ws281x*
  • ERROR: Complete output from command /usr/local/bin/python3 -u -c ‘import setuptools, tokenize;file=’"’"’/tmp/pip-install-koyy4lfw/sysv-ipc/setup.py’"’"’;f=getattr(tokenize, ‘"’"‘open’"’"’, open)(file);code=f.read().replace(’"’"’\r\n’"’"’, ‘"’"’\n’"’"’);f.close();exec(compile(code, file, ‘"’"‘exec’"’"’))’ bdist_wheel -d /tmp/pip-wheel-i8lpqb2m --python-tag cp37:*
  • ERROR: *******************************************************************************
    • Setup can’t determine the value of PAGE_SIZE on your system, so it will*
    • default to 4096 which may not be correct.*

    • Please report this message and your operating system info to the package*
    • maintainer listed in the README file.*

  • running bdist_wheel*
  • running build*
  • running build_ext*
  • building ‘sysv_ipc’ extension*
  • creating build*
  • creating build/temp.linux-armv7l-3.7*
  • gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -DTHREAD_STACK_SIZE=0x100000 -fPIC -I/usr/local/include/python3.7m -c sysv_ipc_module.c -o build/temp.linux-armv7l-3.7/sysv_ipc_module.o*
  • unable to execute ‘gcc’: No such file or directory*
  • error: command ‘gcc’ failed with exit status 1*
  • ----------------------------------------*
  • ERROR: Failed building wheel for sysv-ipc*
  • ERROR: Complete output from command /usr/local/bin/python3 -u -c ‘import setuptools, tokenize;file=’"’"’/tmp/pip-install-koyy4lfw/rpi-ws281x/setup.py’"’"’;f=getattr(tokenize, ‘"’"‘open’"’"’, open)(file);code=f.read().replace(’"’"’\r\n’"’"’, ‘"’"’\n’"’"’);f.close();exec(compile(code, file, ‘"’"‘exec’"’"’))’ install --record /tmp/pip-record-prvn6_3t/install-record.txt --single-version-externally-managed --prefix ‘’ --compile --user --prefix=:*
  • ERROR: running install*
  • running build*
  • running build_py*
  • Compiling ws281x library…*
  • error: [Errno 2] No such file or directory: ‘make’: ‘make’*
  • ----------------------------------------*
    ERROR: Command “/usr/local/bin/python3 -u -c ‘import setuptools, tokenize;file=’”’"’/tmp/pip-install-koyy4lfw/rpi-ws281x/setup.py’"’"’;f=getattr(tokenize, ‘"’"‘open’"’"’, open)(file);code=f.read().replace(’"’"’\r\n’"’"’, ‘"’"’\n’"’"’);f.close();exec(compile(code, file, ‘"’"‘exec’"’"’))’ install --record /tmp/pip-record-prvn6_3t/install-record.txt --single-version-externally-managed --prefix ‘’ --compile --user --prefix=" failed with error code 1 in /tmp/pip-install-koyy4lfw/rpi-ws281x/
    2019-06-02 17:35:29 ERROR (MainThread) [homeassistant.requirements] Not initializing mcp23017 because could not install requirement adafruit-blinka==1.2.1
    2019-06-02 17:35:29 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform mcp23017: Could not install all requirements.

Well, this is the same issue that @rubendijk is having. And the error is basically saying that the adafruit-blinka library couldn’t be installed because one of it’s components couldn’t be installed (rpi-ws281x) and this happens on hass.io. I don’t run hass.io so I can’t really tell why that’s failing.
All I could find for that error code was this: https://github.com/pimoroni/unicorn-hat/issues/70
I am using Adafruit’s library for the component because I didn’t want to reinvent the wheel and unfortunately their library for the mcp230xx chips depends on their Adafruit-blinka library which is the one that fails to install on Hass.io.
Hopefully, now that the component got merged into HA it will be available in the upcoming releases.
I am sorry that I can’t be of much help with this, I will research it a little more once I get back home in a couple of days.

Hello Jardiamj!
I had a break for finishing the house and had to postpone the Home Assistant for a while. But now I can finally take care of it again :slight_smile:
Thank you so much for your answers and for your custom component - it’s great!
However, I have an additional question / request. I changed the concept a little bit. I want to connect RaspberryPi with a microcontroller to which outputs and inputs are connected. My microcontroller is slave on I2C bus. And I prepared it so that your component “thinks” that MCP23xx is connected.
And everything works when I connect one “virtual” MCP23xx. Unfortunately in uC I can set only one address when communicating via I2C. So if I try to create more switches / binary_sensors in HA then only one works.
I am fresh on the subject of Hassio and python. Would it be a problem to add in front of each frame sent one additional byte? I mean that the address should always be constant, and this “i2c_address:” configured in “configuration.yaml” was just another byte? I don’t know if I complicated it too much? And if you understand and what I mean? :slight_smile: I’d like to leave your communication exactly as it is and push an extra byte first. Is this possible?

Ps. Of course, I copied your official component to myself and I use it as a custom component so I can edit it.

Pps. Of course, I don’t count on you doing it for me :slight_smile: But if you could guide me somehow? Or gently push it in the right direction?

I thank you in advance and best regards,
Sebastian

I tried to figure it out yesterday and … It doesn’t work yet, but from what I think I need to change mainly the MCP23xxx.py library. I downloaded it from github and I want to put it locally so I can modify it. Tell me please in which folder to put it and if I write in the switch.py file “import MCP23xxx” then, will it be ok? Or do I have to import the local library to custom component in any other way?

Sorry for the late reply, I had not been to the forums in a while. Firstly, may I ask what microcontroller are you using and why are you trying to use it this way?

Now, what you describe might be possible, I just don’t know how your are emulating the IC since I am not sure I understand your description.

The component uses the Adafruit’s circuit python mcp23017 library as you have already noticed. There are a few internal functions that you can use or override, depending on what you need, which are: _write_u8( ) and _write_u16le( ).

Do I need to be running HassIO to use this?