switch.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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_AUTO,
  10. CONF_SWITCH,
  11. )
  12. from .kogan_socket.switch import KoganSocketSwitch
  13. async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
  14. """Set up the switch device according to its type."""
  15. data = hass.data[DOMAIN][discovery_info[CONF_DEVICE_ID]]
  16. device = data["device"]
  17. if discovery_info[CONF_TYPE] == CONF_TYPE_AUTO:
  18. discovery_info[CONF_TYPE] = await device.async_inferred_type()
  19. if discovery_info[CONF_TYPE] is None:
  20. raise ValueError(f"Unable to detect type for device {device.name}")
  21. if discovery_info[CONF_TYPE] == CONF_TYPE_KOGAN_SWITCH:
  22. data[CONF_SWITCH] = KoganSocketSwitch(device)
  23. else:
  24. raise ValueError("This device does not support working as a switch")
  25. if CONF_SWITCH in data:
  26. async_add_entities([data[CONF_SWITCH]])
  27. async def async_setup_entry(hass, config_entry, async_add_entities):
  28. config = {**config_entry.data, **config_entry.options}
  29. discovery_info = {
  30. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  31. CONF_TYPE: config[CONF_TYPE],
  32. }
  33. await async_setup_platform(hass, {}, async_add_entities, discovery_info)