__init__.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 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. if CONF_CHILD_LOCK in config:
  46. opts.pop(CONF_CHILD_LOCK, False)
  47. opts[CONF_LOCK] = config[CONF_CHILD_LOCK]
  48. if CONF_DISPLAY_LIGHT in config:
  49. opts.pop(CONF_DISPLAY_LIGHT, False)
  50. opts[CONF_LIGHT] = config[CONF_DISPLAY_LIGHT]
  51. entry.options = {**opts}
  52. entry.version = 2
  53. if entry.version == 2:
  54. # CONF_TYPE is not configurable, move it from options to the main config.
  55. config = {**entry.data, **entry.options, "name": entry.title}
  56. opts = {**entry.options}
  57. # Ensure type has been migrated. Some users are reporting errors which
  58. # suggest it was removed completely. But that is probably due to
  59. # overwriting options without CONF_TYPE.
  60. if config.get(CONF_TYPE, CONF_TYPE_AUTO) == CONF_TYPE_AUTO:
  61. device = setup_device(hass, config)
  62. config[CONF_TYPE] = await device.async_inferred_type()
  63. if config[CONF_TYPE] is None:
  64. return False
  65. entry.data = {
  66. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  67. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  68. CONF_HOST: config[CONF_HOST],
  69. CONF_TYPE: config[CONF_TYPE],
  70. }
  71. opts.pop(CONF_TYPE, None)
  72. entry.options = {**opts}
  73. entry.version = 3
  74. return True
  75. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  76. _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")
  77. config = {**entry.data, **entry.options, "name": entry.title}
  78. setup_device(hass, config)
  79. if config.get(CONF_CLIMATE, False) is True:
  80. hass.async_create_task(
  81. hass.config_entries.async_forward_entry_setup(entry, "climate")
  82. )
  83. if config.get(CONF_LIGHT, False) is True:
  84. hass.async_create_task(
  85. hass.config_entries.async_forward_entry_setup(entry, "light")
  86. )
  87. if config.get(CONF_LOCK, False) is True:
  88. hass.async_create_task(
  89. hass.config_entries.async_forward_entry_setup(entry, "lock")
  90. )
  91. if config.get(CONF_SWITCH, False) is True:
  92. hass.async_create_task(
  93. hass.config_entries.async_forward_entry_setup(entry, "switch")
  94. )
  95. if config.get(CONF_HUMIDIFIER, False) is True:
  96. hass.async_create_task(
  97. hass.config_entries.async_forward_entry_setup(entry, "humidifier")
  98. )
  99. if config.get(CONF_FAN, False) is True:
  100. hass.async_create_task(
  101. hass.config_entries.async_forward_entry_setup(entry, "fan")
  102. )
  103. entry.add_update_listener(async_update_entry)
  104. return True
  105. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  106. _LOGGER.debug(f"Unloading entry for device: {entry.data[CONF_DEVICE_ID]}")
  107. config = entry.data
  108. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  109. if CONF_CLIMATE in data:
  110. await hass.config_entries.async_forward_entry_unload(entry, "climate")
  111. if CONF_LIGHT in data:
  112. await hass.config_entries.async_forward_entry_unload(entry, "light")
  113. if CONF_LOCK in data:
  114. await hass.config_entries.async_forward_entry_unload(entry, "lock")
  115. if CONF_SWITCH in data:
  116. await hass.config_entries.async_forward_entry_unload(entry, "switch")
  117. if CONF_HUMIDIFIER in data:
  118. await hass.config_entries.async_forward_entry_unload(entry, "humidifier")
  119. if CONF_FAN in data:
  120. await hass.config_entries.async_forward_entry_unload(entry, "fan")
  121. delete_device(hass, config)
  122. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  123. return True
  124. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  125. _LOGGER.debug(f"Updating entry for device: {entry.data[CONF_DEVICE_ID]}")
  126. await async_unload_entry(hass, entry)
  127. await async_setup_entry(hass, entry)