diff --git a/custom_components/o365/classes/teamssensor.py b/custom_components/o365/classes/teamssensor.py index 8d7aef9..03576f4 100644 --- a/custom_components/o365/classes/teamssensor.py +++ b/custom_components/o365/classes/teamssensor.py @@ -12,6 +12,7 @@ ATTR_IMPORTANCE, ATTR_SUBJECT, ATTR_SUMMARY, + CONF_ACCOUNT, DOMAIN, EVENT_HA_EVENT, EVENT_SEND_CHAT_MESSAGE, @@ -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): @@ -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, @@ -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 diff --git a/custom_components/o365/coordinator.py b/custom_components/o365/coordinator.py index eec6ea8..7092e13 100644 --- a/custom_components/o365/coordinator.py +++ b/custom_components/o365/coordinator.py @@ -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, @@ -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): @@ -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.""" @@ -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] @@ -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 @@ -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, } diff --git a/custom_components/o365/sensor.py b/custom_components/o365/sensor.py index 8405c64..be8abbd 100644 --- a/custom_components/o365/sensor.py +++ b/custom_components/o365/sensor.py @@ -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, @@ -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] @@ -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], ) @@ -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], ) @@ -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], ) )