__init__.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.const import CONF_HOST, CONF_NAME
  13. from homeassistant.core import HomeAssistant
  14. from homeassistant.helpers.discovery import async_load_platform
  15. from .configuration import individual_config_schema
  16. from .const import (
  17. CONF_CHILD_LOCK,
  18. CONF_CLIMATE,
  19. CONF_DEVICE_ID,
  20. CONF_DISPLAY_LIGHT,
  21. CONF_LOCAL_KEY,
  22. CONF_SWITCH,
  23. DOMAIN,
  24. )
  25. _LOGGER = logging.getLogger(__name__)
  26. CONFIG_SCHEMA = vol.Schema(
  27. {DOMAIN: vol.All(cv.ensure_list, [vol.Schema(individual_config_schema())])},
  28. extra=vol.ALLOW_EXTRA,
  29. )
  30. async def async_setup(hass: HomeAssistant, config: dict):
  31. for device_config in config.get(DOMAIN, []):
  32. hass.async_create_task(
  33. hass.config_entries.flow.async_init(
  34. DOMAIN, context={"source": SOURCE_IMPORT}, data=device_config
  35. )
  36. )
  37. return True
  38. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  39. _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")
  40. config = {**entry.data, **entry.options, "name": entry.title}
  41. setup_device(hass, config)
  42. if config[CONF_CLIMATE] is True:
  43. hass.async_create_task(
  44. hass.config_entries.async_forward_entry_setup(entry, "climate")
  45. )
  46. if config[CONF_DISPLAY_LIGHT] is True:
  47. hass.async_create_task(
  48. hass.config_entries.async_forward_entry_setup(entry, "light")
  49. )
  50. if config[CONF_CHILD_LOCK] is True:
  51. hass.async_create_task(
  52. hass.config_entries.async_forward_entry_setup(entry, "lock")
  53. )
  54. if config[CONF_SWITCH] is True:
  55. hass.async_create_task(
  56. hass.config_entries.async_forward_entry_setup(entry, "switch")
  57. )
  58. entry.add_update_listener(async_update_entry)
  59. return True
  60. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  61. if entry.data.get(SOURCE_IMPORT):
  62. raise ValueError("Devices configured via yaml cannot be deleted from the UI.")
  63. _LOGGER.debug(f"Unloading entry for device: {entry.data[CONF_DEVICE_ID]}")
  64. config = entry.data
  65. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  66. if CONF_CLIMATE in data:
  67. await hass.config_entries.async_forward_entry_unload(entry, "climate")
  68. if CONF_DISPLAY_LIGHT in data:
  69. await hass.config_entries.async_forward_entry_unload(entry, "light")
  70. if CONF_CHILD_LOCK in data:
  71. await hass.config_entries.async_forward_entry_unload(entry, "lock")
  72. if CONF_SWITCH in data:
  73. await hass.config_entries.async_forward_entry_unload(entry, "switch")
  74. delete_device(hass, config)
  75. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  76. return True
  77. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  78. if entry.data.get(SOURCE_IMPORT):
  79. raise ValueError("Devices configured via yaml cannot be updated from the UI.")
  80. _LOGGER.debug(f"Updating entry for device: {entry.data[CONF_DEVICE_ID]}")
  81. await async_unload_entry(hass, entry)
  82. await async_setup_entry(hass, entry)
  83. def setup_device(hass: HomeAssistant, config: dict):
  84. """Setup a tuya device based on passed in config."""
  85. from .device import TuyaLocalDevice
  86. _LOGGER.debug(f"Creating device: {config[CONF_DEVICE_ID]}")
  87. hass.data[DOMAIN] = hass.data.get(DOMAIN, {})
  88. device = TuyaLocalDevice(
  89. config[CONF_NAME],
  90. config[CONF_DEVICE_ID],
  91. config[CONF_HOST],
  92. config[CONF_LOCAL_KEY],
  93. hass,
  94. )
  95. hass.data[DOMAIN][config[CONF_DEVICE_ID]] = {"device": device}
  96. return device
  97. def delete_device(hass: HomeAssistant, config: dict):
  98. _LOGGER.debug(f"Deleting device: {config[CONF_DEVICE_ID]}")
  99. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]["device"]