Skip to content

Commit

Permalink
send states on receiving peer id only if it is new + raise exception …
Browse files Browse the repository at this point in the history
…if cant connect to IPFS daemon on setup (#75)
  • Loading branch information
LoSk-p authored Sep 17, 2024
1 parent a02572a commit b34d76c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
7 changes: 4 additions & 3 deletions custom_components/robonomics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
)
Expand Down Expand Up @@ -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}"
Expand Down
7 changes: 6 additions & 1 deletion custom_components/robonomics/ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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"
Expand Down
15 changes: 12 additions & 3 deletions custom_components/robonomics/libp2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
4 changes: 2 additions & 2 deletions custom_components/robonomics/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 2 additions & 0 deletions custom_components/robonomics/robonomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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
)
Expand Down

0 comments on commit b34d76c

Please sign in to comment.