config_flow.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. import asyncio
  2. import logging
  3. from collections import OrderedDict
  4. from typing import Any
  5. import tinytuya
  6. import voluptuous as vol
  7. from homeassistant import config_entries
  8. from homeassistant.const import CONF_HOST, CONF_NAME
  9. from homeassistant.core import HomeAssistant, callback
  10. from homeassistant.data_entry_flow import FlowResult
  11. from homeassistant.helpers.selector import (
  12. QrCodeSelector,
  13. QrCodeSelectorConfig,
  14. QrErrorCorrectionLevel,
  15. SelectOptionDict,
  16. SelectSelector,
  17. SelectSelectorConfig,
  18. SelectSelectorMode,
  19. )
  20. from tuya_sharing import (
  21. CustomerDevice,
  22. LoginControl,
  23. Manager,
  24. SharingDeviceListener,
  25. SharingTokenListener,
  26. )
  27. from . import DOMAIN
  28. from .const import (
  29. API_PROTOCOL_VERSIONS,
  30. CONF_DEVICE_CID,
  31. CONF_DEVICE_ID,
  32. CONF_ENDPOINT,
  33. CONF_LOCAL_KEY,
  34. CONF_POLL_ONLY,
  35. CONF_PROTOCOL_VERSION,
  36. CONF_TERMINAL_ID,
  37. CONF_TYPE,
  38. CONF_USER_CODE,
  39. DATA_STORE,
  40. TUYA_CLIENT_ID,
  41. TUYA_RESPONSE_CODE,
  42. TUYA_RESPONSE_MSG,
  43. TUYA_RESPONSE_QR_CODE,
  44. TUYA_RESPONSE_RESULT,
  45. TUYA_RESPONSE_SUCCESS,
  46. TUYA_SCHEMA,
  47. )
  48. from .device import TuyaLocalDevice
  49. from .helpers.config import get_device_id
  50. from .helpers.device_config import get_config
  51. from .helpers.log import log_json
  52. _LOGGER = logging.getLogger(__name__)
  53. HUB_CATEGORIES = [
  54. "wgsxj", # Gateway camera
  55. "lyqwg", # Router
  56. "bywg", # IoT edge gateway
  57. "zigbee", # Gateway
  58. "wg2", # Gateway
  59. "dgnzk", # Multi-function controller
  60. "videohub", # Videohub
  61. "xnwg", # Virtual gateway
  62. "qtyycp", # Voice gateway composite solution
  63. "alexa_yywg", # Gateway with Alexa
  64. "gywg", # Industrial gateway
  65. "cnwg", # Energy gateway
  66. "wnykq", # Smart IR
  67. ]
  68. class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
  69. VERSION = 13
  70. MINOR_VERSION = 5
  71. CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH
  72. device = None
  73. data = {}
  74. __user_code: str
  75. __qr_code: str
  76. __authentication: dict
  77. __cloud_devices: dict
  78. __cloud_device: dict
  79. def __init__(self) -> None:
  80. """Initialize the config flow."""
  81. self.__login_control = LoginControl()
  82. self.__cloud_devices = {}
  83. self.__cloud_device = None
  84. async def async_step_user(self, user_input=None):
  85. errors = {}
  86. if self.hass.data.get(DOMAIN) is None:
  87. self.hass.data[DOMAIN] = {}
  88. if self.hass.data[DOMAIN].get(DATA_STORE) is None:
  89. self.hass.data[DOMAIN][DATA_STORE] = {}
  90. self.__authentication = self.hass.data[DOMAIN][DATA_STORE].get(
  91. "authentication", None
  92. )
  93. if user_input is not None:
  94. if user_input["setup_mode"] == "cloud":
  95. try:
  96. if self.__authentication is not None:
  97. self.__cloud_devices = await self.load_device_info()
  98. return await self.async_step_choose_device(None)
  99. except Exception as e:
  100. # Re-authentication is needed.
  101. _LOGGER.warning("Connection test failed with %s %s", type(e), e)
  102. _LOGGER.warning("Re-authentication is required.")
  103. return await self.async_step_cloud(None)
  104. if user_input["setup_mode"] == "manual":
  105. return await self.async_step_local(None)
  106. # Build form
  107. fields: OrderedDict[vol.Marker, Any] = OrderedDict()
  108. fields[vol.Required("setup_mode")] = SelectSelector(
  109. SelectSelectorConfig(
  110. options=["cloud", "manual"],
  111. mode=SelectSelectorMode.LIST,
  112. translation_key="setup_mode",
  113. )
  114. )
  115. return self.async_show_form(
  116. step_id="user",
  117. data_schema=vol.Schema(fields),
  118. errors=errors or {},
  119. last_step=False,
  120. )
  121. async def async_step_cloud(
  122. self, user_input: dict[str, Any] | None = None
  123. ) -> FlowResult:
  124. """Step user."""
  125. errors = {}
  126. placeholders = {}
  127. if user_input is not None:
  128. success, response = await self.__async_get_qr_code(
  129. user_input[CONF_USER_CODE]
  130. )
  131. if success:
  132. return await self.async_step_scan(None)
  133. errors["base"] = "login_error"
  134. placeholders = {
  135. TUYA_RESPONSE_MSG: response.get(TUYA_RESPONSE_MSG, "Unknown error"),
  136. TUYA_RESPONSE_CODE: response.get(TUYA_RESPONSE_CODE, "0"),
  137. }
  138. else:
  139. user_input = {}
  140. return self.async_show_form(
  141. step_id="cloud",
  142. data_schema=vol.Schema(
  143. {
  144. vol.Required(
  145. CONF_USER_CODE, default=user_input.get(CONF_USER_CODE, "")
  146. ): str,
  147. }
  148. ),
  149. errors=errors,
  150. description_placeholders=placeholders,
  151. )
  152. async def async_step_scan(
  153. self, user_input: dict[str, Any] | None = None
  154. ) -> FlowResult:
  155. """Step scan."""
  156. if user_input is None:
  157. return self.async_show_form(
  158. step_id="scan",
  159. data_schema=vol.Schema(
  160. {
  161. vol.Optional("QR"): QrCodeSelector(
  162. config=QrCodeSelectorConfig(
  163. data=f"tuyaSmart--qrLogin?token={self.__qr_code}",
  164. scale=5,
  165. error_correction_level=QrErrorCorrectionLevel.QUARTILE,
  166. )
  167. )
  168. }
  169. ),
  170. )
  171. ret, info = await self.hass.async_add_executor_job(
  172. self.__login_control.login_result,
  173. self.__qr_code,
  174. TUYA_CLIENT_ID,
  175. self.__user_code,
  176. )
  177. if not ret:
  178. # Try to get a new QR code on failure
  179. await self.__async_get_qr_code(self.__user_code)
  180. return self.async_show_form(
  181. step_id="scan",
  182. errors={"base": "login_error"},
  183. data_schema=vol.Schema(
  184. {
  185. vol.Optional("QR"): QrCodeSelector(
  186. config=QrCodeSelectorConfig(
  187. data=f"tuyaSmart--qrLogin?token={self.__qr_code}",
  188. scale=5,
  189. error_correction_level=QrErrorCorrectionLevel.QUARTILE,
  190. )
  191. )
  192. }
  193. ),
  194. description_placeholders={
  195. TUYA_RESPONSE_MSG: info.get(TUYA_RESPONSE_MSG, "Unknown error"),
  196. TUYA_RESPONSE_CODE: info.get(TUYA_RESPONSE_CODE, 0),
  197. },
  198. )
  199. # Now that we have successfully logged in we can query for devices for the account.
  200. self.__authentication = {
  201. "user_code": info[CONF_TERMINAL_ID],
  202. "terminal_id": info[CONF_TERMINAL_ID],
  203. "endpoint": info[CONF_ENDPOINT],
  204. "token_info": {
  205. "t": info["t"],
  206. "uid": info["uid"],
  207. "expire_time": info["expire_time"],
  208. "access_token": info["access_token"],
  209. "refresh_token": info["refresh_token"],
  210. },
  211. }
  212. self.hass.data[DOMAIN][DATA_STORE]["authentication"] = self.__authentication
  213. _LOGGER.debug(f"domain_data is {self.hass.data[DOMAIN]}")
  214. self.__cloud_devices = await self.load_device_info()
  215. return await self.async_step_choose_device(None)
  216. async def load_device_info(self) -> dict:
  217. token_listener = TokenListener(self.hass)
  218. manager = Manager(
  219. TUYA_CLIENT_ID,
  220. self.__authentication["user_code"],
  221. self.__authentication["terminal_id"],
  222. self.__authentication["endpoint"],
  223. self.__authentication["token_info"],
  224. token_listener,
  225. )
  226. listener = DeviceListener(self.hass, manager)
  227. manager.add_device_listener(listener)
  228. # Get all devices from Tuya
  229. await self.hass.async_add_executor_job(manager.update_device_cache)
  230. # Register known device IDs
  231. cloud_devices = {}
  232. domain_data = self.hass.data.get(DOMAIN)
  233. for device in manager.device_map.values():
  234. cloud_device = {
  235. # TODO - Use constants throughout
  236. "category": device.category,
  237. "id": device.id,
  238. "ip": device.ip, # This will be the WAN IP address so not usable.
  239. CONF_LOCAL_KEY: device.local_key
  240. if hasattr(device, CONF_LOCAL_KEY)
  241. else "",
  242. "name": device.name,
  243. "node_id": device.node_id if hasattr(device, "node_id") else "",
  244. "online": device.online,
  245. "product_id": device.product_id,
  246. "product_name": device.product_name,
  247. "uid": device.uid,
  248. "uuid": device.uuid,
  249. "support_local": device.support_local, # What does this mean?
  250. CONF_DEVICE_CID: None,
  251. "version": None,
  252. }
  253. _LOGGER.debug(f"Found device: {cloud_device}")
  254. existing_id = domain_data.get(cloud_device["id"]) if domain_data else None
  255. existing_uuid = (
  256. domain_data.get(cloud_device["uuid"]) if domain_data else None
  257. )
  258. existing = existing_id or existing_uuid
  259. if existing and existing.get("device"):
  260. cloud_device["exists"] = True
  261. _LOGGER.debug(f"Adding device: {cloud_device['id']}")
  262. cloud_devices[cloud_device["id"]] = cloud_device
  263. return cloud_devices
  264. async def async_step_choose_device(self, user_input=None):
  265. errors = {}
  266. if user_input is not None:
  267. device_choice = self.__cloud_devices[user_input["device_id"]]
  268. if device_choice["ip"] != "":
  269. # This is a directly addable device.
  270. if user_input["hub_id"] == "None":
  271. device_choice["ip"] = ""
  272. self.__cloud_device = device_choice
  273. return await self.async_step_search(None)
  274. else:
  275. # Show error if user selected a hub.
  276. errors["base"] = "does_not_need_hub"
  277. # Fall through to reshow the form.
  278. else:
  279. # This is an indirectly addressable device. Need to know which hub it is connected to.
  280. if user_input["hub_id"] != "None":
  281. hub_choice = self.__cloud_devices[user_input["hub_id"]]
  282. # Populate uuid and local_key from the child device to pass on complete information to the local step.
  283. hub_choice["ip"] = ""
  284. hub_choice[CONF_DEVICE_CID] = device_choice["uuid"]
  285. hub_choice[CONF_LOCAL_KEY] = device_choice[CONF_LOCAL_KEY]
  286. self.__cloud_device = hub_choice
  287. return await self.async_step_search(None)
  288. else:
  289. # Show error if user did not select a hub.
  290. errors["base"] = "needs_hub"
  291. # Fall through to reshow the form.
  292. device_list = []
  293. for key in self.__cloud_devices.keys():
  294. device_entry = self.__cloud_devices[key]
  295. if device_entry.get("exists"):
  296. continue
  297. if device_entry[CONF_LOCAL_KEY] != "":
  298. if device_entry["online"]:
  299. device_list.append(
  300. SelectOptionDict(
  301. value=key,
  302. label=f"{device_entry['name']} ({device_entry['product_name']})",
  303. )
  304. )
  305. else:
  306. device_list.append(
  307. SelectOptionDict(
  308. value=key,
  309. label=f"{device_entry['name']} ({device_entry['product_name']}) OFFLINE",
  310. )
  311. )
  312. _LOGGER.debug(f"Device count: {len(device_list)}")
  313. if len(device_list) == 0:
  314. return self.async_abort(reason="no_devices")
  315. device_selector = SelectSelector(
  316. SelectSelectorConfig(options=device_list, mode=SelectSelectorMode.DROPDOWN)
  317. )
  318. hub_list = []
  319. hub_list.append(SelectOptionDict(value="None", label="None"))
  320. for key in self.__cloud_devices.keys():
  321. hub_entry = self.__cloud_devices[key]
  322. if (
  323. hub_entry[CONF_LOCAL_KEY] == ""
  324. or hub_entry["category"] in HUB_CATEGORIES
  325. ):
  326. hub_list.append(
  327. SelectOptionDict(
  328. value=key,
  329. label=f"{hub_entry['name']} ({hub_entry['product_name']})",
  330. )
  331. )
  332. _LOGGER.debug(f"Hub count: {len(hub_list) - 1}")
  333. hub_selector = SelectSelector(
  334. SelectSelectorConfig(options=hub_list, mode=SelectSelectorMode.DROPDOWN)
  335. )
  336. # Build form
  337. fields: OrderedDict[vol.Marker, Any] = OrderedDict()
  338. fields[vol.Required("device_id")] = device_selector
  339. fields[vol.Required("hub_id")] = hub_selector
  340. return self.async_show_form(
  341. step_id="choose_device",
  342. data_schema=vol.Schema(fields),
  343. errors=errors or {},
  344. last_step=False,
  345. )
  346. async def async_step_search(self, user_input=None):
  347. if user_input is not None:
  348. # Current IP is the WAN IP which is of no use. Need to try and discover to the local IP.
  349. # This scan will take 18s with the default settings. If we cannot find the device we
  350. # will just leave the IP address blank and hope the user can discover the IP by other
  351. # means such as router device IP assignments.
  352. _LOGGER.debug(
  353. f"Scanning network to get IP address for {self.__cloud_device['id']}."
  354. )
  355. self.__cloud_device["ip"] = ""
  356. try:
  357. local_device = await self.hass.async_add_executor_job(
  358. scan_for_device, self.__cloud_device["id"]
  359. )
  360. except OSError:
  361. local_device = {"ip": None, "version": ""}
  362. if local_device["ip"] is not None:
  363. _LOGGER.debug(f"Found: {local_device}")
  364. self.__cloud_device["ip"] = local_device["ip"]
  365. self.__cloud_device["version"] = local_device["version"]
  366. else:
  367. _LOGGER.warning(f"Could not find device: {self.__cloud_device['id']}")
  368. return await self.async_step_local(None)
  369. return self.async_show_form(
  370. step_id="search", data_schema=vol.Schema({}), errors={}, last_step=False
  371. )
  372. async def async_step_local(self, user_input=None):
  373. errors = {}
  374. devid_opts = {}
  375. host_opts = {"default": ""}
  376. key_opts = {}
  377. proto_opts = {"default": 3.3}
  378. polling_opts = {"default": False}
  379. devcid_opts = {}
  380. if self.__cloud_device is not None:
  381. # We already have some or all of the device settings from the cloud flow. Set them into the defaults.
  382. devid_opts = {"default": self.__cloud_device["id"]}
  383. host_opts = {"default": self.__cloud_device["ip"]}
  384. key_opts = {"default": self.__cloud_device[CONF_LOCAL_KEY]}
  385. if self.__cloud_device["version"] is not None:
  386. proto_opts = {"default": float(self.__cloud_device["version"])}
  387. if self.__cloud_device[CONF_DEVICE_CID] is not None:
  388. devcid_opts = {"default": self.__cloud_device[CONF_DEVICE_CID]}
  389. if user_input is not None:
  390. self.device = await async_test_connection(user_input, self.hass)
  391. if self.device:
  392. self.data = user_input
  393. return await self.async_step_select_type()
  394. else:
  395. errors["base"] = "connection"
  396. devid_opts["default"] = user_input[CONF_DEVICE_ID]
  397. host_opts["default"] = user_input[CONF_HOST]
  398. key_opts["default"] = user_input[CONF_LOCAL_KEY]
  399. if CONF_DEVICE_CID in user_input:
  400. devcid_opts["default"] = user_input[CONF_DEVICE_CID]
  401. proto_opts["default"] = user_input[CONF_PROTOCOL_VERSION]
  402. polling_opts["default"] = user_input[CONF_POLL_ONLY]
  403. return self.async_show_form(
  404. step_id="local",
  405. data_schema=vol.Schema(
  406. {
  407. vol.Required(CONF_DEVICE_ID, **devid_opts): str,
  408. vol.Required(CONF_HOST, **host_opts): str,
  409. vol.Required(CONF_LOCAL_KEY, **key_opts): str,
  410. vol.Required(
  411. CONF_PROTOCOL_VERSION,
  412. **proto_opts,
  413. ): vol.In(["auto"] + API_PROTOCOL_VERSIONS),
  414. vol.Required(CONF_POLL_ONLY, **polling_opts): bool,
  415. vol.Optional(CONF_DEVICE_CID, **devcid_opts): str,
  416. }
  417. ),
  418. errors=errors,
  419. )
  420. async def async_step_select_type(self, user_input=None):
  421. if user_input is not None:
  422. self.data[CONF_TYPE] = user_input[CONF_TYPE]
  423. return await self.async_step_choose_entities()
  424. types = []
  425. best_match = 0
  426. best_matching_type = None
  427. async for type in self.device.async_possible_types():
  428. types.append(type.config_type)
  429. q = type.match_quality(self.device._get_cached_state())
  430. if q > best_match:
  431. best_match = q
  432. best_matching_type = type.config_type
  433. best_match = int(best_match)
  434. dps = self.device._get_cached_state()
  435. _LOGGER.warning(
  436. "Device matches %s with quality of %d%%. DPS: %s",
  437. best_matching_type,
  438. best_match,
  439. log_json(dps),
  440. )
  441. _LOGGER.warning(
  442. "Include the previous log message with any new device request to https://github.com/make-all/tuya-local/issues/",
  443. )
  444. if types:
  445. return self.async_show_form(
  446. step_id="select_type",
  447. data_schema=vol.Schema(
  448. {
  449. vol.Required(
  450. CONF_TYPE,
  451. default=best_matching_type,
  452. ): vol.In(types),
  453. }
  454. ),
  455. )
  456. else:
  457. return self.async_abort(reason="not_supported")
  458. async def async_step_choose_entities(self, user_input=None):
  459. if user_input is not None:
  460. title = user_input[CONF_NAME]
  461. del user_input[CONF_NAME]
  462. return self.async_create_entry(
  463. title=title, data={**self.data, **user_input}
  464. )
  465. config = await self.hass.async_add_executor_job(
  466. get_config,
  467. self.data[CONF_TYPE],
  468. )
  469. schema = {vol.Required(CONF_NAME, default=config.name): str}
  470. return self.async_show_form(
  471. step_id="choose_entities",
  472. data_schema=vol.Schema(schema),
  473. )
  474. @staticmethod
  475. @callback
  476. def async_get_options_flow(config_entry):
  477. return OptionsFlowHandler(config_entry)
  478. async def __async_get_qr_code(self, user_code: str) -> tuple[bool, dict[str, Any]]:
  479. """Get the QR code."""
  480. response = await self.hass.async_add_executor_job(
  481. self.__login_control.qr_code,
  482. TUYA_CLIENT_ID,
  483. TUYA_SCHEMA,
  484. user_code,
  485. )
  486. if success := response.get(TUYA_RESPONSE_SUCCESS, False):
  487. self.__user_code = user_code
  488. self.__qr_code = response[TUYA_RESPONSE_RESULT][TUYA_RESPONSE_QR_CODE]
  489. return success, response
  490. class OptionsFlowHandler(config_entries.OptionsFlow):
  491. def __init__(self, config_entry):
  492. """Initialize options flow."""
  493. self.config_entry = config_entry
  494. async def async_step_init(self, user_input=None):
  495. return await self.async_step_user(user_input)
  496. async def async_step_user(self, user_input=None):
  497. """Manage the options."""
  498. errors = {}
  499. config = {**self.config_entry.data, **self.config_entry.options}
  500. if user_input is not None:
  501. config = {**config, **user_input}
  502. device = await async_test_connection(config, self.hass)
  503. if device:
  504. return self.async_create_entry(title="", data=user_input)
  505. else:
  506. errors["base"] = "connection"
  507. schema = {
  508. vol.Required(
  509. CONF_LOCAL_KEY,
  510. default=config.get(CONF_LOCAL_KEY, ""),
  511. ): str,
  512. vol.Required(CONF_HOST, default=config.get(CONF_HOST, "")): str,
  513. vol.Required(
  514. CONF_PROTOCOL_VERSION,
  515. default=config.get(CONF_PROTOCOL_VERSION, "auto"),
  516. ): vol.In(["auto"] + API_PROTOCOL_VERSIONS),
  517. vol.Required(
  518. CONF_POLL_ONLY, default=config.get(CONF_POLL_ONLY, False)
  519. ): bool,
  520. vol.Optional(
  521. CONF_DEVICE_CID,
  522. default=config.get(CONF_DEVICE_CID, ""),
  523. ): str,
  524. }
  525. cfg = await self.hass.async_add_executor_job(
  526. get_config,
  527. config[CONF_TYPE],
  528. )
  529. if cfg is None:
  530. return self.async_abort(reason="not_supported")
  531. return self.async_show_form(
  532. step_id="user",
  533. data_schema=vol.Schema(schema),
  534. errors=errors,
  535. )
  536. def create_test_device(hass: HomeAssistant, config: dict):
  537. """Set up a tuya device based on passed in config."""
  538. subdevice_id = config.get(CONF_DEVICE_CID)
  539. device = TuyaLocalDevice(
  540. "Test",
  541. config[CONF_DEVICE_ID],
  542. config[CONF_HOST],
  543. config[CONF_LOCAL_KEY],
  544. config[CONF_PROTOCOL_VERSION],
  545. subdevice_id,
  546. hass,
  547. True,
  548. )
  549. return device
  550. async def async_test_connection(config: dict, hass: HomeAssistant):
  551. domain_data = hass.data.get(DOMAIN)
  552. existing = domain_data.get(get_device_id(config)) if domain_data else None
  553. if existing:
  554. _LOGGER.info("Pausing existing device to test new connection parameters")
  555. existing["device"].pause()
  556. await asyncio.sleep(5)
  557. try:
  558. device = await hass.async_add_executor_job(
  559. create_test_device,
  560. hass,
  561. config,
  562. )
  563. await device.async_refresh()
  564. retval = device if device.has_returned_state else None
  565. except Exception as e:
  566. _LOGGER.warning("Connection test failed with %s %s", type(e), e)
  567. retval = None
  568. if existing:
  569. _LOGGER.info("Restarting device after test")
  570. existing["device"].resume()
  571. return retval
  572. def scan_for_device(id):
  573. return tinytuya.find_device(dev_id=id)
  574. class DeviceListener(SharingDeviceListener):
  575. """Device Update Listener."""
  576. def __init__(
  577. self,
  578. hass: HomeAssistant,
  579. manager: Manager,
  580. ) -> None:
  581. """Init DeviceListener."""
  582. self.hass = hass
  583. self.manager = manager
  584. def update_device(self, device: CustomerDevice) -> None:
  585. """Update device status."""
  586. _LOGGER.debug(
  587. "Received update for device %s: %s",
  588. device.id,
  589. self.manager.device_map[device.id].status,
  590. )
  591. def add_device(self, device: CustomerDevice) -> None:
  592. """Add device added listener."""
  593. _LOGGER.debug(
  594. "Received add device %s: %s",
  595. device.id,
  596. self.manager.device_map[device.id].status,
  597. )
  598. def remove_device(self, device_id: str) -> None:
  599. """Add device removed listener."""
  600. _LOGGER.debug(
  601. "Received remove device %s: %s",
  602. device_id,
  603. self.manager.device_map[device_id].status,
  604. )
  605. class TokenListener(SharingTokenListener):
  606. """Token listener for upstream token updates."""
  607. def __init__(
  608. self,
  609. hass: HomeAssistant,
  610. ) -> None:
  611. """Init TokenListener."""
  612. self.hass = hass
  613. def update_token(self, token_info: dict[str, Any]) -> None:
  614. """Update token info in config entry."""
  615. _LOGGER.debug("update_token")