__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """
  2. Platform for Tuya WiFi-connected devices.
  3. Based on nikrolls/homeassistant-goldair-climate for Goldair branded devices.
  4. Based on sean6541/tuya-homeassistant for service call logic, and TarxBoy's
  5. investigation into Goldair's tuyapi statuses
  6. https://github.com/codetheweb/tuyapi/issues/31.
  7. """
  8. import logging
  9. import homeassistant.helpers.config_validation as cv
  10. import voluptuous as vol
  11. from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
  12. from homeassistant.core import HomeAssistant
  13. from .configuration import individual_config_schema
  14. from .const import (
  15. CONF_CHILD_LOCK,
  16. CONF_CLIMATE,
  17. CONF_DEVICE_ID,
  18. CONF_DISPLAY_LIGHT,
  19. CONF_FAN,
  20. CONF_HUMIDIFIER,
  21. CONF_SWITCH,
  22. DOMAIN,
  23. )
  24. from .device import setup_device, delete_device
  25. _LOGGER = logging.getLogger(__name__)
  26. CONFIG_SCHEMA = vol.Schema(
  27. {DOMAIN: vol.All(cv.ensure_list, [vol.Schema(individual_config_schema())])},
  28. extra=vol.ALLOW_EXTRA,
  29. )
  30. async def async_setup(hass: HomeAssistant, config: dict):
  31. for device_config in config.get(DOMAIN, []):
  32. hass.async_create_task(
  33. hass.config_entries.flow.async_init(
  34. DOMAIN, context={"source": SOURCE_IMPORT}, data=device_config
  35. )
  36. )
  37. return True
  38. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  39. _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")
  40. config = {**entry.data, **entry.options, "name": entry.title}
  41. setup_device(hass, config)
  42. if config.get(CONF_CLIMATE, False) is True:
  43. hass.async_create_task(
  44. hass.config_entries.async_forward_entry_setup(entry, "climate")
  45. )
  46. if config.get(CONF_DISPLAY_LIGHT, False) is True:
  47. hass.async_create_task(
  48. hass.config_entries.async_forward_entry_setup(entry, "light")
  49. )
  50. if config.get(CONF_CHILD_LOCK, False) is True:
  51. hass.async_create_task(
  52. hass.config_entries.async_forward_entry_setup(entry, "lock")
  53. )
  54. if config.get(CONF_SWITCH, False) is True:
  55. hass.async_create_task(
  56. hass.config_entries.async_forward_entry_setup(entry, "switch")
  57. )
  58. if config.get(CONF_HUMIDIFIER, False) is True:
  59. hass.async_create_task(
  60. hass.config_entries.async_forward_entry_setup(entry, "humidifier")
  61. )
  62. if config.get(CONF_FAN, False) is True:
  63. hass.async_create_task(
  64. hass.config_entries.async_forward_entry_setup(entry, "fan")
  65. )
  66. entry.add_update_listener(async_update_entry)
  67. return True
  68. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  69. if entry.data.get(SOURCE_IMPORT):
  70. raise ValueError("Devices configured via yaml cannot be deleted from the UI.")
  71. _LOGGER.debug(f"Unloading entry for device: {entry.data[CONF_DEVICE_ID]}")
  72. config = entry.data
  73. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  74. if CONF_CLIMATE in data:
  75. await hass.config_entries.async_forward_entry_unload(entry, "climate")
  76. if CONF_DISPLAY_LIGHT in data:
  77. await hass.config_entries.async_forward_entry_unload(entry, "light")
  78. if CONF_CHILD_LOCK in data:
  79. await hass.config_entries.async_forward_entry_unload(entry, "lock")
  80. if CONF_SWITCH in data:
  81. await hass.config_entries.async_forward_entry_unload(entry, "switch")
  82. if CONF_HUMIDIFIER in data:
  83. await hass.config_entries.async_forward_entry_unload(entry, "humidifier")
  84. if CONF_FAN in data:
  85. await hass.config_entries.async_forward_entry_unload(entry, "fan")
  86. delete_device(hass, config)
  87. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  88. return True
  89. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  90. if entry.data.get(SOURCE_IMPORT):
  91. raise ValueError("Devices configured via yaml cannot be updated from the UI.")
  92. _LOGGER.debug(f"Updating entry for device: {entry.data[CONF_DEVICE_ID]}")
  93. await async_unload_entry(hass, entry)
  94. await async_setup_entry(hass, entry)