__init__.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. from homeassistant.config_entries import ConfigEntry
  10. from homeassistant.const import CONF_HOST
  11. from homeassistant.core import HomeAssistant, callback
  12. from homeassistant.exceptions import ConfigEntryNotReady
  13. from homeassistant.helpers.entity_registry import async_migrate_entries
  14. from homeassistant.util import slugify
  15. from .const import (
  16. CONF_DEVICE_ID,
  17. CONF_LOCAL_KEY,
  18. CONF_POLL_ONLY,
  19. CONF_PROTOCOL_VERSION,
  20. CONF_TYPE,
  21. DOMAIN,
  22. )
  23. from .device import async_delete_device, get_device_id, setup_device
  24. from .helpers.device_config import get_config
  25. _LOGGER = logging.getLogger(__name__)
  26. NOT_FOUND = "Configuration file for %s not found"
  27. def replace_unique_ids(entity_entry, device_id, conf_file, replacements):
  28. """Update the unique id of an entry based on a table of replacements."""
  29. old_id = entity_entry.unique_id
  30. platform = entity_entry.entity_id.split(".", 1)[0]
  31. for suffix, new_suffix in replacements.items():
  32. if old_id.endswith(suffix):
  33. for e in conf_file.all_entities():
  34. new_id = e.unique_id(device_id)
  35. if e.entity == platform and not e.name and new_id.endswith(new_suffix):
  36. break
  37. if e.entity == platform and not e.name and new_id.endswith(new_suffix):
  38. _LOGGER.info(
  39. "Migrating %s unique_id %s to %s",
  40. e.entity,
  41. old_id,
  42. new_id,
  43. )
  44. return {
  45. "new_unique_id": entity_entry.unique_id.replace(
  46. old_id,
  47. new_id,
  48. )
  49. }
  50. async def async_migrate_entry(hass, entry: ConfigEntry):
  51. """Migrate to latest config format."""
  52. CONF_TYPE_AUTO = "auto"
  53. if entry.version == 1:
  54. # Removal of Auto detection.
  55. config = {**entry.data, **entry.options, "name": entry.title}
  56. if config[CONF_TYPE] == CONF_TYPE_AUTO:
  57. device = await hass.async_add_executor_job(
  58. setup_device,
  59. hass,
  60. config,
  61. )
  62. config[CONF_TYPE] = await device.async_inferred_type()
  63. if config[CONF_TYPE] is None:
  64. _LOGGER.error(
  65. "Unable to determine type for device %s",
  66. config[CONF_DEVICE_ID],
  67. )
  68. return False
  69. hass.config_entries.async_update_entry(
  70. entry,
  71. data={
  72. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  73. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  74. CONF_HOST: config[CONF_HOST],
  75. },
  76. version=2,
  77. )
  78. if entry.version == 2:
  79. # CONF_TYPE is not configurable, move from options to main config.
  80. config = {**entry.data, **entry.options, "name": entry.title}
  81. opts = {**entry.options}
  82. # Ensure type has been migrated. Some users are reporting errors which
  83. # suggest it was removed completely. But that is probably due to
  84. # overwriting options without CONF_TYPE.
  85. if config.get(CONF_TYPE, CONF_TYPE_AUTO) == CONF_TYPE_AUTO:
  86. device = await hass.async_add_executor_job(
  87. setup_device,
  88. hass,
  89. config,
  90. )
  91. config[CONF_TYPE] = await device.async_inferred_type()
  92. if config[CONF_TYPE] is None:
  93. _LOGGER.error(
  94. "Unable to determine type for device %s",
  95. config[CONF_DEVICE_ID],
  96. )
  97. return False
  98. opts.pop(CONF_TYPE, None)
  99. hass.config_entries.async_update_entry(
  100. entry,
  101. data={
  102. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  103. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  104. CONF_HOST: config[CONF_HOST],
  105. CONF_TYPE: config[CONF_TYPE],
  106. },
  107. options={**opts},
  108. version=3,
  109. )
  110. if entry.version == 3:
  111. # Migrate to filename based config_type, to avoid needing to
  112. # parse config files to find the right one.
  113. config = {**entry.data, **entry.options, "name": entry.title}
  114. config_yaml = await hass.async_add_executor_job(
  115. get_config,
  116. config[CONF_TYPE],
  117. )
  118. config_type = config_yaml.config_type
  119. # Special case for kogan_switch. Consider also v2.
  120. if config_type == "smartplugv1":
  121. device = await hass.async_add_executor_job(
  122. setup_device,
  123. hass,
  124. config,
  125. )
  126. config_type = await device.async_inferred_type()
  127. if config_type != "smartplugv2":
  128. config_type = "smartplugv1"
  129. hass.config_entries.async_update_entry(
  130. entry,
  131. data={
  132. CONF_DEVICE_ID: config[CONF_DEVICE_ID],
  133. CONF_LOCAL_KEY: config[CONF_LOCAL_KEY],
  134. CONF_HOST: config[CONF_HOST],
  135. CONF_TYPE: config_type,
  136. },
  137. version=4,
  138. )
  139. if entry.version <= 5:
  140. # Migrate unique ids of existing entities to new format
  141. old_id = entry.unique_id
  142. conf_file = await hass.async_add_executor_job(
  143. get_config,
  144. entry.data[CONF_TYPE],
  145. )
  146. if conf_file is None:
  147. _LOGGER.error(NOT_FOUND, entry.data[CONF_TYPE])
  148. return False
  149. @callback
  150. def update_unique_id(entity_entry):
  151. """Update the unique id of an entity entry."""
  152. e = conf_file.primary_entity
  153. if e.entity != entity_entry.platform:
  154. for e in conf_file.secondary_entities():
  155. if e.entity == entity_entry.platform:
  156. break
  157. if e.entity == entity_entry.platform:
  158. new_id = e.unique_id(old_id)
  159. if new_id != old_id:
  160. _LOGGER.info(
  161. "Migrating %s unique_id %s to %s",
  162. e.entity,
  163. old_id,
  164. new_id,
  165. )
  166. return {
  167. "new_unique_id": entity_entry.unique_id.replace(
  168. old_id,
  169. new_id,
  170. )
  171. }
  172. await async_migrate_entries(hass, entry.entry_id, update_unique_id)
  173. hass.config_entries.async_update_entry(entry, version=6)
  174. if entry.version <= 8:
  175. # Deprecated entities are removed, trim the config back to required
  176. # config only
  177. conf = {**entry.data, **entry.options}
  178. hass.config_entries.async_update_entry(
  179. entry,
  180. data={
  181. CONF_DEVICE_ID: conf[CONF_DEVICE_ID],
  182. CONF_LOCAL_KEY: conf[CONF_LOCAL_KEY],
  183. CONF_HOST: conf[CONF_HOST],
  184. CONF_TYPE: conf[CONF_TYPE],
  185. },
  186. options={},
  187. version=9,
  188. )
  189. if entry.version <= 9:
  190. # Added protocol_version, default to auto
  191. conf = {**entry.data, **entry.options}
  192. hass.config_entries.async_update_entry(
  193. entry,
  194. data={
  195. CONF_DEVICE_ID: conf[CONF_DEVICE_ID],
  196. CONF_LOCAL_KEY: conf[CONF_LOCAL_KEY],
  197. CONF_HOST: conf[CONF_HOST],
  198. CONF_TYPE: conf[CONF_TYPE],
  199. CONF_PROTOCOL_VERSION: "auto",
  200. },
  201. options={},
  202. version=10,
  203. )
  204. if entry.version <= 10:
  205. conf = entry.data | entry.options
  206. hass.config_entries.async_update_entry(
  207. entry,
  208. data={
  209. CONF_DEVICE_ID: conf[CONF_DEVICE_ID],
  210. CONF_LOCAL_KEY: conf[CONF_LOCAL_KEY],
  211. CONF_HOST: conf[CONF_HOST],
  212. CONF_TYPE: conf[CONF_TYPE],
  213. CONF_PROTOCOL_VERSION: conf[CONF_PROTOCOL_VERSION],
  214. CONF_POLL_ONLY: False,
  215. },
  216. options={},
  217. version=11,
  218. )
  219. if entry.version <= 11:
  220. # Migrate unique ids of existing entities to new format
  221. device_id = entry.unique_id
  222. conf_file = await hass.async_add_executor_job(
  223. get_config,
  224. entry.data[CONF_TYPE],
  225. )
  226. if conf_file is None:
  227. _LOGGER.error(
  228. NOT_FOUND,
  229. entry.data[CONF_TYPE],
  230. )
  231. return False
  232. @callback
  233. def update_unique_id12(entity_entry):
  234. """Update the unique id of an entity entry."""
  235. old_id = entity_entry.unique_id
  236. platform = entity_entry.entity_id.split(".", 1)[0]
  237. e = conf_file.primary_entity
  238. if e.name:
  239. expect_id = f"{device_id}-{slugify(e.name)}"
  240. else:
  241. expect_id = device_id
  242. if e.entity != platform or expect_id != old_id:
  243. for e in conf_file.secondary_entities():
  244. if e.name:
  245. expect_id = f"{device_id}-{slugify(e.name)}"
  246. else:
  247. expect_id = device_id
  248. if e.entity == platform and expect_id == old_id:
  249. break
  250. if e.entity == platform and expect_id == old_id:
  251. new_id = e.unique_id(device_id)
  252. if new_id != old_id:
  253. _LOGGER.info(
  254. "Migrating %s unique_id %s to %s",
  255. e.entity,
  256. old_id,
  257. new_id,
  258. )
  259. return {
  260. "new_unique_id": entity_entry.unique_id.replace(
  261. old_id,
  262. new_id,
  263. )
  264. }
  265. await async_migrate_entries(hass, entry.entry_id, update_unique_id12)
  266. hass.config_entries.async_update_entry(entry, version=12)
  267. if entry.version <= 12:
  268. # Migrate unique ids of existing entities to new format taking into
  269. # account device_class if name is missing.
  270. device_id = entry.unique_id
  271. conf_file = await hass.async_add_executor_job(
  272. get_config,
  273. entry.data[CONF_TYPE],
  274. )
  275. if conf_file is None:
  276. _LOGGER.error(
  277. NOT_FOUND,
  278. entry.data[CONF_TYPE],
  279. )
  280. return False
  281. @callback
  282. def update_unique_id13(entity_entry):
  283. """Update the unique id of an entity entry."""
  284. old_id = entity_entry.unique_id
  285. platform = entity_entry.entity_id.split(".", 1)[0]
  286. # if unique_id ends with platform name, then this may have
  287. # changed with the addition of device_class.
  288. if old_id.endswith(platform):
  289. e = conf_file.primary_entity
  290. if e.entity != platform or e.name:
  291. for e in conf_file.secondary_entities():
  292. if e.entity == platform and not e.name:
  293. break
  294. if e.entity == platform and not e.name:
  295. new_id = e.unique_id(device_id)
  296. if new_id != old_id:
  297. _LOGGER.info(
  298. "Migrating %s unique_id %s to %s",
  299. e.entity,
  300. old_id,
  301. new_id,
  302. )
  303. return {
  304. "new_unique_id": entity_entry.unique_id.replace(
  305. old_id,
  306. new_id,
  307. )
  308. }
  309. else:
  310. replacements = {
  311. "sensor_co2": "sensor_carbon_dioxide",
  312. "sensor_co": "sensor_carbon_monoxide",
  313. "sensor_pm2_5": "sensor_pm25",
  314. "sensor_pm_10": "sensor_pm10",
  315. "sensor_pm_1_0": "sensor_pm1",
  316. "sensor_pm_2_5": "sensor_pm25",
  317. "sensor_tvoc": "sensor_volatile_organic_compounds",
  318. "sensor_current_humidity": "sensor_humidity",
  319. "sensor_current_temperature": "sensor_temperature",
  320. }
  321. return replace_unique_ids(
  322. entity_entry, device_id, conf_file, replacements
  323. )
  324. await async_migrate_entries(hass, entry.entry_id, update_unique_id13)
  325. hass.config_entries.async_update_entry(entry, version=13)
  326. if entry.version == 13 and entry.minor_version < 2:
  327. # Migrate unique ids of existing entities to new id taking into
  328. # account translation_key, and standardising naming
  329. device_id = entry.unique_id
  330. conf_file = await hass.async_add_executor_job(
  331. get_config,
  332. entry.data[CONF_TYPE],
  333. )
  334. if conf_file is None:
  335. _LOGGER.error(
  336. NOT_FOUND,
  337. entry.data[CONF_TYPE],
  338. )
  339. return False
  340. @callback
  341. def update_unique_id13_2(entity_entry):
  342. """Update the unique id of an entity entry."""
  343. # Standardistion of entity naming to use translation_key
  344. replacements = {
  345. # special meaning of None to handle _full and _empty variants
  346. "binary_sensor_tank": None,
  347. "binary_sensor_tank_full_or_missing": "binary_sensor_tank_full",
  348. "binary_sensor_water_tank_full": "binary_sensor_tank_full",
  349. "binary_sensor_low_water": "binary_sensor_tank_empty",
  350. "binary_sensor_water_tank_empty": "binary_sensor_tank_empty",
  351. "binary_sensor_fault": "binary_sensor_problem",
  352. "binary_sensor_error": "binary_sensor_problem",
  353. "binary_sensor_fault_alarm": "binary_sensor_problem",
  354. "binary_sensor_errors": "binary_sensor_problem",
  355. "binary_sensor_defrosting": "binary_sensor_defrost",
  356. "binary_sensor_anti_frost": "binary_sensor_defrost",
  357. "binary_sensor_anti_freeze": "binary_sensor_defrost",
  358. "binary_sensor_low_battery": "binary_sensor_battery",
  359. "binary_sensor_low_battery_alarm": "binary_sensor_battery",
  360. "select_temperature_units": "select_temperature_unit",
  361. "select_display_temperature_unit": "select_temperature_unit",
  362. "select_display_unit": "select_temperature_unit",
  363. "select_display_units": "select_temperature_unit",
  364. "select_temperature_display_units": "select_temperature_unit",
  365. "switch_defrost": "switch_anti_frost",
  366. "switch_frost_protection": "switch_anti_frost",
  367. }
  368. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  369. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_2)
  370. hass.config_entries.async_update_entry(entry, minor_version=2)
  371. if entry.version == 13 and entry.minor_version < 3:
  372. # Migrate unique ids of existing entities to new id taking into
  373. # account translation_key, and standardising naming
  374. device_id = entry.unique_id
  375. conf_file = await hass.async_add_executor_job(
  376. get_config,
  377. entry.data[CONF_TYPE],
  378. )
  379. if conf_file is None:
  380. _LOGGER.error(
  381. NOT_FOUND,
  382. entry.data[CONF_TYPE],
  383. )
  384. return False
  385. @callback
  386. def update_unique_id13_3(entity_entry):
  387. """Update the unique id of an entity entry."""
  388. # Standardistion of entity naming to use translation_key
  389. replacements = {
  390. "light_front_display": "light_display",
  391. "light_lcd_brightness": "light_display",
  392. "light_coal_bed": "light_logs",
  393. "light_ember": "light_embers",
  394. "light_led_indicator": "light_indicator",
  395. "light_status_indicator": "light_indicator",
  396. "light_indicator_light": "light_indicator",
  397. "light_indicators": "light_indicator",
  398. "light_night_light": "light_nightlight",
  399. "number_tiemout_period": "number_timeout_period",
  400. "sensor_remaining_time": "sensor_time_remaining",
  401. "sensor_timer_remain": "sensor_time_remaining",
  402. "sensor_timer": "sensor_time_remaining",
  403. "sensor_timer_countdown": "sensor_time_remaining",
  404. "sensor_timer_remaining": "sensor_time_remaining",
  405. "sensor_time_left": "sensor_time_remaining",
  406. "sensor_timer_minutes_left": "sensor_time_remaining",
  407. "sensor_timer_time_left": "sensor_time_remaining",
  408. "sensor_auto_shutoff_time_remaining": "sensor_time_remaining",
  409. "sensor_warm_time_remaining": "sensor_time_remaining",
  410. "sensor_run_time_remaining": "sensor_time_remaining",
  411. "switch_ioniser": "switch_ionizer",
  412. "switch_run_uv_cycle": "switch_uv_sterilization",
  413. "switch_uv_light": "switch_uv_sterilization",
  414. "switch_ihealth": "switch_uv_sterilization",
  415. "switch_uv_lamp": "switch_uv_sterilization",
  416. "switch_anti_freeze": "switch_anti_frost",
  417. }
  418. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  419. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_3)
  420. hass.config_entries.async_update_entry(entry, minor_version=3)
  421. if entry.version == 13 and entry.minor_version < 4:
  422. conf = entry.data | entry.options
  423. conf_type = conf[CONF_TYPE]
  424. # Duolicate config removal - make sure the correct one is used
  425. if conf_type == "ble-yl01_waterquality_tester":
  426. conf_type = "ble_yl01_watertester"
  427. hass.config_entries.async_update_entry(
  428. entry,
  429. data={**entry.data, CONF_TYPE: conf_type},
  430. options={**entry.options},
  431. minor_version=4,
  432. )
  433. if entry.version == 13 and entry.minor_version < 5:
  434. # Migrate unique ids of existing entities to new id taking into
  435. # account translation_key, and standardising naming
  436. device_id = entry.unique_id
  437. conf_file = await hass.async_add_executor_job(
  438. get_config,
  439. entry.data[CONF_TYPE],
  440. )
  441. if conf_file is None:
  442. _LOGGER.error(
  443. NOT_FOUND,
  444. entry.data[CONF_TYPE],
  445. )
  446. return False
  447. @callback
  448. def update_unique_id13_5(entity_entry):
  449. """Update the unique id of an entity entry."""
  450. # Standardistion of entity naming to use translation_key
  451. replacements = {
  452. "number_countdown": "number_timer",
  453. "select_countdown": "select_timer",
  454. "sensor_countdown": "sensor_time_remaining",
  455. "sensor_countdown_timer": "sensor_time_remaining",
  456. "fan": "fan_aroma_diffuser",
  457. }
  458. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  459. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_5)
  460. if entry.version == 13 and entry.minor_version < 6:
  461. # Migrate unique ids of existing entities to new id taking into
  462. # account translation_key, and standardising naming
  463. device_id = entry.unique_id
  464. conf_file = await hass.async_add_executor_job(
  465. get_config,
  466. entry.data[CONF_TYPE],
  467. )
  468. if conf_file is None:
  469. _LOGGER.error(
  470. NOT_FOUND,
  471. entry.data[CONF_TYPE],
  472. )
  473. return False
  474. @callback
  475. def update_unique_id13_6(entity_entry):
  476. """Update the unique id of an entity entry."""
  477. # Standardistion of entity naming to use translation_key
  478. replacements = {
  479. "switch_sleep_mode": "switch_sleep",
  480. "switch_sleep_timer": "switch_sleep",
  481. "select_voice_language": "select_language",
  482. "select_restore_power_state": "select_initial_state",
  483. "select_power_on_state": "select_initial_state",
  484. "select_restart_status": "select_initial_state",
  485. "select_poweron_status": "select_initial_state",
  486. "select_mop_mode": "select_mopping",
  487. "select_mop_control": "select_mopping",
  488. "select_water_setting": "select_mopping",
  489. "select_water_mode": "select_mopping",
  490. "select_water_control": "select_mopping",
  491. "select_water_tank": "select_mopping",
  492. "light_light": "light",
  493. "light_lights": "light",
  494. }
  495. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  496. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_6)
  497. hass.config_entries.async_update_entry(entry, minor_version=6)
  498. if entry.version == 13 and entry.minor_version < 7:
  499. # Migrate unique ids of existing entities to new id taking into
  500. # account translation_key, and standardising naming
  501. device_id = entry.unique_id
  502. conf_file = await hass.async_add_executor_job(
  503. get_config,
  504. entry.data[CONF_TYPE],
  505. )
  506. if conf_file is None:
  507. _LOGGER.error(
  508. NOT_FOUND,
  509. entry.data[CONF_TYPE],
  510. )
  511. return False
  512. @callback
  513. def update_unique_id13_7(entity_entry):
  514. """Update the unique id of an entity entry."""
  515. # Standardistion of entity naming to use translation_key
  516. replacements = {
  517. "sensor_charger_state": "sensor_status",
  518. }
  519. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  520. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_7)
  521. hass.config_entries.async_update_entry(entry, minor_version=7)
  522. if entry.version == 13 and entry.minor_version < 8:
  523. # Migrate unique ids of existing entities to new id taking into
  524. # account translation_key, and standardising naming
  525. device_id = entry.unique_id
  526. conf_file = await hass.async_add_executor_job(
  527. get_config,
  528. entry.data[CONF_TYPE],
  529. )
  530. if conf_file is None:
  531. _LOGGER.error(
  532. NOT_FOUND,
  533. entry.data[CONF_TYPE],
  534. )
  535. return False
  536. @callback
  537. def update_unique_id13_8(entity_entry):
  538. """Update the unique id of an entity entry."""
  539. # Standardistion of entity naming to use translation_key
  540. replacements = {
  541. "sensor_forward_energy": "sensor_energy_consumed",
  542. "sensor_total_forward_energy": "sensor_energy_consumed",
  543. "sensor_energy_in": "sensor_energy_consumed",
  544. "sensor_reverse_energy": "sensor_energy_produced",
  545. "sensor_energy_out": "sensor_energy_produced",
  546. "sensor_forward_energy_a": "sensor_energy_consumed_a",
  547. "sensor_reverse_energy_a": "sensor_energy_produced_a",
  548. "sensor_forward_energy_b": "sensor_energy_consumed_b",
  549. "sensor_reverse_energy_b": "sensor_energy_produced_b",
  550. "sensor_energy_consumption_a": "sensor_energy_consumed_a",
  551. "sensor_energy_consumption_b": "sensor_energy_consumed_b",
  552. "number_timer_switch_1": "number_timer_1",
  553. "number_timer_switch_2": "number_timer_2",
  554. "number_timer_switch_3": "number_timer_3",
  555. "number_timer_switch_4": "number_timer_4",
  556. "number_timer_switch_5": "number_timer_5",
  557. "number_timer_socket_1": "number_timer_1",
  558. "number_timer_socket_2": "number_timer_2",
  559. "number_timer_socket_3": "number_timer_3",
  560. "number_timer_outlet_1": "number_timer_1",
  561. "number_timer_outlet_2": "number_timer_2",
  562. "number_timer_outlet_3": "number_timer_3",
  563. "number_timer_gang_1": "number_timer_1",
  564. "number_timer_gang_2": "number_timer_2",
  565. "number_timer_gang_3": "number_timer_3",
  566. }
  567. return replace_unique_ids(entity_entry, device_id, conf_file, replacements)
  568. await async_migrate_entries(hass, entry.entry_id, update_unique_id13_8)
  569. hass.config_entries.async_update_entry(entry, minor_version=8)
  570. return True
  571. async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
  572. _LOGGER.debug(
  573. "Setting up entry for device: %s",
  574. get_device_id(entry.data),
  575. )
  576. config = {**entry.data, **entry.options, "name": entry.title}
  577. try:
  578. await hass.async_add_executor_job(setup_device, hass, config)
  579. except Exception as e:
  580. raise ConfigEntryNotReady("tuya-local device not ready") from e
  581. device_conf = await hass.async_add_executor_job(
  582. get_config,
  583. entry.data[CONF_TYPE],
  584. )
  585. if device_conf is None:
  586. _LOGGER.error(NOT_FOUND, config[CONF_TYPE])
  587. return False
  588. entities = set()
  589. for e in device_conf.all_entities():
  590. entities.add(e.entity)
  591. await hass.config_entries.async_forward_entry_setups(entry, entities)
  592. entry.add_update_listener(async_update_entry)
  593. return True
  594. async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
  595. _LOGGER.debug("Unloading entry for device: %s", get_device_id(entry.data))
  596. config = entry.data
  597. data = hass.data[DOMAIN][get_device_id(config)]
  598. device_conf = await hass.async_add_executor_job(
  599. get_config,
  600. config[CONF_TYPE],
  601. )
  602. if device_conf is None:
  603. _LOGGER.error(NOT_FOUND, config[CONF_TYPE])
  604. return False
  605. entities = {}
  606. for e in device_conf.all_entities():
  607. if e.config_id in data:
  608. entities[e.entity] = True
  609. for e in entities:
  610. await hass.config_entries.async_forward_entry_unload(entry, e)
  611. await async_delete_device(hass, config)
  612. del hass.data[DOMAIN][get_device_id(config)]
  613. return True
  614. async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry):
  615. _LOGGER.debug("Updating entry for device: %s", get_device_id(entry.data))
  616. await async_unload_entry(hass, entry)
  617. await async_setup_entry(hass, entry)