-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
53 lines (40 loc) · 1.63 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
"""
Add suport for ITEAD devices like SONOFF without firmware flashing
"""
import asyncio
import logging
from collections import OrderedDict
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.helpers import discovery, config_validation
import voluptuous as vol
_LOGGER = logging.getLogger(__name__)
#'coolkit-client-phoenix-ng>=1.0.0'
REQUIREMENTS = ['websockets', 'pycryptodome', 'zeroconf']
DOMAIN = 'sonoff'
CONF_REGION = 'region'
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_USERNAME): config_validation.string,
vol.Required(CONF_PASSWORD): config_validation.string,
vol.Optional(CONF_REGION, default='eu'): config_validation.string,
}, extra=vol.ALLOW_EXTRA),
}, extra=vol.ALLOW_EXTRA)
async def async_setup(hass: HomeAssistant, config: OrderedDict):
from .coolkit_client import CoolkitSession
from .coolkit_client.discover import CoolkitDevicesDiscovery
known_devices = config.get('sonoff', {}).get('known_devices')
if known_devices is None:
known_devices = {}
res = await CoolkitSession.login(
config.get(DOMAIN, {}).get(CONF_USERNAME, ''),
config.get(DOMAIN, {}).get(CONF_PASSWORD, ''),
config.get(DOMAIN, {}).get(CONF_REGION, '')
)
if not res:
_LOGGER.error("Unable to login to coolikt server, please check your credentials.")
await CoolkitDevicesDiscovery.discover(known_devices)
await asyncio.sleep(2)
for component in ['switch', 'sensor']:
await discovery.async_load_platform(hass, component, DOMAIN, {}, config)
return True