This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathclimate.py
57 lines (46 loc) · 2.23 KB
/
climate.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
import logging
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers import aiohttp_client, storage
from .api import HeatzyAPI
from .authenticator import HeatzyAuthenticator
from .const import STORAGE_KEY, STORAGE_VERSION
from .pilote_v1 import HeatzyPiloteV1Thermostat
from .pilote_v2 import HeatzyPiloteV2Thermostat
PRODUCT_KEY_TO_DEVICE_IMPLEMENTATION = {
# Heatzy Pilote v1
'9420ae048da545c88fc6274d204dd25f': HeatzyPiloteV1Thermostat,
# Heatzy Pilote v2
'51d16c22a5f74280bc3cfe9ebcdc6402': HeatzyPiloteV2Thermostat,
'b9a67b6ce24b437d9794103fd317e627': HeatzyPiloteV2Thermostat,
'4fc968a21e7243b390e9ede6f1c6465d': HeatzyPiloteV2Thermostat
}
_LOGGER = logging.getLogger(__name__)
async def async_setup_platform(hass, config, add_devices, discovery_info=None):
"""Configure Heatzy API using Home Assistant configuration and fetch all Heatzy devices."""
# retrieve platform config
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
session = aiohttp_client.async_get_clientsession(hass)
store = storage.Store(hass, STORAGE_VERSION, STORAGE_KEY)
authenticator = HeatzyAuthenticator(session, store, username, password)
api = HeatzyAPI(session, authenticator)
# fetch configured Heatzy devices
devices = await api.async_get_devices()
# add all Heatzy devices with HA implementation to home assistant
add_devices(filter(None.__ne__, map(setup_heatzy_device(api), devices)))
return True
def setup_heatzy_device(api):
def find_heatzy_device_implementation(device):
"""Find Home Assistant implementation for the Heatzy device.
Implementation search is based on device 'product_key'.
If the implementation is not found, returns None.
"""
DeviceImplementation = PRODUCT_KEY_TO_DEVICE_IMPLEMENTATION.get(
device.get('product_key'))
if DeviceImplementation is None:
_LOGGER.warn('Device %s with product key %s is not supported',
device.get('did'), device.get('product_key'))
return None
return DeviceImplementation(api, device)
return find_heatzy_device_implementation