switch.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. Setup for different kinds of Tuya switch devices
  3. """
  4. from . import DOMAIN
  5. from .const import (
  6. CONF_DEVICE_ID,
  7. CONF_TYPE,
  8. CONF_TYPE_KOGAN_SWITCH,
  9. CONF_TYPE_PURLINE_M100_HEATER,
  10. CONF_TYPE_AUTO,
  11. CONF_SWITCH,
  12. )
  13. from .kogan_socket.switch import KoganSocketSwitch
  14. from .purline_m100_heater.switch import PurlineM100OpenWindowDetector
  15. async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
  16. """Set up the switch device according to its type."""
  17. data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
  18. device = data["device"]
  19. if discovery_info[CONF_TYPE] == CONF_TYPE_AUTO:
  20. discovery_info[CONF_TYPE] = await device.async_inferred_type()
  21. if discovery_info[CONF_TYPE] is None:
  22. raise ValueError(f"Unable to detect type for device {device.name}")
  23. if discovery_info[CONF_TYPE] == CONF_TYPE_KOGAN_SWITCH:
  24. data[CONF_SWITCH] = KoganSocketSwitch(device)
  25. elif discovery_info[CONF_TYPE] == CONF_TYPE_PURLINE_M100_HEATER:
  26. data[CONF_SWITCH] = PurlineM100OpenWindowDetector(device)
  27. else:
  28. raise ValueError("This device does not support working as a switch")
  29. if CONF_SWITCH in data:
  30. async_add_entities([data[CONF_SWITCH]])
  31. async def async_setup_entry(hass, config_entry, async_add_entities):
  32. config = {**config_entry.data, **config_entry.options}
  33. discovery_info = {
  34. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  35. CONF_TYPE: config[CONF_TYPE],
  36. }
  37. await async_setup_platform(hass, {}, async_add_entities, discovery_info)