|
@@ -1,25 +1,31 @@
|
|
|
-import time # TODO remove
|
|
|
import typing
|
|
|
|
|
|
+# circuitpython
|
|
|
+import busio
|
|
|
+import digitalio
|
|
|
+import adafruit_mcp3xxx.analog_in
|
|
|
+import adafruit_mcp3xxx.mcp3008
|
|
|
+
|
|
|
import homeassistant.const
|
|
|
import homeassistant.core
|
|
|
import homeassistant.helpers.entity
|
|
|
|
|
|
|
|
|
class MCP3008Channel(homeassistant.helpers.entity.Entity):
|
|
|
- def __init__(self, channel_index: int) -> None:
|
|
|
- self._channel_index = channel_index
|
|
|
+ def __init__(self, adc: adafruit_mcp3xxx.mcp3008.MCP3008, channel: int) -> None:
|
|
|
+ self._input = adafruit_mcp3xxx.analog_in.AnalogIn(adc, channel)
|
|
|
+ self._channel = channel
|
|
|
self._state: typing.Optional[float] = None
|
|
|
|
|
|
# https://developers.home-assistant.io/docs/core/entity/#generic-properties
|
|
|
|
|
|
@property
|
|
|
def name(self) -> str:
|
|
|
- return f"MCP3008 Channel #{self._channel_index}"
|
|
|
+ return f"MCP3008 Channel #{self._channel}"
|
|
|
|
|
|
@property
|
|
|
def unique_id(self) -> str:
|
|
|
- return f"mcp3008-ch{self._channel_index}"
|
|
|
+ return f"mcp3008-ch{self._channel}"
|
|
|
|
|
|
# https://developers.home-assistant.io/docs/core/entity/sensor/#properties
|
|
|
|
|
@@ -36,7 +42,7 @@ class MCP3008Channel(homeassistant.helpers.entity.Entity):
|
|
|
return homeassistant.const.VOLT
|
|
|
|
|
|
def update(self) -> None:
|
|
|
- self._state = (time.time() / 60) % 60 # TODO
|
|
|
+ self._state = self._input.voltage
|
|
|
|
|
|
|
|
|
def setup_platform(
|
|
@@ -45,4 +51,24 @@ def setup_platform(
|
|
|
add_entities: typing.Callable,
|
|
|
discovery_info=None,
|
|
|
):
|
|
|
- add_entities([MCP3008Channel(channel_index=0)])
|
|
|
+ board = __import__("board") # raises NotImplementedError
|
|
|
+ spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
|
|
|
+ spi_chip_select = digitalio.DigitalInOut(board.D5)
|
|
|
+ adc = adafruit_mcp3xxx.mcp3008.MCP3008(
|
|
|
+ spi_bus=spi, cs=spi_chip_select, ref_voltage=3.3
|
|
|
+ )
|
|
|
+ add_entities(
|
|
|
+ [
|
|
|
+ MCP3008Channel(adc=adc, channel=c)
|
|
|
+ for c in [
|
|
|
+ adafruit_mcp3xxx.mcp3008.P0,
|
|
|
+ adafruit_mcp3xxx.mcp3008.P1,
|
|
|
+ adafruit_mcp3xxx.mcp3008.P2,
|
|
|
+ adafruit_mcp3xxx.mcp3008.P3,
|
|
|
+ adafruit_mcp3xxx.mcp3008.P4,
|
|
|
+ adafruit_mcp3xxx.mcp3008.P5,
|
|
|
+ adafruit_mcp3xxx.mcp3008.P6,
|
|
|
+ adafruit_mcp3xxx.mcp3008.P7,
|
|
|
+ ]
|
|
|
+ ]
|
|
|
+ )
|