Skip to content

Commit

Permalink
maint: Re-organise autoreply sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Oct 29, 2023
1 parent 3bb387d commit 19b116f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
16 changes: 6 additions & 10 deletions custom_components/o365/classes/teamssensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ATTR_IMPORTANCE,
ATTR_SUBJECT,
ATTR_SUMMARY,
CONF_ACCOUNT,
DOMAIN,
EVENT_HA_EVENT,
EVENT_SEND_CHAT_MESSAGE,
Expand All @@ -28,12 +29,10 @@
class O365TeamsSensor(O365Sensor):
"""O365 Teams sensor processing."""

def __init__(
self, cordinator, account, name, entity_id, config, entity_type, unique_id
):
def __init__(self, cordinator, name, entity_id, config, entity_type, unique_id):
"""Initialise the Teams Sensor."""
super().__init__(cordinator, config, name, entity_id, entity_type, unique_id)
self.teams = account.teams()
self.teams = self._config[CONF_ACCOUNT].teams()

@property
def icon(self):
Expand All @@ -44,11 +43,10 @@ def icon(self):
class O365TeamsStatusSensor(O365TeamsSensor, SensorEntity):
"""O365 Teams sensor processing."""

def __init__(self, coordinator, account, name, entity_id, config, unique_id):
def __init__(self, coordinator, name, entity_id, config, unique_id):
"""Initialise the Teams Sensor."""
super().__init__(
coordinator,
account,
name,
entity_id,
config,
Expand All @@ -60,12 +58,10 @@ def __init__(self, coordinator, account, name, entity_id, config, unique_id):
class O365TeamsChatSensor(O365TeamsSensor, SensorEntity):
"""O365 Teams Chat sensor processing."""

def __init__(
self, coordinator, account, name, entity_id, config, unique_id, enable_update
):
def __init__(self, coordinator, name, entity_id, config, unique_id, enable_update):
"""Initialise the Teams Chat Sensor."""
super().__init__(
coordinator, account, name, entity_id, config, SENSOR_TEAMS_CHAT, unique_id
coordinator, name, entity_id, config, SENSOR_TEAMS_CHAT, unique_id
)
self.enable_update = enable_update

Expand Down
37 changes: 21 additions & 16 deletions custom_components/o365/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from homeassistant.util import dt
from requests.exceptions import HTTPError

from .classes.mailsensor import O365AutoReplySensor, O365EmailSensor, O365QuerySensor
from .classes.mailsensor import O365EmailSensor, O365QuerySensor
from .classes.taskssensor import O365TasksSensorSensorServices
from .const import (
ATTR_ATTRIBUTES,
Expand Down Expand Up @@ -95,8 +95,8 @@ async def async_setup_entries(self):
chat_keys = self._chat_sensors()
todo_keys = await self._async_todo_sensors()
auto_reply_entities = await self._async_auto_reply_sensors()
self._entities = email_entities + query_entities + auto_reply_entities
self._keys = chat_keys + status_keys + todo_keys
self._entities = email_entities + query_entities
self._keys = chat_keys + status_keys + todo_keys + auto_reply_entities
return self._entities, self._keys

async def _async_email_sensors(self):
Expand Down Expand Up @@ -245,16 +245,18 @@ async def _async_todo_entities(self, task_lists):

async def _async_auto_reply_sensors(self):
auto_reply_sensors = self._config.get(CONF_AUTO_REPLY_SENSORS, [])
entities = []
keys = []
for sensor_conf in auto_reply_sensors:
name = sensor_conf.get(CONF_NAME)
entity_id = self._build_entity_id(name)
unique_id = f"{name}_{self._account_name}"
auto_reply_sensor = O365AutoReplySensor(
self, name, entity_id, self._config, unique_id
)
entities.append(auto_reply_sensor)
return entities
new_key = {
CONF_ENTITY_KEY: self._build_entity_id(name),
CONF_UNIQUE_ID: f"{name}_{self._account_name}",
CONF_NAME: name,
CONF_ENTITY_TYPE: SENSOR_AUTO_REPLY,
}

keys.append(new_key)
return keys

async def _async_get_mail_folder(self, sensor_conf, sensor_type):
"""Get the configured folder."""
Expand Down Expand Up @@ -294,8 +296,6 @@ async def _async_update_data(self):
for entity in self._entities:
if entity.entity_type == SENSOR_MAIL:
await self._async_email_update(entity)
elif entity.entity_type == SENSOR_AUTO_REPLY:
await self._async_auto_reply_update(entity)

for key in self._keys:
entity_type = key[CONF_ENTITY_TYPE]
Expand All @@ -305,6 +305,8 @@ async def _async_update_data(self):
await self._async_teams_chat_update(key)
elif entity_type == SENSOR_TEAMS_STATUS:
await self._async_teams_status_update(key)
elif entity_type == SENSOR_AUTO_REPLY:
await self._async_auto_reply_update(key)

return self._data

Expand Down Expand Up @@ -463,10 +465,13 @@ def _create_todo_query(self, key, todo):
)
return query

async def _async_auto_reply_update(self, entity):
async def _async_auto_reply_update(self, key):
"""Update state."""
if data := await self.hass.async_add_executor_job(entity.mailbox.get_settings):
self._data[entity.entity_key] = {
entity_key = key[CONF_ENTITY_KEY]
if data := await self.hass.async_add_executor_job(
self._account.mailbox().get_settings
):
self._data[entity_key] = {
ATTR_STATE: data.automaticrepliessettings.status.value,
ATTR_AUTOREPLIESSETTINGS: data.automaticrepliessettings,
}
Expand Down
25 changes: 17 additions & 8 deletions custom_components/o365/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
from homeassistant.const import CONF_ENABLED, CONF_NAME, CONF_UNIQUE_ID
from homeassistant.helpers import entity_platform

from .classes.mailsensor import O365AutoReplySensor
from .classes.taskssensor import O365TasksSensor, O365TasksSensorSensorServices
from .classes.teamssensor import O365TeamsChatSensor, O365TeamsStatusSensor
from .const import CONF_ACCOUNT_NAME # SENSOR_TODO,
from .const import (
CONF_ACCOUNT,
CONF_ACCOUNT_NAME,
CONF_AUTO_REPLY_SENSORS,
CONF_CHAT_SENSORS,
CONF_COORDINATOR,
Expand Down Expand Up @@ -68,9 +69,9 @@ async def async_setup_platform(
in [
SENSOR_MAIL,
# SENSOR_TEAMS_STATUS,
SENSOR_TEAMS_CHAT,
# SENSOR_TEAMS_CHAT,
# SENSOR_TODO,
SENSOR_AUTO_REPLY,
# SENSOR_AUTO_REPLY,
]
]
coordinator = conf[CONF_COORDINATOR]
Expand All @@ -82,7 +83,7 @@ async def async_setup_platform(
key[CONF_TODO],
key[CONF_NAME],
key[CONF_TASK_LIST],
config,
conf,
key[CONF_ENTITY_KEY],
key[CONF_UNIQUE_ID],
)
Expand All @@ -91,10 +92,9 @@ async def async_setup_platform(
sensorentities.append(
O365TeamsChatSensor(
coordinator,
account,
key[CONF_NAME],
key[CONF_ENTITY_KEY],
config,
conf,
key[CONF_UNIQUE_ID],
key[CONF_ENABLE_UPDATE],
)
Expand All @@ -103,10 +103,19 @@ async def async_setup_platform(
sensorentities.append(
O365TeamsStatusSensor(
coordinator,
account,
key[CONF_NAME],
key[CONF_ENTITY_KEY],
config,
conf,
key[CONF_UNIQUE_ID],
)
)
elif key[CONF_ENTITY_TYPE] == SENSOR_AUTO_REPLY:
sensorentities.append(
O365AutoReplySensor(
coordinator,
key[CONF_NAME],
key[CONF_ENTITY_KEY],
conf,
key[CONF_UNIQUE_ID],
)
)
Expand Down

0 comments on commit 19b116f

Please sign in to comment.