sensor.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import time # TODO remove
  2. import typing
  3. import homeassistant.const
  4. import homeassistant.core
  5. import homeassistant.helpers.entity
  6. class MCP3008Channel(homeassistant.helpers.entity.Entity):
  7. def __init__(self, channel_index: int) -> None:
  8. self._channel_index = channel_index
  9. self._state: typing.Optional[float] = None
  10. # https://developers.home-assistant.io/docs/core/entity/#generic-properties
  11. @property
  12. def name(self) -> str:
  13. return f"MCP3008 Channel #{self._channel_index}"
  14. @property
  15. def unique_id(self) -> str:
  16. return f"mcp3008-ch{self._channel_index}"
  17. # https://developers.home-assistant.io/docs/core/entity/sensor/#properties
  18. @property
  19. def state(self) -> typing.Optional[float]:
  20. return self._state
  21. @property
  22. def device_class(self) -> str:
  23. return "voltage"
  24. @property
  25. def unit_of_measurement(self) -> str:
  26. return homeassistant.const.VOLT
  27. def update(self) -> None:
  28. self._state = (time.time() / 60) % 60 # TODO
  29. def setup_platform(
  30. hass: homeassistant.core.HomeAssistant,
  31. config: dict,
  32. add_entities: typing.Callable,
  33. discovery_info=None,
  34. ):
  35. add_entities([MCP3008Channel(channel_index=0)])