Skip to content

Commit

Permalink
fix: class instance check
Browse files Browse the repository at this point in the history
  • Loading branch information
pilotak committed Jan 15, 2024
1 parent 17ac76d commit c987ca5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 24 deletions.
89 changes: 79 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
# Homeassistant - MCP2221 driver
# HomeAssistant - MCP2221 integration
[![validate](https://github.com/pilotak/homeassistant-mcp2221/actions/workflows/validate.yaml/badge.svg)](https://github.com/pilotak/homeassistant-mcp2221/actions/workflows/validate.yaml)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)


## ⚠️ Work in progress ⚠️

Before install you need to SSH/Terminal into Home Assistant and run
```sh
apk add g++ linux-headers libusb-dev eudev-dev
```
The advantage of running HomeAssistant on Raspberry Pi is that you have a few GPIOs to play with. This integration adds missing GPIOs (input, output, ADC) to your NUC like / Proxmox based instalation.

```yaml
# Example configuration.yaml entry
mcp2221:
- dev: 0
switches:
- switches:
- name: "Output 0"
pin: 0
unique_id: out0
Expand All @@ -36,4 +30,79 @@ mcp2221:
# - name: "voltage"
# pin: 3
# unit_of_measurement: "bit"
```
# unique_id: adc3
```

## Install via [HACS](https://github.com/custom-components/hacs)
You can find this integration in the community store.

## Install manually
You need to copy the `mcp2221` folder from this repo to the `custom_components` folder in the root of your configuration. The file tree should look like this:
```
└── ...
└── configuration.yaml
└── custom_components
└── mcp2221
└── __init__.py
└── sensor.py
└── ...
```

>__Note__: if the `custom_components` directory does not exist, you need to create it.
### Full examples

<details>
<summary>1️⃣Switch (output) & multiple devices</summary>
You can also specift multiple device, just adjust the index (`dev`) or even specify different PID/VID
```yaml
mcp2221:
- dev: 0
pid: 0x00DD
vid: 0x04D8
switches:
- name: "Output 0"
pin: 0
unique_id: output_0_0
icon: mdi:toggle-switch
- dev: 1
switches:
- name: "Output 0"
pin: 0
unique_id: output_1_0
icon: mdi:toggle-switch
```
</details>
<details>
<summary>2️⃣Binary sensor (input)</summary>
```yaml
mcp2221:
binary_sensors:
- name: "Input 1"
pin: 1
inverted: True
unique_id: input_0
icon: mdi:toggle-switch
scan_interval: 10s
device_class: door
```
</details>
<details>
<summary>3️⃣Sensor (ADC)</summary>
Only pins GP1-GP3, output is 10-bit (0-1023)
```yaml
mcp2221:
adc:
ref: 4.096 # 1.024, 2.048, 4.096 or "VDD"
sensors:
- name: "Battery voltage"
pin: 3
unit_of_measurement: "V"
unique_id: battery_voltage
scan_interval: 10s
icon: mdi:car-battery
device_class: voltage
```
</details>
3 changes: 1 addition & 2 deletions custom_components/mcp2221/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""The command_line component."""
from __future__ import annotations
"""The MCP2221 component."""

import asyncio
from collections.abc import Coroutine
Expand Down
10 changes: 6 additions & 4 deletions custom_components/mcp2221/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""MCP2221 binary sensor"""

from datetime import timedelta
from typing import Union
from datetime import datetime, timedelta
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
Expand Down Expand Up @@ -64,7 +63,7 @@ async def async_setup_platform(
device_instance: MCP2221.MCP2221() | None = hass.data[DOMAIN].get(
binary_sensor.get(CONF_DEVICE_ID))

if not isinstance(device_instance, MCP2221.MCP2221()):
if not isinstance(device_instance, MCP2221.MCP2221):
LOGGER.error("No instance of MCP2221")
return

Expand Down Expand Up @@ -109,7 +108,7 @@ async def async_added_to_hass(self) -> None:
self.async_on_remove(
async_track_time_interval(
self.hass,
self.update,
self._update_state,
self._scan_interval,
cancel_on_shutdown=True,
),
Expand All @@ -125,3 +124,6 @@ def update(self):
"""Update state."""
self._state = self._device.ReadGP(self._pin)
self.async_write_ha_state()

def _update_state(self, now: datetime | None = None) -> None:
self.update()
1 change: 0 additions & 1 deletion custom_components/mcp2221/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""MCP2221 constants"""


import logging

from homeassistant.const import Platform
Expand Down
12 changes: 8 additions & 4 deletions custom_components/mcp2221/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""MCP2221 binary sensor"""
"""MCP2221 sensor"""

from datetime import timedelta
from datetime import datetime, timedelta

from homeassistant.components.sensor import (
SensorEntity,
Expand Down Expand Up @@ -69,7 +69,7 @@ async def async_setup_platform(
device_instance: MCP2221.MCP2221() | None = hass.data[DOMAIN].get(
sensor.get(CONF_DEVICE_ID))

if not isinstance(device_instance, MCP2221.MCP2221()):
if not isinstance(device_instance, MCP2221.MCP2221):
LOGGER.error("No instance of MCP2221")
return

Expand Down Expand Up @@ -124,7 +124,7 @@ async def async_added_to_hass(self) -> None:
self.async_on_remove(
async_track_time_interval(
self.hass,
self.update,
self._update_state,
self._scan_interval,
cancel_on_shutdown=True,
),
Expand All @@ -138,3 +138,7 @@ def update(self):
"""Update value."""
self.value = self._device.ReadADC(self._pin)
self.async_write_ha_state()

def _update_state(self, now: datetime | None = None) -> None:
self.update()

4 changes: 1 addition & 3 deletions custom_components/mcp2221/switch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""MCP2221 switch"""

from datetime import datetime, timedelta

from homeassistant.components.switch import SwitchEntity
from homeassistant.const import (
CONF_PIN,
Expand Down Expand Up @@ -47,7 +45,7 @@ async def async_setup_platform(
device_instance: MCP2221.MCP2221() | None = hass.data[DOMAIN].get(
switch.get(CONF_DEVICE_ID))

if not isinstance(device_instance, MCP2221.MCP2221()):
if not isinstance(device_instance, MCP2221.MCP2221):
LOGGER.error("No instance of MCP2221")
return

Expand Down

0 comments on commit c987ca5

Please sign in to comment.