Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
caibinqing committed Aug 18, 2024
1 parent e6ef8af commit 3e48a65
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 208 deletions.
30 changes: 8 additions & 22 deletions custom_components/ds_air/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,19 @@
import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SCAN_INTERVAL
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SCAN_INTERVAL, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntry

from .const import CONF_GW, DEFAULT_GW, DEFAULT_HOST, DEFAULT_PORT, DOMAIN
from .ds_air_service import Config
from .hass_inst import GetHass
from .ds_air_service import Config, Service

_LOGGER = logging.getLogger(__name__)
PLATFORMS = ["climate", "sensor"]


def _log(s: str):
s = str(s)
for i in s.split("\n"):
_LOGGER.debug(i)


def setup(hass, config):
hass.data[DOMAIN] = {}
GetHass.set_hass(hass)
return True
PLATFORMS = [
Platform.CLIMATE,
Platform.SENSOR,
]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
Expand All @@ -37,7 +28,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
gw = entry.data[CONF_GW]
scan_interval = entry.data[CONF_SCAN_INTERVAL]

_log(f"{host}:{port} {gw} {scan_interval}")
_LOGGER.debug(f"{host}:{port} {gw} {scan_interval}")

hass.data[DOMAIN][CONF_HOST] = host
hass.data[DOMAIN][CONF_PORT] = port
Expand All @@ -46,8 +37,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):

Config.is_c611 = gw == DEFAULT_GW

from .ds_air_service import Service

await hass.async_add_executor_job(Service.init, host, port, scan_interval)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))
Expand All @@ -60,16 +49,13 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if hass.data[DOMAIN].get("listener") is not None:
hass.data[DOMAIN].get("listener")()
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
from .ds_air_service import Service

Service.destroy()

return unload_ok


async def update_listener(hass: HomeAssistant, entry: ConfigEntry):
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
await hass.config_entries.async_reload(entry.entry_id)
return True


async def async_remove_config_entry_device(
Expand Down
Loading

0 comments on commit 3e48a65

Please sign in to comment.