4
0

__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_SWITCH,
  20. DOMAIN,
  21. )
  22. from .device import setup_device, delete_device
  23. _LOGGER = logging.getLogger(__name__)
  24. CONFIG_SCHEMA = vol.Schema(
  25. {DOMAIN: vol.All(cv.ensure_list, [vol.Schema(individual_config_schema())])},
  26. extra=vol.ALLOW_EXTRA,
  27. )
  28. async def async_setup(hass: HomeAssistant, config: dict):
  29. for device_config in config.get(DOMAIN, []):
  30. hass.async_create_task(
  31. hass.config_entries.flow.async_init(
  32. DOMAIN, context={"source": SOURCE_IMPORT}, data=device_config
  33. )
  34. )
  35. return True
  36. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  37. _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")
  38. config = {**entry.data, **entry.options, "name": entry.title}
  39. setup_device(hass, config)
  40. if config[CONF_CLIMATE] is True:
  41. hass.async_create_task(
  42. hass.config_entries.async_forward_entry_setup(entry, "climate")
  43. )
  44. if config[CONF_DISPLAY_LIGHT] is True:
  45. hass.async_create_task(
  46. hass.config_entries.async_forward_entry_setup(entry, "light")
  47. )
  48. if config[CONF_CHILD_LOCK] is True:
  49. hass.async_create_task(
  50. hass.config_entries.async_forward_entry_setup(entry, "lock")
  51. )
  52. if config[CONF_SWITCH] is True:
  53. hass.async_create_task(
  54. hass.config_entries.async_forward_entry_setup(entry, "switch")
  55. )
  56. entry.add_update_listener(async_update_entry)
  57. return True
  58. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  59. if entry.data.get(SOURCE_IMPORT):
  60. raise ValueError("Devices configured via yaml cannot be deleted from the UI.")
  61. _LOGGER.debug(f"Unloading entry for device: {entry.data[CONF_DEVICE_ID]}")
  62. config = entry.data
  63. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  64. if CONF_CLIMATE in data:
  65. await hass.config_entries.async_forward_entry_unload(entry, "climate")
  66. if CONF_DISPLAY_LIGHT in data:
  67. await hass.config_entries.async_forward_entry_unload(entry, "light")
  68. if CONF_CHILD_LOCK in data:
  69. await hass.config_entries.async_forward_entry_unload(entry, "lock")
  70. if CONF_SWITCH in data:
  71. await hass.config_entries.async_forward_entry_unload(entry, "switch")
  72. delete_device(hass, config)
  73. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  74. return True
  75. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  76. if entry.data.get(SOURCE_IMPORT):
  77. raise ValueError("Devices configured via yaml cannot be updated from the UI.")
  78. _LOGGER.debug(f"Updating entry for device: {entry.data[CONF_DEVICE_ID]}")
  79. await async_unload_entry(hass, entry)
  80. await async_setup_entry(hass, entry)