__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 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 (DOMAIN, CONF_CHILD_LOCK, CONF_CLIMATE, CONF_DEVICE_ID,
  17. CONF_DISPLAY_LIGHT, CONF_LOCAL_KEY, CONF_TYPE,
  18. CONF_TYPE_DEHUMIDIFIER, CONF_TYPE_FAN, CONF_TYPE_HEATER, SCAN_INTERVAL, CONF_TYPE_AUTO)
  19. from .device import TuyaLocalDevice
  20. _LOGGER = logging.getLogger(__name__)
  21. VERSION = "0.0.8"
  22. CONFIG_SCHEMA = vol.Schema(
  23. {DOMAIN: vol.All(cv.ensure_list, [vol.Schema(individual_config_schema())])},
  24. extra=vol.ALLOW_EXTRA,
  25. )
  26. async def async_setup(hass: HomeAssistant, config: dict):
  27. hass.data[DOMAIN] = {}
  28. for device_config in config.get(DOMAIN, []):
  29. setup_device(hass, device_config)
  30. discovery_info = {
  31. CONF_DEVICE_ID: device_config[CONF_DEVICE_ID],
  32. CONF_TYPE: device_config[CONF_TYPE],
  33. }
  34. if device_config[CONF_CLIMATE] == True:
  35. hass.async_create_task(
  36. async_load_platform(hass, "climate", DOMAIN, discovery_info, config)
  37. )
  38. if device_config[CONF_DISPLAY_LIGHT] == True:
  39. hass.async_create_task(
  40. async_load_platform(hass, "light", DOMAIN, discovery_info, config)
  41. )
  42. if device_config[CONF_CHILD_LOCK] == True:
  43. hass.async_create_task(
  44. async_load_platform(hass, "lock", DOMAIN, discovery_info, config)
  45. )
  46. return True
  47. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  48. config = {**entry.data, **entry.options, 'name': entry.title}
  49. setup_device(hass, config)
  50. if config[CONF_CLIMATE] == True:
  51. hass.async_create_task(
  52. hass.config_entries.async_forward_entry_setup(entry, "climate")
  53. )
  54. if config[CONF_DISPLAY_LIGHT] == True:
  55. hass.async_create_task(
  56. hass.config_entries.async_forward_entry_setup(entry, "light")
  57. )
  58. if config[CONF_CHILD_LOCK] == True:
  59. hass.async_create_task(
  60. hass.config_entries.async_forward_entry_setup(entry, "lock")
  61. )
  62. entry.add_update_listener(async_update_entry)
  63. return True
  64. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  65. config = entry.data
  66. data = hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  67. if CONF_CLIMATE in data:
  68. await hass.config_entries.async_forward_entry_unload(entry, "climate")
  69. if CONF_DISPLAY_LIGHT in data:
  70. await hass.config_entries.async_forward_entry_unload(entry, "light")
  71. if CONF_CHILD_LOCK in data:
  72. await hass.config_entries.async_forward_entry_unload(entry, "lock")
  73. delete_device(hass, config)
  74. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]
  75. return True
  76. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  77. await async_unload_entry(hass, entry)
  78. await async_setup_entry(hass, entry)
  79. def setup_device(hass: HomeAssistant, config: dict):
  80. device = TuyaLocalDevice(
  81. config[CONF_NAME],
  82. config[CONF_DEVICE_ID],
  83. config[CONF_HOST],
  84. config[CONF_LOCAL_KEY],
  85. hass,
  86. )
  87. hass.data[DOMAIN][config[CONF_DEVICE_ID]] = {
  88. 'device': device
  89. }
  90. return device
  91. def delete_device(hass: HomeAssistant, config: dict):
  92. del hass.data[DOMAIN][config[CONF_DEVICE_ID]]['device']