Skip to content

Commit

Permalink
add target
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaclav committed Jan 6, 2022
1 parent bacc143 commit 67363f3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 40 deletions.
78 changes: 40 additions & 38 deletions custom_components/garbage_collection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@

COLLECT_NOW_SCHEMA = vol.Schema(
{
vol.Required(CONF_ENTITY_ID): cv.string,
vol.Required(CONF_ENTITY_ID): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(ATTR_LAST_COLLECTION): cv.datetime,
}
)

UPDATE_STATE_SCHEMA = vol.Schema(
{
vol.Required(CONF_ENTITY_ID): cv.string,
vol.Required(CONF_ENTITY_ID): vol.All(cv.ensure_list, [cv.string]),
}
)

ADD_REMOVE_DATE_SCHEMA = vol.Schema(
{
vol.Required(CONF_ENTITY_ID): cv.string,
vol.Required(CONF_ENTITY_ID): vol.All(cv.ensure_list, [cv.string]),
vol.Required(CONF_DATE): cv.date,
}
)
Expand All @@ -63,50 +63,52 @@ async def async_setup(hass, config):

async def handle_add_date(call):
"""Handle the add_date service call."""
entity_id = call.data.get(CONF_ENTITY_ID)
collection_date = call.data.get(CONF_DATE)
_LOGGER.debug("called add_date %s to %s", collection_date, entity_id)
try:
entity = hass.data[DOMAIN][SENSOR_PLATFORM][entity_id]
await entity.add_date(collection_date)
except Exception as err:
_LOGGER.error("Failed adding date for %s - %s", entity_id, err)
for entity_id in call.data.get(CONF_ENTITY_ID):
collection_date = call.data.get(CONF_DATE)
_LOGGER.debug("called add_date %s to %s", collection_date, entity_id)
try:
entity = hass.data[DOMAIN][SENSOR_PLATFORM][entity_id]
await entity.add_date(collection_date)
except Exception as err:
_LOGGER.error("Failed adding date for %s - %s", entity_id, err)

async def handle_remove_date(call):
"""Handle the remove_date service call."""
entity_id = call.data.get(CONF_ENTITY_ID)
collection_date = call.data.get(CONF_DATE)
_LOGGER.debug("called remove_date %s to %s", collection_date, entity_id)
try:
entity = hass.data[DOMAIN][SENSOR_PLATFORM][entity_id]
await entity.remove_date(collection_date)
except Exception as err:
_LOGGER.error("Failed removing date for %s - %s", entity_id, err)
for entity_id in call.data.get(CONF_ENTITY_ID):
collection_date = call.data.get(CONF_DATE)
_LOGGER.debug("called remove_date %s to %s", collection_date, entity_id)
try:
entity = hass.data[DOMAIN][SENSOR_PLATFORM][entity_id]
await entity.remove_date(collection_date)
except Exception as err:
_LOGGER.error("Failed removing date for %s - %s", entity_id, err)

async def handle_update_state(call):
"""Handle the update_state service call."""
entity_id = call.data.get(CONF_ENTITY_ID)
_LOGGER.debug("called update_state for %s", entity_id)
try:
entity = hass.data[DOMAIN][SENSOR_PLATFORM][entity_id]
await entity.async_update_state()
except Exception as err:
_LOGGER.error("Failed updating state for %s - %s", entity_id, err)
for entity_id in call.data.get(CONF_ENTITY_ID):
_LOGGER.debug("called update_state for %s", entity_id)
try:
entity = hass.data[DOMAIN][SENSOR_PLATFORM][entity_id]
await entity.async_update_state()
except Exception as err:
_LOGGER.error("Failed updating state for %s - %s", entity_id, err)

async def handle_collect_garbage(call):
"""Handle the collect_garbage service call."""
entity_id = call.data.get(CONF_ENTITY_ID)
last_collection = call.data.get(ATTR_LAST_COLLECTION)
_LOGGER.debug("called collect_garbage for %s", entity_id)
try:
entity = hass.data[DOMAIN][SENSOR_PLATFORM][entity_id]
if last_collection is None:
entity.last_collection = dt_util.now()
else:
entity.last_collection = dt_util.as_local(last_collection)
await entity.async_update_state()
except Exception as err:
_LOGGER.error("Failed setting last collection for %s - %s", entity_id, err)
for entity_id in call.data.get(CONF_ENTITY_ID):
last_collection = call.data.get(ATTR_LAST_COLLECTION)
_LOGGER.debug("called collect_garbage for %s", entity_id)
try:
entity = hass.data[DOMAIN][SENSOR_PLATFORM][entity_id]
if last_collection is None:
entity.last_collection = dt_util.now()
else:
entity.last_collection = dt_util.as_local(last_collection)
await entity.async_update_state()
except Exception as err:
_LOGGER.error(
"Failed setting last collection for %s - %s", entity_id, err
)

if DOMAIN not in hass.services.async_services():
hass.services.async_register(
Expand Down
16 changes: 14 additions & 2 deletions custom_components/garbage_collection/services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
collect_garbage:
description: Set the last_collection attribute to the current date and time.
target:
entity:
integration: garbage_collection
fields:
entity_id:
description: The garbage_collection sensor entity_id
Expand All @@ -9,24 +12,33 @@ collect_garbage:
example: "2020-08-16 10:54:00"
add_date:
description: Manually add collection date.
target:
entity:
integration: garbage_collection
fields:
entity_id:
description: The garbage_collection sensor entity_id
example: sensor.general_waste
date:
description: Collection date to add
example: "2020-08-16"
example: '"2020-08-16"'
remove_date:
description: Remove automatically calculated collection date.
target:
entity:
integration: garbage_collection
fields:
entity_id:
description: The garbage_collection sensor entity_id
example: sensor.general_waste
date:
description: Collection date to remove
example: "2020-08-16"
example: '"2020-08-16"'
update_state:
description: Update the entity state and attributes. Used with the manual_update option, do defer the update after changing the automatically created schedule by automation trigered by the garbage_collection_loaded event.
target:
entity:
integration: garbage_collection
fields:
entity_id:
description: The garbage_collection sensor entity_id
Expand Down

0 comments on commit 67363f3

Please sign in to comment.