4
0

__init__.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. if config[CONF_TYPE] == CONF_TYPE_AUTO:
  35. device = setup_device(hass, config)
  36. config[CONF_TYPE] = await device.async_inferred_type()
  37. entry.data = {
  38. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  39. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  40. CONF_HOST: config[CONF_HOST],
  41. }
  42. opts = {**entry.options}
  43. opts[CONF_TYPE] = config[CONF_TYPE]
  44. if CONF_CHILD_LOCK in config:
  45. opts.pop(CONF_CHILD_LOCK)
  46. opts[CONF_LOCK] = config[CONF_CHILD_LOCK]
  47. if CONF_DISPLAY_LIGHT in config:
  48. opts.pop(CONF_DISPLAY_LIGHT)
  49. opts[CONF_LIGHT] = config[CONF_DISPLAY_LIGHT]
  50. entry.options = {**opts}
  51. entry.version = 2
  52. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  53. _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")
  54. config = {**entry.data, **entry.options, "name": entry.title}
  55. device = setup_device(hass, config)
  56. if config.get(CONF_CLIMATE, False) is True:
  57. hass.async_create_task(
  58. hass.config_entries.async_forward_entry_setup(entry, "climate")
  59. )
  60. if config.get(CONF_LIGHT, False) is True:
  61. hass.async_create_task(
  62. hass.config_entries.async_forward_entry_setup(entry, "light")
  63. )
  64. if config.get(CONF_LOCK, False) is True:
  65. hass.async_create_task(
  66. hass.config_entries.async_forward_entry_setup(entry, "lock")
  67. )
  68. if config.get(CONF_SWITCH, False) is True:
  69. hass.async_create_task(
  70. hass.config_entries.async_forward_entry_setup(entry, "switch")
  71. )
  72. if config.get(CONF_HUMIDIFIER, False) is True:
  73. hass.async_create_task(
  74. hass.config_entries.async_forward_entry_setup(entry, "humidifier")
  75. )
  76. if config.get(CONF_FAN, False) is True:
  77. hass.async_create_task(
  78. hass.config_entries.async_forward_entry_setup(entry, "fan")
  79. )
  80. entry.add_update_listener(async_update_entry)
  81. return True
  82. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  83. if entry.data.get(SOURCE_IMPORT):
  84. raise ValueError("Devices configured via yaml cannot be deleted from the UI.")
  85. _LOGGER.debug(f"Unloading entry for device: {entry.data[CONF_DEVICE_ID]}")
  86. config = entry.data
  87. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  88. if CONF_CLIMATE in data:
  89. await hass.config_entries.async_forward_entry_unload(entry, "climate")
  90. if CONF_LIGHT in data:
  91. await hass.config_entries.async_forward_entry_unload(entry, "light")
  92. if CONF_LOCK in data:
  93. await hass.config_entries.async_forward_entry_unload(entry, "lock")
  94. if CONF_SWITCH in data:
  95. await hass.config_entries.async_forward_entry_unload(entry, "switch")
  96. if CONF_HUMIDIFIER in data:
  97. await hass.config_entries.async_forward_entry_unload(entry, "humidifier")
  98. if CONF_FAN in data:
  99. await hass.config_entries.async_forward_entry_unload(entry, "fan")
  100. delete_device(hass, config)
  101. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  102. return True
  103. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  104. _LOGGER.debug(f"Updating entry for device: {entry.data[CONF_DEVICE_ID]}")
  105. await async_unload_entry(hass, entry)
  106. await async_setup_entry(hass, entry)