Clean up zwave_js logging and hass.data (#73856)

pull/73867/head
Martin Hjelmare 2022-06-22 21:35:26 +02:00 committed by GitHub
parent 1ead6d6762
commit 0e674fc597
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 10 additions and 42 deletions

View File

@ -104,8 +104,6 @@ from .services import ZWaveServices
CONNECT_TIMEOUT = 10 CONNECT_TIMEOUT = 10
DATA_CLIENT_LISTEN_TASK = "client_listen_task" DATA_CLIENT_LISTEN_TASK = "client_listen_task"
DATA_START_PLATFORM_TASK = "start_platform_task" DATA_START_PLATFORM_TASK = "start_platform_task"
DATA_CONNECT_FAILED_LOGGED = "connect_failed_logged"
DATA_INVALID_SERVER_VERSION_LOGGED = "invalid_server_version_logged"
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
@ -170,28 +168,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await async_ensure_addon_running(hass, entry) await async_ensure_addon_running(hass, entry)
client = ZwaveClient(entry.data[CONF_URL], async_get_clientsession(hass)) client = ZwaveClient(entry.data[CONF_URL], async_get_clientsession(hass))
entry_hass_data: dict = hass.data[DOMAIN].setdefault(entry.entry_id, {})
# connect and throw error if connection failed # connect and throw error if connection failed
try: try:
async with timeout(CONNECT_TIMEOUT): async with timeout(CONNECT_TIMEOUT):
await client.connect() await client.connect()
except InvalidServerVersion as err: except InvalidServerVersion as err:
if not entry_hass_data.get(DATA_INVALID_SERVER_VERSION_LOGGED):
LOGGER.error("Invalid server version: %s", err)
entry_hass_data[DATA_INVALID_SERVER_VERSION_LOGGED] = True
if use_addon: if use_addon:
async_ensure_addon_updated(hass) async_ensure_addon_updated(hass)
raise ConfigEntryNotReady from err raise ConfigEntryNotReady(f"Invalid server version: {err}") from err
except (asyncio.TimeoutError, BaseZwaveJSServerError) as err: except (asyncio.TimeoutError, BaseZwaveJSServerError) as err:
if not entry_hass_data.get(DATA_CONNECT_FAILED_LOGGED): raise ConfigEntryNotReady(f"Failed to connect: {err}") from err
LOGGER.error("Failed to connect: %s", err)
entry_hass_data[DATA_CONNECT_FAILED_LOGGED] = True
raise ConfigEntryNotReady from err
else: else:
LOGGER.info("Connected to Zwave JS Server") LOGGER.info("Connected to Zwave JS Server")
entry_hass_data[DATA_CONNECT_FAILED_LOGGED] = False
entry_hass_data[DATA_INVALID_SERVER_VERSION_LOGGED] = False
dev_reg = device_registry.async_get(hass) dev_reg = device_registry.async_get(hass)
ent_reg = entity_registry.async_get(hass) ent_reg = entity_registry.async_get(hass)
@ -202,7 +191,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async_register_api(hass) async_register_api(hass)
platform_task = hass.async_create_task(start_platforms(hass, entry, client)) platform_task = hass.async_create_task(start_platforms(hass, entry, client))
entry_hass_data[DATA_START_PLATFORM_TASK] = platform_task hass.data[DOMAIN].setdefault(entry.entry_id, {})[
DATA_START_PLATFORM_TASK
] = platform_task
return True return True
@ -635,9 +626,7 @@ async def disconnect_client(hass: HomeAssistant, entry: ConfigEntry) -> None:
platform_task: asyncio.Task = data[DATA_START_PLATFORM_TASK] platform_task: asyncio.Task = data[DATA_START_PLATFORM_TASK]
listen_task.cancel() listen_task.cancel()
platform_task.cancel() platform_task.cancel()
platform_setup_tasks = ( platform_setup_tasks = data.get(DATA_PLATFORM_SETUP, {}).values()
hass.data[DOMAIN].get(entry.entry_id, {}).get(DATA_PLATFORM_SETUP, {}).values()
)
for task in platform_setup_tasks: for task in platform_setup_tasks:
task.cancel() task.cancel()
@ -711,8 +700,7 @@ async def async_ensure_addon_running(hass: HomeAssistant, entry: ConfigEntry) ->
try: try:
addon_info = await addon_manager.async_get_addon_info() addon_info = await addon_manager.async_get_addon_info()
except AddonError as err: except AddonError as err:
LOGGER.error(err) raise ConfigEntryNotReady(err) from err
raise ConfigEntryNotReady from err
usb_path: str = entry.data[CONF_USB_PATH] usb_path: str = entry.data[CONF_USB_PATH]
# s0_legacy_key was saved as network_key before s2 was added. # s0_legacy_key was saved as network_key before s2 was added.

View File

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
import logging
from zwave_js_server.client import Client as ZwaveClient from zwave_js_server.client import Client as ZwaveClient
from zwave_js_server.const import CommandClass from zwave_js_server.const import CommandClass
@ -30,8 +29,6 @@ from .entity import ZWaveBaseEntity
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
LOGGER = logging.getLogger(__name__)
NOTIFICATION_SMOKE_ALARM = "1" NOTIFICATION_SMOKE_ALARM = "1"
NOTIFICATION_CARBON_MONOOXIDE = "2" NOTIFICATION_CARBON_MONOOXIDE = "2"

View File

@ -1,7 +1,6 @@
"""Support for Z-Wave cover devices.""" """Support for Z-Wave cover devices."""
from __future__ import annotations from __future__ import annotations
import logging
from typing import Any, cast from typing import Any, cast
from zwave_js_server.client import Client as ZwaveClient from zwave_js_server.client import Client as ZwaveClient
@ -39,8 +38,6 @@ from .entity import ZWaveBaseEntity
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
LOGGER = logging.getLogger(__name__)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,

View File

@ -1,8 +1,6 @@
"""Generic Z-Wave Entity Class.""" """Generic Z-Wave Entity Class."""
from __future__ import annotations from __future__ import annotations
import logging
from zwave_js_server.const import NodeStatus from zwave_js_server.const import NodeStatus
from zwave_js_server.model.driver import Driver from zwave_js_server.model.driver import Driver
from zwave_js_server.model.value import Value as ZwaveValue, get_value_id from zwave_js_server.model.value import Value as ZwaveValue, get_value_id
@ -12,12 +10,10 @@ from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import DeviceInfo, Entity from homeassistant.helpers.entity import DeviceInfo, Entity
from .const import DOMAIN from .const import DOMAIN, LOGGER
from .discovery import ZwaveDiscoveryInfo from .discovery import ZwaveDiscoveryInfo
from .helpers import get_device_id, get_unique_id from .helpers import get_device_id, get_unique_id
LOGGER = logging.getLogger(__name__)
EVENT_VALUE_UPDATED = "value updated" EVENT_VALUE_UPDATED = "value updated"
EVENT_VALUE_REMOVED = "value removed" EVENT_VALUE_REMOVED = "value removed"
EVENT_DEAD = "dead" EVENT_DEAD = "dead"

View File

@ -1,7 +1,6 @@
"""Support for Z-Wave lights.""" """Support for Z-Wave lights."""
from __future__ import annotations from __future__ import annotations
import logging
from typing import Any, cast from typing import Any, cast
from zwave_js_server.client import Client as ZwaveClient from zwave_js_server.client import Client as ZwaveClient
@ -49,8 +48,6 @@ from .entity import ZWaveBaseEntity
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
LOGGER = logging.getLogger(__name__)
MULTI_COLOR_MAP = { MULTI_COLOR_MAP = {
ColorComponent.WARM_WHITE: COLOR_SWITCH_COMBINED_WARM_WHITE, ColorComponent.WARM_WHITE: COLOR_SWITCH_COMBINED_WARM_WHITE,
ColorComponent.COLD_WHITE: COLOR_SWITCH_COMBINED_COLD_WHITE, ColorComponent.COLD_WHITE: COLOR_SWITCH_COMBINED_COLD_WHITE,

View File

@ -1,7 +1,6 @@
"""Representation of Z-Wave locks.""" """Representation of Z-Wave locks."""
from __future__ import annotations from __future__ import annotations
import logging
from typing import Any from typing import Any
import voluptuous as vol import voluptuous as vol
@ -27,6 +26,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import ( from .const import (
DATA_CLIENT, DATA_CLIENT,
DOMAIN, DOMAIN,
LOGGER,
SERVICE_CLEAR_LOCK_USERCODE, SERVICE_CLEAR_LOCK_USERCODE,
SERVICE_SET_LOCK_USERCODE, SERVICE_SET_LOCK_USERCODE,
) )
@ -35,8 +35,6 @@ from .entity import ZWaveBaseEntity
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
LOGGER = logging.getLogger(__name__)
STATE_TO_ZWAVE_MAP: dict[int, dict[str, int | bool]] = { STATE_TO_ZWAVE_MAP: dict[int, dict[str, int | bool]] = {
CommandClass.DOOR_LOCK: { CommandClass.DOOR_LOCK: {
STATE_UNLOCKED: DoorLockMode.UNSECURED, STATE_UNLOCKED: DoorLockMode.UNSECURED,

View File

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Mapping from collections.abc import Mapping
import logging
from typing import cast from typing import cast
import voluptuous as vol import voluptuous as vol
@ -55,6 +54,7 @@ from .const import (
ENTITY_DESC_KEY_TEMPERATURE, ENTITY_DESC_KEY_TEMPERATURE,
ENTITY_DESC_KEY_TOTAL_INCREASING, ENTITY_DESC_KEY_TOTAL_INCREASING,
ENTITY_DESC_KEY_VOLTAGE, ENTITY_DESC_KEY_VOLTAGE,
LOGGER,
SERVICE_RESET_METER, SERVICE_RESET_METER,
) )
from .discovery import ZwaveDiscoveryInfo from .discovery import ZwaveDiscoveryInfo
@ -67,8 +67,6 @@ from .helpers import get_device_id, get_valueless_base_unique_id
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
LOGGER = logging.getLogger(__name__)
STATUS_ICON: dict[NodeStatus, str] = { STATUS_ICON: dict[NodeStatus, str] = {
NodeStatus.ALIVE: "mdi:heart-pulse", NodeStatus.ALIVE: "mdi:heart-pulse",
NodeStatus.ASLEEP: "mdi:sleep", NodeStatus.ASLEEP: "mdi:sleep",

View File

@ -1,7 +1,6 @@
"""Representation of Z-Wave switches.""" """Representation of Z-Wave switches."""
from __future__ import annotations from __future__ import annotations
import logging
from typing import Any from typing import Any
from zwave_js_server.client import Client as ZwaveClient from zwave_js_server.client import Client as ZwaveClient
@ -23,8 +22,6 @@ from .entity import ZWaveBaseEntity
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
LOGGER = logging.getLogger(__name__)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,