Skip to content

Commit ee9c552

Browse files
committed
.
1 parent d251c66 commit ee9c552

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

custom_components/comwatt/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from homeassistant.core import HomeAssistant
77

88
from .const import DOMAIN
9-
from .client import comwatt_client
9+
from comwatt_client import ComwattClient
1010

1111
import asyncio
1212

@@ -18,7 +18,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
1818
# Set default DOMAIN
1919
hass.data.setdefault(DOMAIN, {})
2020

21-
await asyncio.to_thread(lambda: comwatt_client.authenticate(entry.data["username"], entry.data["password"]))
21+
client = ComwattClient()
22+
await asyncio.to_thread(lambda: client.authenticate(entry.data["username"], entry.data["password"]))
23+
24+
hass.data[DOMAIN]["cookies"] = client.session.cookies.get_dict()
2225

2326
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
2427

custom_components/comwatt/client.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

custom_components/comwatt/sensor.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717

1818
import asyncio
1919
from .const import DOMAIN
20-
from .client import comwatt_client
20+
from comwatt_client import ComwattClient
2121
from datetime import timedelta
2222

2323
SCAN_INTERVAL = timedelta(minutes=2)
2424

2525
async def async_setup_entry(hass, entry, async_add_entities):
26+
comwatt_client = ComwattClient()
27+
comwatt_client.session.cookies.update(hass.data[DOMAIN]["cookies"])
28+
2629
new_devices = []
2730
sites = await asyncio.to_thread(lambda: comwatt_client.get_sites())
2831
for site in sites:
@@ -79,11 +82,14 @@ def __init__(self, entry, device):
7982
# TODO: Update it ~ only 1 per hour
8083
def update(self) -> None:
8184
"""Fetch new state data for the sensor."""
85+
comwatt_client = ComwattClient()
86+
comwatt_client.session.cookies.update(self.hass.data[DOMAIN]["cookies"])
8287

8388
try:
8489
time_series_data = comwatt_client.get_device_ts_time_ago(self._device["id"], "VIRTUAL_QUANTITY", "HOUR", "NONE")
8590
except Exception:
8691
comwatt_client.authenticate(self._username, self._password)
92+
self.hass.data[DOMAIN]["cookies"] = comwatt_client.session.cookies.get_dict()
8793
time_series_data = comwatt_client.get_device_ts_time_ago(self._device["id"], "VIRTUAL_QUANTITY", "HOUR", "NONE")
8894

8995
if self._attr_native_value == None:
@@ -111,11 +117,14 @@ def __init__(self, entry, device):
111117

112118
def update(self) -> None:
113119
"""Fetch new state data for the sensor."""
120+
comwatt_client = ComwattClient()
121+
comwatt_client.session.cookies.update(self.hass.data[DOMAIN]["cookies"])
114122

115123
try:
116124
time_series_data = comwatt_client.get_device_ts_time_ago(self._device["id"], "FLOW", "NONE", "NONE", "HOUR", 1)
117125
except Exception:
118126
comwatt_client.authenticate(self._username, self._password)
127+
self.hass.data[DOMAIN]["cookies"] = comwatt_client.session.cookies.get_dict()
119128
time_series_data = comwatt_client.get_device_ts_time_ago(self._device["id"], "FLOW", "NONE", "NONE", "HOUR", 1)
120129

121130
# TODO: Update to the time of comwatt and not the current time

custom_components/comwatt/switch.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
from homeassistant.helpers.device_registry import DeviceInfo
33

44
import asyncio
5-
from .client import comwatt_client
5+
from comwatt_client import ComwattClient
66
from datetime import timedelta
77

88
SCAN_INTERVAL = timedelta(minutes=2)
99
SWITCH_NATURE = ['POWER_SWITCH', 'RELAY']
1010

1111
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+
1215
new_devices = []
1316
sites = await asyncio.to_thread(lambda: comwatt_client.get_sites())
1417
for site in sites:
@@ -70,6 +73,9 @@ def is_on(self):
7073

7174
def turn_on(self, **kwargs) -> None:
7275
"""Turn the entity on."""
76+
comwatt_client = ComwattClient()
77+
comwatt_client.session.cookies.update(self.hass.data[DOMAIN]["cookies"])
78+
7379
try:
7480
device = comwatt_client.get_device(self._ref)
7581
for feature in device['features']:
@@ -80,6 +86,7 @@ def turn_on(self, **kwargs) -> None:
8086

8187
except Exception:
8288
comwatt_client.authenticate(self._username, self._password)
89+
self.hass.data[DOMAIN]["cookies"] = comwatt_client.session.cookies.get_dict()
8390
device = comwatt_client.get_device(self._ref)
8491
for feature in device['features']:
8592
for capacity in feature['capacities']:
@@ -91,6 +98,9 @@ def turn_on(self, **kwargs) -> None:
9198

9299
def turn_off(self, **kwargs) -> None:
93100
"""Turn the entity off."""
101+
comwatt_client = ComwattClient()
102+
comwatt_client.session.cookies.update(self.hass.data[DOMAIN]["cookies"])
103+
94104
try:
95105
device = comwatt_client.get_device(self._ref)
96106
for feature in device['features']:
@@ -101,6 +111,7 @@ def turn_off(self, **kwargs) -> None:
101111

102112
except Exception:
103113
comwatt_client.authenticate(self._username, self._password)
114+
self.hass.data[DOMAIN]["cookies"] = comwatt_client.session.cookies.get_dict()
104115
device = comwatt_client.get_device(self._ref)
105116
for feature in device['features']:
106117
for capacity in feature['capacities']:
@@ -112,6 +123,9 @@ def turn_off(self, **kwargs) -> None:
112123

113124
def update(self) -> None:
114125
"""Fetch new state data for the sensor."""
126+
comwatt_client = ComwattClient()
127+
comwatt_client.session.cookies.update(self.hass.data[DOMAIN]["cookies"])
128+
115129
try:
116130
device = comwatt_client.get_device(self._ref)
117131
for feature in device['features']:
@@ -121,6 +135,7 @@ def update(self) -> None:
121135

122136
except Exception:
123137
comwatt_client.authenticate(self._username, self._password)
138+
self.hass.data[DOMAIN]["cookies"] = comwatt_client.session.cookies.get_dict()
124139
device = comwatt_client.get_device(self._ref)
125140
for feature in device['features']:
126141
for capacity in feature['capacities']:

0 commit comments

Comments
 (0)