|
2 | 2 | from homeassistant.helpers.device_registry import DeviceInfo
|
3 | 3 |
|
4 | 4 | import asyncio
|
5 |
| -from .client import comwatt_client |
| 5 | +from .const import DOMAIN |
| 6 | +from comwatt_client import ComwattClient |
6 | 7 | from datetime import timedelta
|
7 | 8 |
|
| 9 | + |
8 | 10 | SCAN_INTERVAL = timedelta(minutes=2)
|
9 | 11 | SWITCH_NATURE = ['POWER_SWITCH', 'RELAY']
|
10 | 12 |
|
11 | 13 | async def async_setup_entry(hass, entry, async_add_entities):
|
| 14 | + client = ComwattClient() |
| 15 | + client.session.cookies.update(hass.data[DOMAIN]["cookies"]) |
| 16 | + |
12 | 17 | new_devices = []
|
13 |
| - sites = await asyncio.to_thread(lambda: comwatt_client.get_sites()) |
| 18 | + sites = await asyncio.to_thread(lambda: client.get_sites()) |
14 | 19 | for site in sites:
|
15 |
| - devices = await asyncio.to_thread(lambda: comwatt_client.get_devices(site['id'])) |
| 20 | + devices = await asyncio.to_thread(lambda: client.get_devices(site['id'])) |
16 | 21 | for device in devices:
|
17 | 22 | if 'id' in device:
|
18 | 23 | if 'partChilds' in device and len(device['partChilds']) > 0:
|
@@ -70,58 +75,70 @@ def is_on(self):
|
70 | 75 |
|
71 | 76 | def turn_on(self, **kwargs) -> None:
|
72 | 77 | """Turn the entity on."""
|
| 78 | + client = ComwattClient() |
| 79 | + client.session.cookies.update(self.hass.data[DOMAIN]["cookies"]) |
| 80 | + |
73 | 81 | try:
|
74 |
| - device = comwatt_client.get_device(self._ref) |
| 82 | + device = client.get_device(self._ref) |
75 | 83 | for feature in device['features']:
|
76 | 84 | for capacity in feature['capacities']:
|
77 | 85 | if capacity.get('capacity', {}).get('nature') in SWITCH_NATURE:
|
78 | 86 | capacity_id = capacity['capacity']['id']
|
79 |
| - comwatt_client.switch_capacity(capacity_id, True) |
| 87 | + client.switch_capacity(capacity_id, True) |
80 | 88 |
|
81 | 89 | except Exception:
|
82 |
| - comwatt_client.authenticate(self._username, self._password) |
83 |
| - device = comwatt_client.get_device(self._ref) |
| 90 | + client.authenticate(self._username, self._password) |
| 91 | + self.hass.data[DOMAIN]["cookies"] = client.session.cookies.get_dict() |
| 92 | + device = client.get_device(self._ref) |
84 | 93 | for feature in device['features']:
|
85 | 94 | for capacity in feature['capacities']:
|
86 | 95 | if capacity.get('capacity', {}).get('nature') in SWITCH_NATURE:
|
87 | 96 | capacity_id = capacity['capacity']['id']
|
88 |
| - comwatt_client.switch_capacity(capacity_id, True) |
| 97 | + client.switch_capacity(capacity_id, True) |
89 | 98 |
|
90 | 99 | self.schedule_update_ha_state()
|
91 | 100 |
|
92 | 101 | def turn_off(self, **kwargs) -> None:
|
93 | 102 | """Turn the entity off."""
|
| 103 | + client = ComwattClient() |
| 104 | + client.session.cookies.update(self.hass.data[DOMAIN]["cookies"]) |
| 105 | + |
94 | 106 | try:
|
95 |
| - device = comwatt_client.get_device(self._ref) |
| 107 | + device = client.get_device(self._ref) |
96 | 108 | for feature in device['features']:
|
97 | 109 | for capacity in feature['capacities']:
|
98 | 110 | if capacity.get('capacity', {}).get('nature') in SWITCH_NATURE:
|
99 | 111 | capacity_id = capacity['capacity']['id']
|
100 |
| - comwatt_client.switch_capacity(capacity_id, False) |
| 112 | + client.switch_capacity(capacity_id, False) |
101 | 113 |
|
102 | 114 | except Exception:
|
103 |
| - comwatt_client.authenticate(self._username, self._password) |
104 |
| - device = comwatt_client.get_device(self._ref) |
| 115 | + client.authenticate(self._username, self._password) |
| 116 | + self.hass.data[DOMAIN]["cookies"] = client.session.cookies.get_dict() |
| 117 | + device = client.get_device(self._ref) |
105 | 118 | for feature in device['features']:
|
106 | 119 | for capacity in feature['capacities']:
|
107 | 120 | if capacity.get('capacity', {}).get('nature') in SWITCH_NATURE:
|
108 | 121 | capacity_id = capacity['capacity']['id']
|
109 |
| - comwatt_client.switch_capacity(capacity_id, False) |
| 122 | + client.switch_capacity(capacity_id, False) |
110 | 123 |
|
111 | 124 | self.schedule_update_ha_state()
|
112 | 125 |
|
113 | 126 | def update(self) -> None:
|
114 | 127 | """Fetch new state data for the sensor."""
|
| 128 | + client = ComwattClient() |
| 129 | + client.session.cookies.update(self.hass.data[DOMAIN]["cookies"]) |
| 130 | + |
115 | 131 | try:
|
116 |
| - device = comwatt_client.get_device(self._ref) |
| 132 | + device = client.get_device(self._ref) |
117 | 133 | for feature in device['features']:
|
118 | 134 | for capacity in feature['capacities']:
|
119 | 135 | if capacity.get('capacity', {}).get('nature') in SWITCH_NATURE:
|
120 | 136 | self._is_on = capacity['capacity']['enable']
|
121 | 137 |
|
122 | 138 | except Exception:
|
123 |
| - comwatt_client.authenticate(self._username, self._password) |
124 |
| - device = comwatt_client.get_device(self._ref) |
| 139 | + client.authenticate(self._username, self._password) |
| 140 | + self.hass.data[DOMAIN]["cookies"] = client.session.cookies.get_dict() |
| 141 | + device = client.get_device(self._ref) |
125 | 142 | for feature in device['features']:
|
126 | 143 | for capacity in feature['capacities']:
|
127 | 144 | if capacity.get('capacity', {}).get('nature') in SWITCH_NATURE:
|
|
0 commit comments