Browse Source

prepare sensor entity for MCP3008's channels (dummy states)

Fabian Peter Hammerle 2 years ago
commit
352015fa19
3 changed files with 55 additions and 0 deletions
  1. 1 0
      __init__.py
  2. 6 0
      manifest.json
  3. 48 0
      sensor.py

+ 1 - 0
__init__.py

@@ -0,0 +1 @@
+""" MCP3008 """

+ 6 - 0
manifest.json

@@ -0,0 +1,6 @@
+{
+  "domain": "mcp3008",
+  "name": "MCP3008 Analog-to-Digital Converter",
+  "version": "0.1.0",
+  "requirements": ["adafruit-circuitpython-mcp3xxx==1.4.5"]
+}

+ 48 - 0
sensor.py

@@ -0,0 +1,48 @@
+import time  # TODO remove
+import typing
+
+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
+        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}"
+
+    @property
+    def unique_id(self) -> str:
+        return f"mcp3008-ch{self._channel_index}"
+
+    # https://developers.home-assistant.io/docs/core/entity/sensor/#properties
+
+    @property
+    def state(self) -> typing.Optional[float]:
+        return self._state
+
+    @property
+    def device_class(self) -> str:
+        return "voltage"
+
+    @property
+    def unit_of_measurement(self) -> str:
+        return homeassistant.const.VOLT
+
+    def update(self) -> None:
+        self._state = (time.time() / 60) % 60  # TODO
+
+
+def setup_platform(
+    hass: homeassistant.core.HomeAssistant,
+    config: dict,
+    add_entities: typing.Callable,
+    discovery_info=None,
+):
+    add_entities([MCP3008Channel(channel_index=0)])