Skip to content

Commit 10eb9d9

Browse files
committed
fixed switch-states
1 parent 8cba21a commit 10eb9d9

7 files changed

Lines changed: 63 additions & 26 deletions

File tree

custom_components/liebherr/__init__.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,26 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
5252

5353
api.session = ClientSession(connector=api.connector)
5454

55-
async def async_update_method():
55+
async def async_update_method() -> None:
5656
"""Fetch both appliances and notifications."""
57+
58+
_LOGGER.debug("async_update_method called")
5759
try:
5860
# Geräte abrufen
5961
appliances = await api.get_appliances()
6062

6163
# Benachrichtigungen abrufen
62-
filtered_notifications = [] # await api.fetch_notifications(config_entry)
64+
# await api.fetch_notifications(config_entry)
65+
filtered_notifications = []
6366

6467
# Kombinierte Daten zurückgeben
6568
combined_data = {
6669
"appliances": appliances,
6770
"notifications": filtered_notifications,
6871
}
6972
except LiebherrUpdateException as e:
70-
raise LiebherrUpdateException(f"Error updating Liebherr data: {e}") from e
73+
raise LiebherrUpdateException(
74+
f"Error updating Liebherr data: {e}") from e
7175
else:
7276
return combined_data
7377

@@ -76,9 +80,11 @@ async def async_update_method():
7680
_LOGGER,
7781
name="Liebherr devices",
7882
update_method=async_update_method,
79-
update_interval=timedelta(
80-
seconds=config_entry.options.get("update_interval", 6)
81-
),
83+
update_interval=timedelta(seconds=10),
84+
)
85+
_LOGGER.debug(
86+
"[LIEBHERR] Effective update interval: %s seconds",
87+
coordinator.update_interval.total_seconds(),
8288
)
8389

8490
hass.data[DOMAIN][config_entry.entry_id] = {
@@ -149,11 +155,11 @@ async def get_appliances(self):
149155
headers = {
150156
"api-key": self._key,
151157
}
152-
_LOGGER.debug(headers)
153-
_LOGGER.debug(BASE_API_URL)
158+
154159
async with self.session.get(BASE_API_URL, headers=headers) as response:
155160
if response.status != 200:
156-
_LOGGER.error("Failed to fetch appliances: %s", response.status)
161+
_LOGGER.error("Failed to fetch appliances: %s",
162+
response.status)
157163
return []
158164

159165
data = await response.json()
@@ -193,7 +199,8 @@ async def get_controls(self, device_id):
193199
_LOGGER.error("API-KEY provided is not valid")
194200
return []
195201
data = await response.json()
196-
_LOGGER.debug("Fetched controls for device %s: %s", device_id, data)
202+
_LOGGER.debug("Fetched controls for device %s: %s",
203+
device_id, data)
197204
return data
198205

199206
async def set_value(self, deviceId, control, value):
@@ -244,7 +251,8 @@ async def fetch_notifications(self, config_entry):
244251
notifications = await self.get_notifications()
245252

246253
# Get selected devices from options
247-
selected_devices = config_entry.options.get("devices_to_notify", [])
254+
selected_devices = config_entry.options.get(
255+
"devices_to_notify", [])
248256

249257
# Filter notifications for selected devices
250258
filtered_notifications = [
@@ -350,7 +358,8 @@ async def dismiss_handler(event):
350358
"persistent_notification.dismiss", dismiss_handler
351359
).remove()
352360

353-
self._hass.bus.async_listen("persistent_notification.dismiss", dismiss_handler)
361+
self._hass.bus.async_listen(
362+
"persistent_notification.dismiss", dismiss_handler)
354363

355364
async def _acknowledge_notification(self, notification):
356365
"""Send acknowledgment to the API."""

custom_components/liebherr/climate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
2727
for appliance in appliances:
2828
controls = await api.get_controls(appliance["deviceId"])
2929
if not controls:
30-
_LOGGER.warning("No controls found for appliance %s", appliance["deviceId"])
30+
_LOGGER.warning("No controls found for appliance %s",
31+
appliance["deviceId"])
3132
continue
3233

3334
if appliance["applianceType"] in [

custom_components/liebherr/cover.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ async def async_setup_entry(
2525
for appliance in appliances:
2626
controls = await api.get_controls(appliance["deviceId"])
2727
if not controls:
28-
_LOGGER.warning("No controls found for appliance %s", appliance["deviceId"])
28+
_LOGGER.warning("No controls found for appliance %s",
29+
appliance["deviceId"])
2930
continue
3031

3132
for control in controls:

custom_components/liebherr/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"domain": "liebherr",
33
"name": "Liebherr",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"dependencies": [],
66
"config_flow": true,
77
"documentation": "https://github.com/bhuebschen/liebherr",

custom_components/liebherr/select.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ async def async_setup_entry(
2626
for appliance in appliances:
2727
controls = await api.get_controls(appliance["deviceId"])
2828
if not controls:
29-
_LOGGER.warning("No controls found for appliance %s", appliance["deviceId"])
29+
_LOGGER.warning("No controls found for appliance %s",
30+
appliance["deviceId"])
3031
continue
3132

3233
for control in controls:

custom_components/liebherr/sensor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ async def async_setup_entry(
2424
for appliance in appliances:
2525
controls = await api.get_controls(appliance["deviceId"])
2626
if not controls:
27-
_LOGGER.warning("No controls found for appliance %s", appliance["deviceId"])
27+
_LOGGER.warning("No controls found for appliance %s",
28+
appliance["deviceId"])
2829
continue
2930

3031
for control in controls:

custom_components/liebherr/switch.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ async def async_setup_entry(
2525
for appliance in appliances:
2626
controls = await api.get_controls(appliance["deviceId"])
2727
if not controls:
28-
_LOGGER.warning("No controls found for appliance %s", appliance["deviceId"])
28+
_LOGGER.warning("No controls found for appliance %s",
29+
appliance["deviceId"])
2930
continue
3031

3132
for control in controls:
@@ -35,7 +36,8 @@ async def async_setup_entry(
3536
entities.extend(
3637
[
3738
LiebherrSwitch(
38-
api, coordinator, appliance, control, control.get("zoneId")
39+
api, coordinator, appliance, control, control.get(
40+
"zoneId")
3941
),
4042
]
4143
)
@@ -57,7 +59,8 @@ def __init__(self, api, coordinator, appliance, control, zoneId) -> None:
5759
self._control = control
5860
self._zoneId = control.get("zoneId", zoneId)
5961
self._identifier = (
60-
appliance.get("nickname") + "_" + control.get("name", control.get("type"))
62+
appliance.get("nickname") + "_" +
63+
control.get("name", control.get("type"))
6164
)
6265
if "zonePosition" in control:
6366
self._identifier += f"_{control['zonePosition']}"
@@ -116,9 +119,32 @@ def is_on(self):
116119
for control in controls:
117120
if self._control_name == control.get("name"):
118121
if self._zoneId == control.get("zoneId"):
119-
return control.get("active", False)
122+
_LOGGER.debug(control)
123+
return control.get("value", False)
120124
return False
121125

126+
def setControlValue(self, value):
127+
"""Change controls value."""
128+
appliances = self._coordinator.data.get("appliances", [])
129+
device = next(
130+
(d for d in appliances if d.get("deviceId")
131+
== self._appliance["deviceId"]),
132+
None,
133+
)
134+
135+
if device:
136+
control = next(
137+
(
138+
c
139+
for c in device.get("controls", [])
140+
if self._control_name == c.get("name")
141+
and self._zoneId == c.get("zoneId")
142+
),
143+
None,
144+
)
145+
if control:
146+
control["value"] = value
147+
122148
@property
123149
def available(self):
124150
"""Return True if the switch is available."""
@@ -152,8 +178,7 @@ async def async_turn_on(self, **kwargs):
152178
await self._api.set_value(
153179
self._appliance["deviceId"], self._control["name"], data
154180
)
155-
await asyncio.sleep(5)
156-
await self._coordinator.async_request_refresh()
181+
self.setControlValue(True)
157182

158183
async def async_turn_off(self, **kwargs):
159184
"""Turn the switch off."""
@@ -169,7 +194,7 @@ async def async_turn_off(self, **kwargs):
169194
)
170195
if self._control["type"] == "ToggleControl":
171196
if self._control.get("zoneId", None) is None:
172-
data = BaseToggleControlRequest(value=True)
197+
data = BaseToggleControlRequest(value=False)
173198
else:
174199
data = ZoneToggleControlRequest(
175200
zoneId=self._control.get("zoneId"), value=False
@@ -178,5 +203,4 @@ async def async_turn_off(self, **kwargs):
178203
await self._api.set_value(
179204
self._appliance["deviceId"], self._control["name"], data
180205
)
181-
await asyncio.sleep(5)
182-
await self._coordinator.async_request_refresh()
206+
self.setControlValue(False)

0 commit comments

Comments
 (0)