__init__.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
  10. from homeassistant.const import CONF_HOST
  11. from homeassistant.core import HomeAssistant
  12. from .const import (
  13. CONF_CLIMATE,
  14. CONF_DEVICE_ID,
  15. CONF_FAN,
  16. CONF_HUMIDIFIER,
  17. CONF_LIGHT,
  18. CONF_LOCAL_KEY,
  19. CONF_LOCK,
  20. CONF_SWITCH,
  21. CONF_TYPE,
  22. DOMAIN,
  23. )
  24. from .device import setup_device, delete_device
  25. _LOGGER = logging.getLogger(__name__)
  26. async def async_migrate_entry(hass, entry: ConfigEntry):
  27. """Migrate to latest config format."""
  28. CONF_TYPE_AUTO = "auto"
  29. CONF_DISPLAY_LIGHT = "display_light"
  30. CONF_CHILD_LOCK = "child_lock"
  31. if entry.version == 1:
  32. # Removal of Auto detection.
  33. config = {**entry.data, **entry.options, "name": entry.title}
  34. opts = {**entry.options}
  35. if config[CONF_TYPE] == CONF_TYPE_AUTO:
  36. device = setup_device(hass, config)
  37. config[CONF_TYPE] = await device.async_inferred_type()
  38. if config[CONF_TYPE] is None:
  39. return False
  40. entry.data = {
  41. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  42. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  43. CONF_HOST: config[CONF_HOST],
  44. }
  45. opts[CONF_TYPE] = config[CONF_TYPE]
  46. if CONF_CHILD_LOCK in config:
  47. opts.pop(CONF_CHILD_LOCK)
  48. opts[CONF_LOCK] = config[CONF_CHILD_LOCK]
  49. if CONF_DISPLAY_LIGHT in config:
  50. opts.pop(CONF_DISPLAY_LIGHT)
  51. opts[CONF_LIGHT] = config[CONF_DISPLAY_LIGHT]
  52. entry.options = {**opts}
  53. entry.version = 2
  54. return True
  55. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  56. _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")
  57. config = {**entry.data, **entry.options, "name": entry.title}
  58. device = setup_device(hass, config)
  59. if config.get(CONF_CLIMATE, False) is True:
  60. hass.async_create_task(
  61. hass.config_entries.async_forward_entry_setup(entry, "climate")
  62. )
  63. if config.get(CONF_LIGHT, False) is True:
  64. hass.async_create_task(
  65. hass.config_entries.async_forward_entry_setup(entry, "light")
  66. )
  67. if config.get(CONF_LOCK, False) is True:
  68. hass.async_create_task(
  69. hass.config_entries.async_forward_entry_setup(entry, "lock")
  70. )
  71. if config.get(CONF_SWITCH, False) is True:
  72. hass.async_create_task(
  73. hass.config_entries.async_forward_entry_setup(entry, "switch")
  74. )
  75. if config.get(CONF_HUMIDIFIER, False) is True:
  76. hass.async_create_task(
  77. hass.config_entries.async_forward_entry_setup(entry, "humidifier")
  78. )
  79. if config.get(CONF_FAN, False) is True:
  80. hass.async_create_task(
  81. hass.config_entries.async_forward_entry_setup(entry, "fan")
  82. )
  83. entry.add_update_listener(async_update_entry)
  84. return True
  85. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  86. if entry.data.get(SOURCE_IMPORT):
  87. raise ValueError("Devices configured via yaml cannot be deleted from the UI.")
  88. _LOGGER.debug(f"Unloading entry for device: {entry.data[CONF_DEVICE_ID]}")
  89. config = entry.data
  90. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  91. if CONF_CLIMATE in data:
  92. await hass.config_entries.async_forward_entry_unload(entry, "climate")
  93. if CONF_LIGHT in data:
  94. await hass.config_entries.async_forward_entry_unload(entry, "light")
  95. if CONF_LOCK in data:
  96. await hass.config_entries.async_forward_entry_unload(entry, "lock")
  97. if CONF_SWITCH in data:
  98. await hass.config_entries.async_forward_entry_unload(entry, "switch")
  99. if CONF_HUMIDIFIER in data:
  100. await hass.config_entries.async_forward_entry_unload(entry, "humidifier")
  101. if CONF_FAN in data:
  102. await hass.config_entries.async_forward_entry_unload(entry, "fan")
  103. delete_device(hass, config)
  104. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  105. return True
  106. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  107. _LOGGER.debug(f"Updating entry for device: {entry.data[CONF_DEVICE_ID]}")
  108. await async_unload_entry(hass, entry)
  109. await async_setup_entry(hass, entry)