Skip to content

Commit

Permalink
maint: Consolidate duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Jul 13, 2024
1 parent 08dc34c commit 1678928
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
10 changes: 10 additions & 0 deletions custom_components/ms365_mail/helpers/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Utilities processes."""

from bs4 import BeautifulSoup
from homeassistant.helpers.entity import async_generate_entity_id


def clean_html(html):
Expand All @@ -27,3 +28,12 @@ def add_attribute_to_item(item, user_input, attribute):
item[attribute] = user_input[attribute]
elif attribute in item:
del item[attribute]


def build_entity_id(hass, entity_id_format, name):
"""Build an entity ID."""
return async_generate_entity_id(
entity_id_format,
name,
hass=hass,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID
from homeassistant.core import HomeAssistant

# from homeassistant.helpers import entity_registry
from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

# from homeassistant.util import dt
from ..const import (
ATTR_DATA,
CONF_ENTITY_KEY,
CONF_ENTITY_NAME,
CONF_ENTITY_TYPE,
)
from ..helpers.utils import build_entity_id
from .const_integration import (
ATTR_AUTOREPLIESSETTINGS,
ATTR_STATE,
Expand Down Expand Up @@ -68,7 +65,7 @@ async def _async_email_sensors(self):
name = f"{self._entry.data[CONF_ENTITY_NAME]}_mail"
if mail_folder := await self._async_get_mail_folder():
new_key = {
CONF_ENTITY_KEY: _build_entity_id(
CONF_ENTITY_KEY: build_entity_id(
self.hass, ENTITY_ID_FORMAT_SENSOR, name
),
CONF_UNIQUE_ID: f"{mail_folder.folder_id}_{self._entity_name}",
Expand Down Expand Up @@ -121,7 +118,7 @@ async def _async_auto_reply_sensors(self):
if self._entry.data[CONF_ENABLE_AUTOREPLY]:
name = f"{self._entry.data[CONF_ENTITY_NAME]}_autoreply"
new_key = {
CONF_ENTITY_KEY: _build_entity_id(
CONF_ENTITY_KEY: build_entity_id(
self.hass, ENTITY_ID_FORMAT_SENSOR, name
),
CONF_UNIQUE_ID: name,
Expand Down Expand Up @@ -177,12 +174,3 @@ async def _async_auto_reply_update(self, key):
ATTR_STATE: data.automaticrepliessettings.status.value,
ATTR_AUTOREPLIESSETTINGS: data.automaticrepliessettings,
}


def _build_entity_id(hass, entity_id_format, name):
"""Build and entity ID."""
return async_generate_entity_id(
entity_id_format,
name,
hass=hass,
)

0 comments on commit 1678928

Please sign in to comment.