__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. Platform for Goldair WiFi-connected heaters, dehumidifiers and fans.
  3. Based on sean6541/tuya-homeassistant for service call logic, and TarxBoy's
  4. investigation into Goldair's tuyapi statuses
  5. https://github.com/codetheweb/tuyapi/issues/31.
  6. """
  7. import logging
  8. import homeassistant.helpers.config_validation as cv
  9. import voluptuous as vol
  10. from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
  11. from homeassistant.const import CONF_HOST, CONF_NAME
  12. from homeassistant.core import HomeAssistant
  13. from homeassistant.helpers.discovery import async_load_platform
  14. from .configuration import individual_config_schema
  15. from .const import (
  16. CONF_CHILD_LOCK,
  17. CONF_CLIMATE,
  18. CONF_DEVICE_ID,
  19. CONF_DISPLAY_LIGHT,
  20. CONF_LOCAL_KEY,
  21. CONF_TYPE,
  22. CONF_TYPE_AUTO,
  23. CONF_TYPE_DEHUMIDIFIER,
  24. CONF_TYPE_FAN,
  25. CONF_TYPE_GPPH_HEATER,
  26. DOMAIN,
  27. SCAN_INTERVAL,
  28. )
  29. from .device import GoldairTuyaDevice
  30. _LOGGER = logging.getLogger(__name__)
  31. VERSION = "0.1.2"
  32. CONFIG_SCHEMA = vol.Schema(
  33. {DOMAIN: vol.All(cv.ensure_list, [vol.Schema(individual_config_schema())])},
  34. extra=vol.ALLOW_EXTRA,
  35. )
  36. async def async_setup(hass: HomeAssistant, config: dict):
  37. for device_config in config.get(DOMAIN, []):
  38. hass.async_create_task(
  39. hass.config_entries.flow.async_init(
  40. DOMAIN, context={"source": SOURCE_IMPORT}, data=device_config
  41. )
  42. )
  43. return True
  44. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  45. _LOGGER.debug(f"Setting up entry for device: {entry.data[CONF_DEVICE_ID]}")
  46. config = {**entry.data, **entry.options, "name": entry.title}
  47. setup_device(hass, config)
  48. if config[CONF_CLIMATE] == True:
  49. hass.async_create_task(
  50. hass.config_entries.async_forward_entry_setup(entry, "climate")
  51. )
  52. if config[CONF_DISPLAY_LIGHT] == True:
  53. hass.async_create_task(
  54. hass.config_entries.async_forward_entry_setup(entry, "light")
  55. )
  56. if config[CONF_CHILD_LOCK] == True:
  57. hass.async_create_task(
  58. hass.config_entries.async_forward_entry_setup(entry, "lock")
  59. )
  60. entry.add_update_listener(async_update_entry)
  61. return True
  62. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  63. if entry.data.get(SOURCE_IMPORT):
  64. raise ValueError("Devices configured via yaml cannot be deleted from the UI.")
  65. _LOGGER.debug(f"Unloading entry for device: {entry.data[CONF_DEVICE_ID]}")
  66. config = entry.data
  67. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  68. if CONF_CLIMATE in data:
  69. await hass.config_entries.async_forward_entry_unload(entry, "climate")
  70. if CONF_DISPLAY_LIGHT in data:
  71. await hass.config_entries.async_forward_entry_unload(entry, "light")
  72. if CONF_CHILD_LOCK in data:
  73. await hass.config_entries.async_forward_entry_unload(entry, "lock")
  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. _LOGGER.debug(f"Creating device: {config[CONF_DEVICE_ID]}")
  85. hass.data[DOMAIN] = hass.data.get(DOMAIN, {})
  86. device = GoldairTuyaDevice(
  87. config[CONF_NAME],
  88. config[CONF_DEVICE_ID],
  89. config[CONF_HOST],
  90. config[CONF_LOCAL_KEY],
  91. hass,
  92. )
  93. hass.data[DOMAIN][config[CONF_DEVICE_ID]] = {"device": device}
  94. return device
  95. def delete_device(hass: HomeAssistant, config: dict):
  96. _LOGGER.debug(f"Deleting device: {config[CONF_DEVICE_ID]}")
  97. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]["device"]