Skip to content

Commit

Permalink
allow unassigning schedule via service - Allow un-assigning schedule …
Browse files Browse the repository at this point in the history
…from wiser.assign_schedule service #470
  • Loading branch information
msp1974 committed May 4, 2024
1 parent 4ac51c4 commit bef3917
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 30 deletions.
83 changes: 56 additions & 27 deletions custom_components/wiser/schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,37 +147,66 @@ async def assign_schedule_by_id_or_name(
to_id = None
schedule_type = self.get_schedule_type()
schedule_identifier = (
"id " + str(schedule_id) if schedule_id else "name " + schedule_name
"id " + str(schedule_id)
if schedule_id is not None
else "name " + schedule_name
)
if hasattr(self, "room"):
to_id = self.room.id
room_name = self.room.name
_LOGGER.debug(
f"Assigning {schedule_type.value} schedule with {schedule_identifier} to room {room_name}"
)

if schedule_id == 0:
if hasattr(self, "room"):
if self.schedule:
_LOGGER.debug(
f"Unassigning {schedule_type.value} schedule with {self.schedule.id} from room {self.room.name}"
)
await self.schedule.unassign_schedule(self.room.id)
else:
_LOGGER.warning(
f"Unable to unassign schedule from {self.room.name} as no schedule is assigned"
)
else:
if self.schedule:
_LOGGER.debug(
f"Unassigning {schedule_type.value} schedule with {self.schedule.id} from room {self.device.name}"
)
await self.schedule.unassign_schedule(
self.device.device_type_id
)
await self.data.async_refresh()
else:
_LOGGER.warning(
f"Unable to unassign schedule from {self.device.name} as no schedule is assigned"
)
else:
to_id = self.device.device_type_id
device_name = self.device.name
_LOGGER.info(
f"Assigning {schedule_type.value} schedule with {schedule_identifier} to device {device_name}"
)
if hasattr(self, "room"):
to_id = self.room.id
room_name = self.room.name
_LOGGER.debug(
f"Assigning {schedule_type.value} schedule with {schedule_identifier} to room {room_name}"
)
else:
to_id = self.device.device_type_id
device_name = self.device.name
_LOGGER.info(
f"Assigning {schedule_type.value} schedule with {schedule_identifier} to device {device_name}"
)

if schedule_name:
schedule = self.data.wiserhub.schedules.get_by_name(
schedule_type, schedule_name
)
elif schedule_id:
schedule = self.data.wiserhub.schedules.get_by_id(
schedule_type, schedule_id
)
schedule = None
if schedule_name:
schedule = self.data.wiserhub.schedules.get_by_name(
schedule_type, schedule_name
)
elif schedule_id:
schedule = self.data.wiserhub.schedules.get_by_id(
schedule_type, schedule_id
)

if schedule:
await schedule.assign_schedule(to_id)
await self.data.async_refresh()
else:
_LOGGER.error(
f"Error assigning schedule to {self.name}. {schedule_type.value} schedule with {schedule_identifier} does not exist" # noqa: E501
)
if schedule:
await schedule.assign_schedule(to_id)
await self.data.async_refresh()
else:
_LOGGER.error(
f"Error assigning schedule to {self.name}. {schedule_type.value} schedule with {schedule_identifier} does not exist" # noqa: E501
)
except Exception as ex: # pylint: disable=broad-exception-caught
_LOGGER.error(
f"Error assigning schedule with id {schedule_id} to {self.name}. {ex}"
Expand Down
6 changes: 3 additions & 3 deletions custom_components/wiser/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async def assign_schedule(service_call):
schedule_name = service_call.data.get(ATTR_SCHEDULE_NAME)
to_entity_ids = service_call.data[ATTR_TO_ENTITY_ID]

if entity_id:
if entity_id is not None:
# Assign schedule from this entity to another
for to_entity_id in to_entity_ids:
from_entity = get_entity_from_entity_id(entity_id)
Expand All @@ -236,7 +236,7 @@ async def assign_schedule(service_call):
_LOGGER.error(
f"Invalid entity - {from_entity_id_text}{and_text}{to_entity_id_text} does not exist in this integration" # noqa=E501
)
elif schedule_id:
elif schedule_id is not None:
# Assign scheduel with id to this entity
for to_entity_id in to_entity_ids:
to_entity = get_entity_from_entity_id(to_entity_id)
Expand All @@ -248,7 +248,7 @@ async def assign_schedule(service_call):
_LOGGER.error(
f"Cannot assign schedule to entity {to_entity.name}. Please see wiki for entities to choose"
)
elif schedule_name:
elif schedule_name is not None:
# Assign schedule with name to this entity
for to_entity_id in to_entity_ids:
to_entity = get_entity_from_entity_id(to_entity_id)
Expand Down

0 comments on commit bef3917

Please sign in to comment.