__init__.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. vol.All(
  28. cv.deprecated(DOMAIN),
  29. {DOMAIN: vol.All(cv.ensure_list, [vol.Schema(individual_config_schema())])},
  30. ),
  31. extra=vol.ALLOW_EXTRA,
  32. )
  33. async def async_setup(hass: HomeAssistant, config: dict):
  34. for device_config in config.get(DOMAIN, []):
  35. hass.async_create_task(
  36. hass.config_entries.flow.async_init(
  37. DOMAIN, context={"source": SOURCE_IMPORT}, data=device_config
  38. )
  39. )
  40. return True
  41. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  42. _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")
  43. config = {**entry.data, **entry.options, "name": entry.title}
  44. setup_device(hass, config)
  45. if config.get(CONF_CLIMATE, False) is True:
  46. hass.async_create_task(
  47. hass.config_entries.async_forward_entry_setup(entry, "climate")
  48. )
  49. if config.get(CONF_DISPLAY_LIGHT, False) is True:
  50. hass.async_create_task(
  51. hass.config_entries.async_forward_entry_setup(entry, "light")
  52. )
  53. if config.get(CONF_CHILD_LOCK, False) is True:
  54. hass.async_create_task(
  55. hass.config_entries.async_forward_entry_setup(entry, "lock")
  56. )
  57. if config.get(CONF_SWITCH, False) is True:
  58. hass.async_create_task(
  59. hass.config_entries.async_forward_entry_setup(entry, "switch")
  60. )
  61. if config.get(CONF_HUMIDIFIER, False) is True:
  62. hass.async_create_task(
  63. hass.config_entries.async_forward_entry_setup(entry, "humidifier")
  64. )
  65. if config.get(CONF_FAN, False) is True:
  66. hass.async_create_task(
  67. hass.config_entries.async_forward_entry_setup(entry, "fan")
  68. )
  69. entry.add_update_listener(async_update_entry)
  70. return True
  71. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  72. if entry.data.get(SOURCE_IMPORT):
  73. raise ValueError("Devices configured via yaml cannot be deleted from the UI.")
  74. _LOGGER.debug(f"Unloading entry for device: {entry.data[CONF_DEVICE_ID]}")
  75. config = entry.data
  76. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  77. if CONF_CLIMATE in data:
  78. await hass.config_entries.async_forward_entry_unload(entry, "climate")
  79. if CONF_DISPLAY_LIGHT in data:
  80. await hass.config_entries.async_forward_entry_unload(entry, "light")
  81. if CONF_CHILD_LOCK in data:
  82. await hass.config_entries.async_forward_entry_unload(entry, "lock")
  83. if CONF_SWITCH in data:
  84. await hass.config_entries.async_forward_entry_unload(entry, "switch")
  85. if CONF_HUMIDIFIER in data:
  86. await hass.config_entries.async_forward_entry_unload(entry, "humidifier")
  87. if CONF_FAN in data:
  88. await hass.config_entries.async_forward_entry_unload(entry, "fan")
  89. delete_device(hass, config)
  90. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  91. return True
  92. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  93. if entry.data.get(SOURCE_IMPORT):
  94. raise ValueError("Devices configured via yaml cannot be updated from the UI.")
  95. _LOGGER.debug(f"Updating entry for device: {entry.data[CONF_DEVICE_ID]}")
  96. await async_unload_entry(hass, entry)
  97. await async_setup_entry(hass, entry)