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