Skip to content

Commit

Permalink
Add existing auto reply settings display
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Jan 17, 2023
1 parent c507f3d commit 84132fa
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
45 changes: 31 additions & 14 deletions custom_components/o365/classes/mailsensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

from ..const import (
ATTR_ATTRIBUTES,
ATTR_AUTOREPLIESSETTINGS,
ATTR_END,
ATTR_EXTERNAL_AUDIENCE,
ATTR_EXTERNALREPLY,
ATTR_INTERNALREPLY,
ATTR_START,
CONF_ACCOUNT,
CONF_BODY_CONTAINS,
CONF_CONFIG_TYPE,
Expand All @@ -19,12 +25,18 @@
CONF_MAX_ITEMS,
CONF_SUBJECT_CONTAINS,
CONF_SUBJECT_IS,
DATETIME_FORMAT,
PERM_MAILBOX_SETTINGS,
PERM_MINIMUM_MAILBOX_SETTINGS,
SENSOR_AUTO_REPLY,
SENSOR_MAIL,
)
from ..utils import build_token_filename, get_permissions, validate_minimum_permission
from ..utils import (
build_token_filename,
clean_html,
get_permissions,
validate_minimum_permission,
)
from .sensorentity import O365Sensor


Expand Down Expand Up @@ -131,23 +143,32 @@ def __init__(


class O365AutoReplySensor(O365Sensor, SensorEntity):
"""O365 Tasks sensor processing."""
"""O365 Auto Reply sensor processing."""

def __init__(self, coordinator, name, entity_id, config, unqique_id):
"""Initialise the Tasks Sensor."""
"""Initialise the Auto reply Sensor."""
super().__init__(coordinator, name, entity_id, SENSOR_AUTO_REPLY, unqique_id)
self._config = config

@property
def state(self):
"""Sensor state."""
return "TBC"
account = self._config[CONF_ACCOUNT]
self.mailbox = account.mailbox()

@property
def icon(self):
"""Entity icon."""
return "mdi:reply-all"

@property
def extra_state_attributes(self):
"""Return entity specific state attributes."""
ars = self.coordinator.data[self.entity_key][ATTR_AUTOREPLIESSETTINGS]
return {
ATTR_INTERNALREPLY: clean_html(ars.internal_reply_message),
ATTR_EXTERNALREPLY: clean_html(ars.external_reply_message),
ATTR_EXTERNAL_AUDIENCE: ars.external_audience.value,
ATTR_START: ars.scheduled_startdatetime.strftime(DATETIME_FORMAT),
ATTR_END: ars.scheduled_enddatetime.strftime(DATETIME_FORMAT),
}

def auto_reply_enable(
self,
external_reply,
Expand All @@ -160,9 +181,7 @@ def auto_reply_enable(
if not self._validate_permissions():
return

account = self._config[CONF_ACCOUNT]
mailbox = account.mailbox()
mailbox.set_automatic_reply(
self.mailbox.set_automatic_reply(
internal_reply, external_reply, start, end, external_audience
)

Expand All @@ -171,9 +190,7 @@ def auto_reply_disable(self):
if not self._validate_permissions():
return

account = self._config[CONF_ACCOUNT]
mailbox = account.mailbox()
mailbox.set_disable_reply()
self.mailbox.set_disable_reply()

def _validate_permissions(self):
permissions = get_permissions(
Expand Down
1 change: 1 addition & 0 deletions custom_components/o365/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class EventResponse(Enum):
ATTR_ATTACHMENTS = "attachments"
ATTR_ATTRIBUTES = "attributes"
ATTR_ATTENDEES = "attendees"
ATTR_AUTOREPLIESSETTINGS = "autorepliessettings"
ATTR_BODY = "body"
ATTR_CALENDAR_ID = "calendar_id"
ATTR_CATEGORIES = "categories"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/o365/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@RogerSelwyn"
],
"requirements": [
"O365==2.0.24",
"O365==2.0.25",
"BeautifulSoup4>=4.10.0"
],
"version": "v4.0.8",
Expand Down
12 changes: 12 additions & 0 deletions custom_components/o365/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .classes.teamssensor import O365TeamsChatSensor, O365TeamsStatusSensor
from .const import (
ATTR_ATTRIBUTES,
ATTR_AUTOREPLIESSETTINGS,
ATTR_CHAT_ID,
ATTR_CONTENT,
ATTR_ERROR,
Expand Down Expand Up @@ -44,6 +45,7 @@
LEGACY_ACCOUNT_NAME,
PERM_MINIMUM_MAILBOX_SETTINGS,
PERM_MINIMUM_TASKS_WRITE,
SENSOR_AUTO_REPLY,
SENSOR_ENTITY_ID_FORMAT,
SENSOR_MAIL,
SENSOR_TEAMS_CHAT,
Expand Down Expand Up @@ -344,6 +346,8 @@ async def _async_update_data(self):
await self._async_teams_chat_update(entity)
elif entity.entity_type == SENSOR_TODO:
await self._async_todos_update(entity)
elif entity.entity_type == SENSOR_AUTO_REPLY:
await self._async_auto_reply_update(entity)

return self._data

Expand Down Expand Up @@ -430,6 +434,14 @@ async def _async_todos_update(self, entity):
error = True
self._data[entity.entity_key][ATTR_ERROR] = error

async def _async_auto_reply_update(self, entity):
"""Update state."""
if data := await self.hass.async_add_executor_job(entity.mailbox.get_settings):
self._data[entity.entity_key] = {
ATTR_STATE: data.automaticrepliessettings.status.value,
ATTR_AUTOREPLIESSETTINGS: data.automaticrepliessettings,
}


def _build_entity_id(hass, name, conf):
return async_generate_entity_id(
Expand Down

0 comments on commit 84132fa

Please sign in to comment.