Skip to content

Commit

Permalink
Improve schedule services.
Browse files Browse the repository at this point in the history
Allow to select multiple weekdays to get/set a schedule for.
  • Loading branch information
denpamusic committed Oct 15, 2023
1 parent 4e8e2a7 commit e341541
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 104 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,10 @@ Allows to get different schedules from the device.

#### Fields

| Field | Type | Description |
|---------|---------------------------|--------------------------|
| type | "heating", "water_heater" | Type of the schedule. |
| weekday | "monday", "tuesday", ... | Weekday of the schedule. |
| Field | Type | Description |
|----------|---------------------------|--------------------------|
| type | "heating", "water_heater" | Type of the schedule. |
| weekdays | "monday", "tuesday", ... | Weekdays of the schedule. |

#### Response

Expand All @@ -390,13 +390,13 @@ Allows to set different schedules on the device.

#### Fields

| Field | Type | Description |
|---------|---------------------------|---------------------------|
| type | "heating", "water_heater" | Type of the schedule. |
| weekday | "monday", "tuesday", ... | Weekday of the schedule. |
| preset | "day", "night" | Preset at scheduled time. |
| start | time | Time of schedule start. |
| end | time | Time of schedule end. |
| Field | Type | Description |
|----------|---------------------------|---------------------------|
| type | "heating", "water_heater" | Type of the schedule. |
| weekdays | "monday", "tuesday", ... | Weekdays of the schedule. |
| preset | "day", "night" | Preset at scheduled time. |
| start | time | Time of schedule start. |
| end | time | Time of schedule end. |

### Calibrate meter
Allows to set meter entity to a specific value.<br>
Expand Down
2 changes: 1 addition & 1 deletion custom_components/plum_ecomax/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
ATTR_VALUE: Final = "value"
ATTR_WATER_HEATER: Final = "water_heater"
ATTR_WATER_HEATER_TEMP: Final = "water_heater_temp"
ATTR_WEEKDAY: Final = "weekday"
ATTR_WEEKDAYS: Final = "weekdays"
ATTR_FIRMWARE: Final = "firmware"
ATTR_REGDATA: Final = "regdata"

Expand Down
72 changes: 43 additions & 29 deletions custom_components/plum_ecomax/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
)
from pyplumio.devices import Device
from pyplumio.exceptions import ParameterNotFoundError
from pyplumio.helpers.schedule import START_OF_DAY, STATE_DAY, STATE_NIGHT, TIME_FORMAT
from pyplumio.helpers.schedule import (
START_OF_DAY,
STATE_DAY,
STATE_NIGHT,
TIME_FORMAT,
ScheduleDay,
)
import voluptuous as vol

from .connection import EcomaxConnection
Expand All @@ -36,7 +42,7 @@
ATTR_START,
ATTR_TYPE,
ATTR_VALUE,
ATTR_WEEKDAY,
ATTR_WEEKDAYS,
DOMAIN,
WEEKDAYS,
)
Expand Down Expand Up @@ -70,21 +76,23 @@
SERVICE_GET_SCHEDULE_SCHEMA = vol.Schema(
{
vol.Required(ATTR_TYPE): vol.All(str, vol.In(SCHEDULES)),
vol.Required(ATTR_WEEKDAY): vol.All(str, vol.In(WEEKDAYS)),
vol.Required(ATTR_WEEKDAYS): vol.All(cv.ensure_list, [vol.In(WEEKDAYS)]),
}
)

SERVICE_SET_SCHEDULE = "set_schedule"
SERVICE_SET_SCHEDULE_SCHEMA = vol.Schema(
{
vol.Required(ATTR_TYPE): vol.All(str, vol.In(SCHEDULES)),
vol.Required(ATTR_WEEKDAY): vol.All(str, vol.In(WEEKDAYS)),
vol.Required(ATTR_WEEKDAYS): vol.All(cv.ensure_list, [vol.In(WEEKDAYS)]),
vol.Required(ATTR_PRESET): vol.All(str, vol.In(PRESETS)),
vol.Optional(ATTR_START, default="00:00:00"): vol.Datetime("%H:%M:%S"),
vol.Optional(ATTR_END, default="00:00:00"): vol.Datetime("%H:%M:%S"),
}
)

START_OF_DAY_DT = dt.datetime.strptime(START_OF_DAY, TIME_FORMAT)

_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -234,6 +242,16 @@ async def async_set_parameter_service(service_call: ServiceCall) -> None:
)


def async_schedule_day_to_dict(schedule_day: ScheduleDay):
"""Format schedule day as dictionary."""
return {
(START_OF_DAY_DT + dt.timedelta(minutes=30 * index)).strftime(TIME_FORMAT): (
STATE_DAY if value else STATE_NIGHT
)
for index, value in enumerate(schedule_day.intervals)
}


@callback
def async_setup_get_schedule_service(
hass: HomeAssistant, connection: EcomaxConnection
Expand All @@ -243,25 +261,21 @@ def async_setup_get_schedule_service(
async def async_get_schedule_service(service_call: ServiceCall) -> ServiceResponse:
"""Service to get a schedule."""
schedule_type = service_call.data[ATTR_TYPE]
weekday = service_call.data[ATTR_WEEKDAY]
weekdays = service_call.data[ATTR_WEEKDAYS]

schedules = connection.device.get_nowait(ATTR_SCHEDULES, {})
if schedule_type in schedules:
schedule = schedules[schedule_type]
schedule_day = getattr(schedule, weekday)
start_of_day_dt = dt.datetime.strptime(START_OF_DAY, TIME_FORMAT)
return {
"schedule": {
(start_of_day_dt + dt.timedelta(minutes=30 * index)).strftime(
TIME_FORMAT
): (STATE_DAY if value else STATE_NIGHT)
for index, value in enumerate(schedule_day.intervals)
}
}
if schedule_type not in schedules:
raise HomeAssistantError(
f"{schedule_type} schedule is not supported by the device, check logs for more info"
)

raise HomeAssistantError(
f"{schedule_type} schedule is not supported by the device, check logs for more info"
)
schedule = schedules[schedule_type]
return {
"schedule": {
weekday: async_schedule_day_to_dict(getattr(schedule, weekday))
for weekday in weekdays
}
}

hass.services.async_register(
DOMAIN,
Expand All @@ -281,14 +295,19 @@ def async_setup_set_schedule_service(
async def async_set_schedule_service(service_call: ServiceCall) -> None:
"""Service to set a schedule."""
schedule_type = service_call.data[ATTR_TYPE]
weekday = service_call.data[ATTR_WEEKDAY]
weekdays = service_call.data[ATTR_WEEKDAYS]
preset = service_call.data[ATTR_PRESET]
start_time = service_call.data[ATTR_START]
end_time = service_call.data[ATTR_END]

schedules = connection.device.get_nowait(ATTR_SCHEDULES, {})
if schedule_type in schedules:
schedule = schedules[schedule_type]
if schedule_type not in schedules:
raise HomeAssistantError(
f"{schedule_type} schedule is not supported by the device, check logs for more info"
)

schedule = schedules[schedule_type]
for weekday in weekdays:
schedule_day = getattr(schedule, weekday)
try:
schedule_day.set_state(preset, start_time[:-3], end_time[:-3])
Expand All @@ -297,12 +316,7 @@ async def async_set_schedule_service(service_call: ServiceCall) -> None:
f"Error while trying to parse time interval for {schedule_type} schedule"
) from e

schedule.commit()
return

raise HomeAssistantError(
f"{schedule_type} schedule is not supported by the device, check logs for more info"
)
schedule.commit()

hass.services.async_register(
DOMAIN,
Expand Down
10 changes: 6 additions & 4 deletions custom_components/plum_ecomax/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ get_schedule:
options:
- "heating"
- "water_heater"
weekday:
weekdays:
example: "monday"
required: true
selector:
select:
translation_key: "weekday"
translation_key: "weekdays"
multiple: true
options:
- "monday"
- "tuesday"
Expand All @@ -62,12 +63,13 @@ set_schedule:
options:
- "heating"
- "water_heater"
weekday:
weekdays:
example: "monday"
required: true
selector:
select:
translation_key: "weekday"
translation_key: "weekdays"
multiple: true
options:
- "monday"
- "tuesday"
Expand Down
14 changes: 7 additions & 7 deletions custom_components/plum_ecomax/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
"water_heater": "Water heater"
}
},
"weekday": {
"weekdays": {
"options": {
"monday": "Monday",
"tuesday": "Tuesday",
Expand Down Expand Up @@ -228,9 +228,9 @@
"name": "Type",
"description": "Type of the schedule"
},
"weekday": {
"name": "Weekday",
"description": "Weekday to get the schedule"
"weekdays": {
"name": "Weekdays",
"description": "Weekdays to get the schedule"
}
}
},
Expand All @@ -242,9 +242,9 @@
"name": "Type",
"description": "Type of the schedule"
},
"weekday": {
"name": "Weekday",
"description": "Weekday to set the schedule"
"weekdays": {
"name": "Weekdays",
"description": "Weekdays to set the schedule"
},
"preset": {
"name": "Preset",
Expand Down
14 changes: 7 additions & 7 deletions custom_components/plum_ecomax/translations/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
"water_heater": "Water heater"
}
},
"weekday": {
"weekdays": {
"options": {
"monday": "Monday",
"tuesday": "Tuesday",
Expand Down Expand Up @@ -228,9 +228,9 @@
"name": "Type",
"description": "Type of the schedule"
},
"weekday": {
"name": "Weekday",
"description": "Weekday to get the schedule"
"weekdays": {
"name": "Weekdays",
"description": "Weekdays for which to get the schedules"
}
}
},
Expand All @@ -242,9 +242,9 @@
"name": "Type",
"description": "Type of the schedule"
},
"weekday": {
"name": "Weekday",
"description": "Weekday to set the schedule"
"weekdays": {
"name": "Weekdays",
"description": "Weekdays for which to set the schedules"
},
"preset": {
"name": "Preset",
Expand Down
14 changes: 7 additions & 7 deletions custom_components/plum_ecomax/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
"water_heater": "Water heater"
}
},
"weekday": {
"weekdays": {
"options": {
"monday": "Monday",
"tuesday": "Tuesday",
Expand Down Expand Up @@ -228,9 +228,9 @@
"name": "Type",
"description": "Type of the schedule"
},
"weekday": {
"name": "Weekday",
"description": "Weekday to get the schedule"
"weekdays": {
"name": "Weekdays",
"description": "Weekdays for which to get the schedules"
}
}
},
Expand All @@ -242,9 +242,9 @@
"name": "Type",
"description": "Type of the schedule"
},
"weekday": {
"name": "Weekday",
"description": "Weekday to set the schedule"
"weekdays": {
"name": "Weekdays",
"description": "Weekdays for which to set the schedules"
},
"preset": {
"name": "Preset",
Expand Down
14 changes: 7 additions & 7 deletions custom_components/plum_ecomax/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
"water_heater": "Water heater"
}
},
"weekday": {
"weekdays": {
"options": {
"monday": "Monday",
"tuesday": "Tuesday",
Expand Down Expand Up @@ -228,9 +228,9 @@
"name": "Type",
"description": "Type of the schedule"
},
"weekday": {
"name": "Weekday",
"description": "Weekday to get the schedule"
"weekdays": {
"name": "Weekdays",
"description": "Weekdays for which to get the schedules"
}
}
},
Expand All @@ -242,9 +242,9 @@
"name": "Type",
"description": "Type of the schedule"
},
"weekday": {
"name": "Weekday",
"description": "Weekday to set the schedule"
"weekdays": {
"name": "Weekdays",
"description": "Weekdays for which to set the schedules"
},
"preset": {
"name": "Preset",
Expand Down
14 changes: 7 additions & 7 deletions custom_components/plum_ecomax/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
"water_heater": "Water heater"
}
},
"weekday": {
"weekdays": {
"options": {
"monday": "Monday",
"tuesday": "Tuesday",
Expand Down Expand Up @@ -228,9 +228,9 @@
"name": "Type",
"description": "Type of the schedule"
},
"weekday": {
"name": "Weekday",
"description": "Weekday to get the schedule"
"weekdays": {
"name": "Weekdays",
"description": "Weekdays for which to get the schedules"
}
}
},
Expand All @@ -242,9 +242,9 @@
"name": "Type",
"description": "Type of the schedule"
},
"weekday": {
"name": "Weekday",
"description": "Weekday to set the schedule"
"weekdays": {
"name": "Weekdays",
"description": "Weekdays for which to set the schedules"
},
"preset": {
"name": "Preset",
Expand Down
Loading

0 comments on commit e341541

Please sign in to comment.