From f95bc724405e6d7ad359b2b4843ff35a988cbcab Mon Sep 17 00:00:00 2001 From: Kevin Eifinger Date: Sat, 11 Apr 2020 18:04:17 +0200 Subject: [PATCH] Mark entities unavailable on disconnect --- .../foldingathomecontrol/__init__.py | 28 ++++++++++++----- .../foldingathomecontrol/manifest.json | 2 +- .../foldingathomecontrol/services.py | 30 +++++++------------ requirements.txt | 2 +- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/custom_components/foldingathomecontrol/__init__.py b/custom_components/foldingathomecontrol/__init__.py index 314fdec..fcd366d 100644 --- a/custom_components/foldingathomecontrol/__init__.py +++ b/custom_components/foldingathomecontrol/__init__.py @@ -91,10 +91,12 @@ def __init__(self, hass: HomeAssistant, config_entry) -> None: self.client = None self._remove_callback = None self._task = None + self._available = False @callback def data_received_callback(self, message_type: str, data: Any) -> None: """Called when data is received from the Folding@Home client.""" + self._available = True if message_type == PyOnMessageTypes.SLOTS.value: self.handle_slots_data_received(data) if message_type == PyOnMessageTypes.ERROR.value: @@ -102,15 +104,32 @@ def data_received_callback(self, message_type: str, data: Any) -> None: self.data[message_type] = data async_dispatcher_send(self.hass, self.get_data_update_identifer()) + @callback + def on_disconnect_callback(self) -> None: + """Called when data is received from the Folding@Home client.""" + if self._available: + self._available = False + _LOGGER.error( + "Disconnected from %s:%s. Trying to reconnect.", + self.config_entry.data[CONF_ADDRESS], + self.config_entry.data[CONF_PORT], + ) + async_dispatcher_send(self.hass, self.get_data_update_identifer()) + async def async_setup(self) -> bool: """Set up the Folding@Home client.""" address = self.config_entry.data[CONF_ADDRESS] port = self.config_entry.data[CONF_PORT] password = self.config_entry.data.get(CONF_PASSWORD) - self.client = FoldingAtHomeController(address, port, password) + self.client = FoldingAtHomeController(address, port, password, read_timeout=10) try: + self._remove_callback = self.client.register_callback( + self.data_received_callback + ) + self.client.on_disconnect(self.on_disconnect_callback) await self.client.try_connect_async(timeout=5) await self.client.subscribe_async() + self._available = True except FoldingAtHomeControlConnectionFailed: return False @@ -120,9 +139,6 @@ async def async_setup(self) -> bool: ) ) - self._remove_callback = self.client.register_callback( - self.data_received_callback - ) self._task = asyncio.ensure_future(self.client.start()) return True @@ -186,6 +202,4 @@ def calculate_slot_changes(self, slots: dict) -> Tuple[dict, dict]: @property def available(self): """Is the Folding@Home client available.""" - if self.client: - return self.client.is_connected - return False + return self._available diff --git a/custom_components/foldingathomecontrol/manifest.json b/custom_components/foldingathomecontrol/manifest.json index 05e8101..df62f2a 100644 --- a/custom_components/foldingathomecontrol/manifest.json +++ b/custom_components/foldingathomecontrol/manifest.json @@ -8,7 +8,7 @@ "@eifinger" ], "requirements": [ - "PyFoldingAtHomeControl==1.1.0" + "PyFoldingAtHomeControl==1.1.2" ], "homeassistant": "0.96.0" } \ No newline at end of file diff --git a/custom_components/foldingathomecontrol/services.py b/custom_components/foldingathomecontrol/services.py index dec2566..d4256bd 100644 --- a/custom_components/foldingathomecontrol/services.py +++ b/custom_components/foldingathomecontrol/services.py @@ -22,15 +22,15 @@ SERVICE_PAUSE = "pause" SERVICE_UNPAUSE = "unpause" SERVICE_SLOT_SCHEMA = vol.Schema( - {vol.Required(SERVICE_ADDRESS): cv.string, - vol.Optional(SERVICE_SLOT, default=None): cv.string} + { + vol.Required(SERVICE_ADDRESS): cv.string, + vol.Optional(SERVICE_SLOT, default=None): cv.string, + } ) SERVICE_SHUTDOWN = "shutdown" SERVICE_REQUEST_WORK_SERVER_ASSIGNMENT = "request_work_server_assignment" -SERVICE_SCHEMA = vol.Schema( - {vol.Required(SERVICE_ADDRESS): cv.string} -) +SERVICE_SCHEMA = vol.Schema({vol.Required(SERVICE_ADDRESS): cv.string}) async def async_setup_services(hass): @@ -102,13 +102,9 @@ async def async_pause_service(hass, data): for config_entry in hass.data[DOMAIN]: if hass.data[DOMAIN][config_entry].config_entry.data[CONF_ADDRESS] == address: if slot is not None: - await hass.data[DOMAIN][ - config_entry - ].client.pause_slot_async(slot) + await hass.data[DOMAIN][config_entry].client.pause_slot_async(slot) return - await hass.data[DOMAIN][ - config_entry - ].client.pause_all_slots_async() + await hass.data[DOMAIN][config_entry].client.pause_all_slots_async() return _LOGGER.warning("Could not find a registered integration with address: %s", address) @@ -122,13 +118,9 @@ async def async_unpause_service(hass, data): for config_entry in hass.data[DOMAIN]: if hass.data[DOMAIN][config_entry].config_entry.data[CONF_ADDRESS] == address: if slot is not None: - await hass.data[DOMAIN][ - config_entry - ].client.unpause_slot_async(slot) + await hass.data[DOMAIN][config_entry].client.unpause_slot_async(slot) return - await hass.data[DOMAIN][ - config_entry - ].client.unpause_all_slots_async() + await hass.data[DOMAIN][config_entry].client.unpause_all_slots_async() return _LOGGER.warning("Could not find a registered integration with address: %s", address) @@ -140,9 +132,7 @@ async def async_shutdown_service(hass, data): for config_entry in hass.data[DOMAIN]: if hass.data[DOMAIN][config_entry].config_entry.data[CONF_ADDRESS] == address: - await hass.data[DOMAIN][ - config_entry - ].client.shutdown() + await hass.data[DOMAIN][config_entry].client.shutdown() return _LOGGER.warning("Could not find a registered integration with address: %s", address) diff --git a/requirements.txt b/requirements.txt index 670dd2b..f0e2457 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -PyFoldingAtHomeControl==1.1.0 \ No newline at end of file +PyFoldingAtHomeControl==1.1.2 \ No newline at end of file