diff --git a/custom_components/robonomics/__init__.py b/custom_components/robonomics/__init__.py index 365f28f..fc0007b 100644 --- a/custom_components/robonomics/__init__.py +++ b/custom_components/robonomics/__init__.py @@ -165,7 +165,6 @@ async def init_integration(_: Event = None) -> None: await get_and_send_data(hass) - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, init_integration) _LOGGER.debug("Robonomics user control starting set up") conf = entry.data if CONF_IPFS_GATEWAY in conf: @@ -213,12 +212,12 @@ async def init_integration(_: Event = None) -> None: if os.path.isdir(data_path): shutil.rmtree(data_path) - await wait_ipfs_daemon(hass) + await wait_ipfs_daemon(hass, timeout = 30) try: await create_folders(hass) except Exception as e: _LOGGER.error(f"Exception in create ipfs folders: {e}") - await wait_ipfs_daemon(hass) + await wait_ipfs_daemon(hass, timeout = 30) hass.states.async_set( f"sensor.{IPFS_STATUS_ENTITY}", hass.data[DOMAIN][IPFS_STATUS] ) @@ -378,6 +377,8 @@ async def handle_save_video(call: ServiceCall) -> None: asyncio.ensure_future(hass.data[DOMAIN][ROBONOMICS].pin_dapp_to_local_node()) if hass.state == CoreState.running: asyncio.ensure_future(init_integration()) + else: + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, init_integration) _LOGGER.debug( f"Robonomics user control successfuly set up, hass state: {hass.state}" diff --git a/custom_components/robonomics/ipfs.py b/custom_components/robonomics/ipfs.py index 6cbf5be..5071402 100644 --- a/custom_components/robonomics/ipfs.py +++ b/custom_components/robonomics/ipfs.py @@ -59,6 +59,7 @@ ) from .ipfs_helpers.decorators import catch_ipfs_errors from .ipfs_helpers.get_data import GetIPFSData +from .exceptions import CantConnectToIPFS _LOGGER = logging.getLogger(__name__) @@ -223,13 +224,17 @@ async def handle_ipfs_status_change(hass: HomeAssistant, ipfs_daemon_ok: bool): await create_notification(hass, service_data, "ipfs") -async def wait_ipfs_daemon(hass: HomeAssistant) -> None: +async def wait_ipfs_daemon(hass: HomeAssistant, timeout: tp.Optional[int] = None) -> None: if hass.data[DOMAIN][WAIT_IPFS_DAEMON]: return hass.data[DOMAIN][WAIT_IPFS_DAEMON] = True _LOGGER.debug("Wait for IPFS local node connection...") + start_time = time.time() connected = await hass.async_add_executor_job(_check_connection, hass) while not connected: + if timeout: + if (time.time() - start_time) > timeout: + raise CantConnectToIPFS await asyncio.sleep(10) connected = await hass.async_add_executor_job(_check_connection, hass) hass.data[DOMAIN][IPFS_STATUS] = "OK" diff --git a/custom_components/robonomics/libp2p.py b/custom_components/robonomics/libp2p.py index a721a45..3f2da03 100644 --- a/custom_components/robonomics/libp2p.py +++ b/custom_components/robonomics/libp2p.py @@ -119,7 +119,16 @@ async def _handle_libp2p_errors(self, data: tp.Union[str, dict]) -> None: await create_notification(self.hass, service_data, "libp2p") def _set_peer_id(self, message: InitialMessage) -> None: - self.hass.data[DOMAIN][PEER_ID_LOCAL] = message.peer_id - self.hass.data[DOMAIN][LIBP2P_MULTIADDRESS] = message.multi_addressess - asyncio.ensure_future(get_and_send_data(self.hass)) + if self._is_initial_data_new(message): + self.hass.data[DOMAIN][PEER_ID_LOCAL] = message.peer_id + self.hass.data[DOMAIN][LIBP2P_MULTIADDRESS] = message.multi_addressess + _LOGGER.debug("Start getting states because of new peer id") + asyncio.ensure_future(get_and_send_data(self.hass)) + def _is_initial_data_new(self, message: InitialMessage) -> bool: + return ( + (PEER_ID_LOCAL not in self.hass.data[DOMAIN]) + or (self.hass.data[DOMAIN][PEER_ID_LOCAL] != message.peer_id) + or (LIBP2P_MULTIADDRESS not in self.hass.data[DOMAIN]) + or (self.hass.data[DOMAIN][LIBP2P_MULTIADDRESS] != message.multi_addressess) + ) diff --git a/custom_components/robonomics/manifest.json b/custom_components/robonomics/manifest.json index b85ccff..11a11eb 100644 --- a/custom_components/robonomics/manifest.json +++ b/custom_components/robonomics/manifest.json @@ -8,6 +8,6 @@ "documentation": "https://wiki.robonomics.network/en/", "iot_class": "cloud_push", "issue_tracker": "https://github.com/airalab/homeassistant-robonomics-integration/issues", - "requirements": ["pycryptodome==3.15.0", "wheel", "IPFS-Toolkit==0.4.0", "robonomics-interface==1.6.2", "pinatapy-vourhey==0.1.9", "aenum==3.1.11", "ipfs-api==0.2.3", "crust-interface-patara==0.1.1", "tenacity==8.2.2", "py-ws-libp2p-proxy~=0.2.1"], - "version": "1.9.1" + "requirements": ["pycryptodome==3.15.0", "wheel", "IPFS-Toolkit==0.4.0", "robonomics-interface==1.6.2", "pinatapy-vourhey==0.1.9", "aenum==3.1.11", "ipfs-api==0.2.3", "crust-interface-patara==0.1.1", "tenacity==8.2.2", "py-ws-libp2p-proxy~=0.3.0"], + "version": "1.9.2" } diff --git a/custom_components/robonomics/robonomics.py b/custom_components/robonomics/robonomics.py index ffed213..8ccd4e8 100644 --- a/custom_components/robonomics/robonomics.py +++ b/custom_components/robonomics/robonomics.py @@ -346,6 +346,7 @@ async def _handle_launch(self, data: tp.Tuple[str]) -> None: _LOGGER.debug(f"Got call service command {json_result}") await _run_launch_command(self.hass, result, data[0]) # asyncio.ensure_future(get_and_send_data(self.hass)) + _LOGGER.debug("Start getting states because of new launch") await get_and_send_data(self.hass) except Exception as e: _LOGGER.error(f"Exception in launch handler command: {e}") @@ -769,6 +770,7 @@ def callback_new_event(self, data: tp.Tuple[tp.Union[str, tp.List[str]]]) -> Non asyncio.run_coroutine_threadsafe( UserManager(self.hass).update_users(data[1]), self.hass.loop ) + _LOGGER.debug("Start getting states because of new devices") asyncio.run_coroutine_threadsafe( get_and_send_data(self.hass), self.hass.loop )