2
2
from homeassistant .helpers .device_registry import DeviceInfo
3
3
4
4
import asyncio
5
- from . client import comwatt_client
5
+ from comwatt_client import ComwattClient
6
6
from datetime import timedelta
7
7
8
8
SCAN_INTERVAL = timedelta (minutes = 2 )
9
9
SWITCH_NATURE = ['POWER_SWITCH' , 'RELAY' ]
10
10
11
11
async def async_setup_entry (hass , entry , async_add_entities ):
12
+ comwatt_client = ComwattClient ()
13
+ comwatt_client .session .cookies .update (hass .data [DOMAIN ]["cookies" ])
14
+
12
15
new_devices = []
13
16
sites = await asyncio .to_thread (lambda : comwatt_client .get_sites ())
14
17
for site in sites :
@@ -70,6 +73,9 @@ def is_on(self):
70
73
71
74
def turn_on (self , ** kwargs ) -> None :
72
75
"""Turn the entity on."""
76
+ comwatt_client = ComwattClient ()
77
+ comwatt_client .session .cookies .update (self .hass .data [DOMAIN ]["cookies" ])
78
+
73
79
try :
74
80
device = comwatt_client .get_device (self ._ref )
75
81
for feature in device ['features' ]:
@@ -80,6 +86,7 @@ def turn_on(self, **kwargs) -> None:
80
86
81
87
except Exception :
82
88
comwatt_client .authenticate (self ._username , self ._password )
89
+ self .hass .data [DOMAIN ]["cookies" ] = comwatt_client .session .cookies .get_dict ()
83
90
device = comwatt_client .get_device (self ._ref )
84
91
for feature in device ['features' ]:
85
92
for capacity in feature ['capacities' ]:
@@ -91,6 +98,9 @@ def turn_on(self, **kwargs) -> None:
91
98
92
99
def turn_off (self , ** kwargs ) -> None :
93
100
"""Turn the entity off."""
101
+ comwatt_client = ComwattClient ()
102
+ comwatt_client .session .cookies .update (self .hass .data [DOMAIN ]["cookies" ])
103
+
94
104
try :
95
105
device = comwatt_client .get_device (self ._ref )
96
106
for feature in device ['features' ]:
@@ -101,6 +111,7 @@ def turn_off(self, **kwargs) -> None:
101
111
102
112
except Exception :
103
113
comwatt_client .authenticate (self ._username , self ._password )
114
+ self .hass .data [DOMAIN ]["cookies" ] = comwatt_client .session .cookies .get_dict ()
104
115
device = comwatt_client .get_device (self ._ref )
105
116
for feature in device ['features' ]:
106
117
for capacity in feature ['capacities' ]:
@@ -112,6 +123,9 @@ def turn_off(self, **kwargs) -> None:
112
123
113
124
def update (self ) -> None :
114
125
"""Fetch new state data for the sensor."""
126
+ comwatt_client = ComwattClient ()
127
+ comwatt_client .session .cookies .update (self .hass .data [DOMAIN ]["cookies" ])
128
+
115
129
try :
116
130
device = comwatt_client .get_device (self ._ref )
117
131
for feature in device ['features' ]:
@@ -121,6 +135,7 @@ def update(self) -> None:
121
135
122
136
except Exception :
123
137
comwatt_client .authenticate (self ._username , self ._password )
138
+ self .hass .data [DOMAIN ]["cookies" ] = comwatt_client .session .cookies .get_dict ()
124
139
device = comwatt_client .get_device (self ._ref )
125
140
for feature in device ['features' ]:
126
141
for capacity in feature ['capacities' ]:
0 commit comments