Skip to content

Commit

Permalink
Mark entities unavailable on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger committed Apr 11, 2020
1 parent 449edb9 commit f95bc72
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
28 changes: 21 additions & 7 deletions custom_components/foldingathomecontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,45 @@ 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:
self.handle_error_received(data)
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

Expand All @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion custom_components/foldingathomecontrol/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@eifinger"
],
"requirements": [
"PyFoldingAtHomeControl==1.1.0"
"PyFoldingAtHomeControl==1.1.2"
],
"homeassistant": "0.96.0"
}
30 changes: 10 additions & 20 deletions custom_components/foldingathomecontrol/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PyFoldingAtHomeControl==1.1.0
PyFoldingAtHomeControl==1.1.2

0 comments on commit f95bc72

Please sign in to comment.