Perform some Notion code cleanup (#58863)
This commit is contained in:
parent
5694250445
commit
595184aa55
5 changed files with 14 additions and 20 deletions
|
@ -9,7 +9,7 @@ from aionotion import async_get_client
|
||||||
from aionotion.errors import InvalidCredentialsError, NotionError
|
from aionotion.errors import InvalidCredentialsError, NotionError
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION, CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers import (
|
from homeassistant.helpers import (
|
||||||
|
@ -24,7 +24,7 @@ from homeassistant.helpers.update_coordinator import (
|
||||||
UpdateFailed,
|
UpdateFailed,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DOMAIN, LOGGER
|
from .const import DOMAIN, LOGGER
|
||||||
|
|
||||||
PLATFORMS = ["binary_sensor", "sensor"]
|
PLATFORMS = ["binary_sensor", "sensor"]
|
||||||
|
|
||||||
|
@ -39,9 +39,6 @@ CONFIG_SCHEMA = cv.deprecated(DOMAIN)
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Notion as a config entry."""
|
"""Set up Notion as a config entry."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = {}
|
|
||||||
|
|
||||||
if not entry.unique_id:
|
if not entry.unique_id:
|
||||||
hass.config_entries.async_update_entry(
|
hass.config_entries.async_update_entry(
|
||||||
entry, unique_id=entry.data[CONF_USERNAME]
|
entry, unique_id=entry.data[CONF_USERNAME]
|
||||||
|
@ -99,7 +96,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
)
|
)
|
||||||
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR] = coordinator
|
hass.data.setdefault(DOMAIN, {})
|
||||||
|
hass.data[DOMAIN][entry.entry_id] = coordinator
|
||||||
|
|
||||||
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
|
@ -157,7 +155,7 @@ class NotionEntity(CoordinatorEntity):
|
||||||
via_device=(DOMAIN, bridge.get("hardware_id")),
|
via_device=(DOMAIN, bridge.get("hardware_id")),
|
||||||
)
|
)
|
||||||
|
|
||||||
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
self._attr_extra_state_attributes = {}
|
||||||
self._attr_name = f'{sensor["name"]}: {description.name}'
|
self._attr_name = f'{sensor["name"]}: {description.name}'
|
||||||
self._attr_unique_id = (
|
self._attr_unique_id = (
|
||||||
f'{sensor_id}_{coordinator.data["tasks"][task_id]["task_type"]}'
|
f'{sensor_id}_{coordinator.data["tasks"][task_id]["task_type"]}'
|
||||||
|
|
|
@ -22,7 +22,6 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import NotionEntity
|
from . import NotionEntity
|
||||||
from .const import (
|
from .const import (
|
||||||
DATA_COORDINATOR,
|
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
LOGGER,
|
LOGGER,
|
||||||
SENSOR_BATTERY,
|
SENSOR_BATTERY,
|
||||||
|
@ -122,7 +121,7 @@ async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Notion sensors based on a config entry."""
|
"""Set up Notion sensors based on a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
|
|
|
@ -44,23 +44,22 @@ class NotionFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
assert self._username
|
assert self._username
|
||||||
assert self._password
|
assert self._password
|
||||||
|
|
||||||
|
errors = {}
|
||||||
session = aiohttp_client.async_get_clientsession(self.hass)
|
session = aiohttp_client.async_get_clientsession(self.hass)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await async_get_client(self._username, self._password, session=session)
|
await async_get_client(self._username, self._password, session=session)
|
||||||
except InvalidCredentialsError:
|
except InvalidCredentialsError:
|
||||||
return self.async_show_form(
|
errors["base"] = "invalid_auth"
|
||||||
step_id=step_id,
|
|
||||||
data_schema=schema,
|
|
||||||
errors={"base": "invalid_auth"},
|
|
||||||
description_placeholders={CONF_USERNAME: self._username},
|
|
||||||
)
|
|
||||||
except NotionError as err:
|
except NotionError as err:
|
||||||
LOGGER.error("Unknown Notion error: %s", err)
|
LOGGER.error("Unknown Notion error: %s", err)
|
||||||
|
errors["base"] = "unknown"
|
||||||
|
|
||||||
|
if errors:
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id=step_id,
|
step_id=step_id,
|
||||||
data_schema=schema,
|
data_schema=schema,
|
||||||
errors={"base": "unknown"},
|
errors=errors,
|
||||||
description_placeholders={CONF_USERNAME: self._username},
|
description_placeholders={CONF_USERNAME: self._username},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,6 @@ import logging
|
||||||
DOMAIN = "notion"
|
DOMAIN = "notion"
|
||||||
LOGGER = logging.getLogger(__package__)
|
LOGGER = logging.getLogger(__package__)
|
||||||
|
|
||||||
DATA_COORDINATOR = "coordinator"
|
|
||||||
|
|
||||||
SENSOR_BATTERY = "low_battery"
|
SENSOR_BATTERY = "low_battery"
|
||||||
SENSOR_DOOR = "door"
|
SENSOR_DOOR = "door"
|
||||||
SENSOR_GARAGE_DOOR = "garage_door"
|
SENSOR_GARAGE_DOOR = "garage_door"
|
||||||
|
|
|
@ -10,7 +10,7 @@ from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import NotionEntity
|
from . import NotionEntity
|
||||||
from .const import DATA_COORDINATOR, DOMAIN, LOGGER, SENSOR_TEMPERATURE
|
from .const import DOMAIN, LOGGER, SENSOR_TEMPERATURE
|
||||||
|
|
||||||
SENSOR_DESCRIPTIONS = (
|
SENSOR_DESCRIPTIONS = (
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
|
@ -27,7 +27,7 @@ async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Notion sensors based on a config entry."""
|
"""Set up Notion sensors based on a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
|
|
Loading…
Add table
Reference in a new issue