Skip to content

Commit

Permalink
v0.1.1 - fix Connectionloss, add services
Browse files Browse the repository at this point in the history
  • Loading branch information
eifinger committed Apr 10, 2020
1 parent 927d557 commit 449edb9
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
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.0.0"
"PyFoldingAtHomeControl==1.1.0"
],
"homeassistant": "0.96.0"
}
97 changes: 96 additions & 1 deletion custom_components/foldingathomecontrol/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@
DOMAIN_SERVICES = f"{DOMAIN}_services"

SERVICE_ADDRESS = "address"
SERVICE_SLOT = "slot"

SERVICE_REQUEST_WORK_SERVER_ASSIGNMENT = "request_work_server_assignment"
SERVICE_REQUEST_WORK_SERVER_ASSIGNMENT_SCHEMA = vol.Schema(
{vol.Required(SERVICE_ADDRESS): cv.string}
)

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}
)

SERVICE_SHUTDOWN = "shutdown"
SERVICE_REQUEST_WORK_SERVER_ASSIGNMENT = "request_work_server_assignment"
SERVICE_SCHEMA = vol.Schema(
{vol.Required(SERVICE_ADDRESS): cv.string}
)


async def async_setup_services(hass):
"""Set up services for FoldingAtHomeControl integration."""
Expand All @@ -33,12 +47,36 @@ async def async_call_folding_at_home_control_service(service_call):

if service == SERVICE_REQUEST_WORK_SERVER_ASSIGNMENT:
await async_request_assignment_service(hass, service_data)
if service == SERVICE_PAUSE:
await async_pause_service(hass, service_data)
if service == SERVICE_UNPAUSE:
await async_unpause_service(hass, service_data)
if service == SERVICE_SHUTDOWN:
await async_shutdown_service(hass, service_data)

hass.services.async_register(
DOMAIN,
SERVICE_PAUSE,
async_call_folding_at_home_control_service,
schema=SERVICE_SLOT_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_UNPAUSE,
async_call_folding_at_home_control_service,
schema=SERVICE_SLOT_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_REQUEST_WORK_SERVER_ASSIGNMENT,
async_call_folding_at_home_control_service,
schema=SERVICE_REQUEST_WORK_SERVER_ASSIGNMENT_SCHEMA,
schema=SERVICE_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_SHUTDOWN,
async_call_folding_at_home_control_service,
schema=SERVICE_SCHEMA,
)


Expand All @@ -49,9 +87,66 @@ async def async_unload_services(hass):

hass.data[DOMAIN_SERVICES] = False

hass.services.async_remove(DOMAIN, SERVICE_PAUSE)
hass.services.async_remove(DOMAIN, SERVICE_UNPAUSE)
hass.services.async_remove(DOMAIN, SERVICE_SHUTDOWN)
hass.services.async_remove(DOMAIN, SERVICE_REQUEST_WORK_SERVER_ASSIGNMENT)


async def async_pause_service(hass, data):
"""Let the client pause one or all slots."""

address = data[SERVICE_ADDRESS]
slot = data[SERVICE_SLOT]

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)
return
await hass.data[DOMAIN][
config_entry
].client.pause_all_slots_async()
return
_LOGGER.warning("Could not find a registered integration with address: %s", address)


async def async_unpause_service(hass, data):
"""Let the client unpause one or all slots."""

address = data[SERVICE_ADDRESS]
slot = data[SERVICE_SLOT]

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)
return
await hass.data[DOMAIN][
config_entry
].client.unpause_all_slots_async()
return
_LOGGER.warning("Could not find a registered integration with address: %s", address)


async def async_shutdown_service(hass, data):
"""Let the client shutdown."""

address = data[SERVICE_ADDRESS]

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()
return
_LOGGER.warning("Could not find a registered integration with address: %s", address)


async def async_request_assignment_service(hass, data):
"""Let the client request a new work server assignment."""

Expand Down
27 changes: 27 additions & 0 deletions custom_components/foldingathomecontrol/services.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
pause:
description: Pause one or all slots.
fields:
address:
description: The IP address or hostname of the client. It can be found as part of the integration name.
example: 'localhost'
slot:
description: The slot to pause. Be sure to include the 0 in front if needed. Leave this out to pause all slots.
example: "01"

unpause:
description: Unpause one or all slots.
fields:
address:
description: The IP address or hostname of the client. It can be found as part of the integration name.
example: 'localhost'
slot:
description: The slot to unpause. Be sure to include the 0 in front if needed. Leave this out to unpause all slots.
example: "01"

shutdown:
description: Shut down the client.
fields:
address:
description: The IP address or hostname of the client. It can be found as part of the integration name.
example: 'localhost'

request_work_server_assignment:
description: Request a new assignment from the work server.
fields:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PyFoldingAtHomeControl==1.0.0
PyFoldingAtHomeControl==1.1.0

0 comments on commit 449edb9

Please sign in to comment.