__init__.py 3.8 KB

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