forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
90 lines (66 loc) · 2.42 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""The Gryf Smart integration."""
from __future__ import annotations
import logging
from pygryfsmart.api import GryfApi
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.typing import ConfigType
from .const import CONF_API, CONF_COMMUNICATION, CONF_DEVICE_DATA, CONF_PORT, DOMAIN
from .schema import CONFIG_SCHEMA as SCHEMA
CONFIG_SCHEMA = SCHEMA
_PLATFORMS: list[Platform] = [
Platform.LIGHT,
# Platform.BINARY_SENSOR,
# Platform.SENSOR,
# Platform.CLIMATE,
# Platform.SWITCH,
]
_LOGGER = logging.getLogger(__name__)
async def async_setup(
hass: HomeAssistant,
config: ConfigType,
) -> bool:
"""Set up the Gryf Smart Integration."""
if config.get(DOMAIN) is None:
return True
try:
api = GryfApi(config[DOMAIN][CONF_PORT])
await api.start_connection()
api.start_update_interval(1)
except ConnectionError:
_LOGGER.error("Unable to connect: %s", ConnectionError)
return False
hass.data[DOMAIN] = config.get(DOMAIN)
hass.data[DOMAIN][CONF_API] = api
for PLATFORM in _PLATFORMS:
await async_load_platform(hass, PLATFORM, DOMAIN, None, config)
return True
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
) -> bool:
"""Config flow for Gryf Smart Integration."""
try:
api = GryfApi(entry.data[CONF_COMMUNICATION][CONF_PORT])
await api.start_connection()
api.start_update_interval(1)
except ConnectionError:
raise ConfigEntryNotReady("Unable to connect with device") from ConnectionError
entry.runtime_data = {}
entry.runtime_data[CONF_API] = api
entry.runtime_data[CONF_DEVICE_DATA] = {
"identifiers": {(DOMAIN, "Gryf Smart", entry.unique_id)},
"name": f"Gryf Smart {entry.unique_id}",
"manufacturer": "Gryf Smart",
"model": "serial",
"sw_version": "1.0.0",
"hw_version": "1.0.0",
}
await hass.config_entries.async_forward_entry_setups(entry, _PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, _PLATFORMS)