Skip to content

Commit

Permalink
add test notification option
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasJoKuJonas committed Jan 15, 2024
1 parent fd7a696 commit ca3b807
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 20 deletions.
12 changes: 2 additions & 10 deletions custom_components/webuntis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)
from .notify import *
from .services import async_setup_services
from .utils import compact_list, get_schoolyear
from .utils import compact_list, get_schoolyear, async_notify

PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.CALENDAR]

Expand Down Expand Up @@ -926,7 +926,7 @@ async def update_notify(self):
notification["target"] = self.notify_target
notification["data"] = self.notify_data
try:
await self.async_notify(
await async_notify(
self._hass, service=self.notify_entity_id, data=notification
)
except Exception as error:
Expand All @@ -937,14 +937,6 @@ async def update_notify(self):
)
self.event_list_old = self.event_list

async def async_notify(self, hass, service, data):
"""Show a notification"""
_LOGGER.debug("Send notification(%s): %s", service, data)

domain = service.split(".")[0]
service = service.split(".")[1]

await hass.services.async_call(domain, service, data, blocking=True)


class WebUntisEntity(Entity):
Expand Down
58 changes: 48 additions & 10 deletions custom_components/webuntis/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


from .const import CONFIG_ENTRY_VERSION, DEFAULT_OPTIONS, DOMAIN, NOTIFY_OPTIONS
from .utils import is_service
from .utils import is_service, async_notify

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -238,27 +238,26 @@ def _show_form_user(
)


OPTIONS_MENU = {
"filter": "Filter",
"calendar": "Calendar",
"notify": "Notify",
"backend": "Backend",
}


class OptionsFlowHandler(config_entries.OptionsFlow):
"""Handle the option flow for WebUntis."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
self.OPTIONS_MENU = {
"filter": "Filter",
"calendar": "Calendar",
"notify": "Notify",
"backend": "Backend",
"test": "Test",
}

async def async_step_init(
self,
user_input: dict[str, Any] | None = None, # pylint: disable=unused-argument
) -> FlowResult:
"""Manage the options."""
return self.async_show_menu(step_id="init", menu_options=OPTIONS_MENU)
return self.async_show_menu(step_id="init", menu_options=self.OPTIONS_MENU)

async def save(self, user_input):
"""Save the options"""
Expand Down Expand Up @@ -508,6 +507,45 @@ async def async_step_notify(
errors=errors,
)

async def async_step_test(
self,
user_input: dict[str, str] = None,
errors: dict[str, Any] | None = None,
) -> FlowResult:
"""Manage the test options."""
if user_input is None:
return self.async_show_form(
step_id="test",
data_schema=vol.Schema(
{
vol.Optional(
"tests", default="notify"
): selector.SelectSelector(
selector.SelectSelectorConfig(options=["notify"])
),
}
),
errors=errors,
)
else:
if user_input["tests"] == "notify":
options = dict(self.config_entry.options)
notification = {
"title": "WebUntis - Test message",
"message": "Subject: Demo\nDate: {}\nTime: {}".format(
*(datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S").split())
),
}
notification["target"] = options.get("notify_target")
notification["data"] = options["notify_data"]
notify_entity_id = options.get("notify_entity_id")

await async_notify(
self.hass, service=notify_entity_id, data=notification
)
pass
return await self.save({})


def _create_subject_list(server):
"""Create a list of subjects."""
Expand Down
6 changes: 6 additions & 0 deletions custom_components/webuntis/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
"data_description": {
"notify_data": "z. B.:\n Benachrichtigungssymbol: mdi:school-outline"
}
},
"test": {
"title": "Test Menü",
"data": {
"tests": "Option zum testen"
}
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions custom_components/webuntis/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
"data_description": {
"notify_data": "e.g.,\nnotification_icon: mdi:school-outline"
}
},
"test": {
"title": "Test Menu",
"data": {
"tests": "Option for testing"
}
}
}
},
Expand Down
17 changes: 17 additions & 0 deletions custom_components/webuntis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@


from datetime import datetime
import logging

_LOGGER = logging.getLogger(__name__)


def is_service(hass, entry):
Expand Down Expand Up @@ -96,3 +99,17 @@ def get_schoolyear(school_year, date=datetime.now().date()):
return time_range

return None


async def async_notify(hass, service, data):
"""Show a notification"""

if "target" in data and not data["target"]:
del data["target"]

_LOGGER.debug("Send notification(%s): %s", service, data)

domain = service.split(".")[0]
service = service.split(".")[1]

await hass.services.async_call(domain, service, data, blocking=True)

0 comments on commit ca3b807

Please sign in to comment.