Skip to content

Commit fce1de8

Browse files
author
Gabriel Wieskamp
committed
Added control for lock
1 parent 5536453 commit fce1de8

5 files changed

Lines changed: 118 additions & 6 deletions

File tree

custom_components/mill/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Platform.SENSOR,
1919
Platform.BINARY_SENSOR,
2020
Platform.SWITCH,
21+
Platform.SELECT,
2122
]
2223

2324

custom_components/mill/api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ async def async_update_token(self):
122122
self._token = results.get('token')
123123

124124

125+
async def async_set_lock(self, device, setting: str):
126+
"""Set the lid lock setting.
127+
128+
Valid settings: 'AlwaysLocked', 'AlwaysUnlocked', 'LockedWhenHot'
129+
"""
130+
valid_settings = ['AlwaysLocked', 'AlwaysUnlocked', 'LockedWhenHot']
131+
if setting not in valid_settings:
132+
raise ValueError(f"Invalid lid lock setting: {setting}. Must be one of {valid_settings}")
133+
134+
await self.async_update_token()
135+
auth = {"Authorization": "Bearer " + self._token}
136+
results = await self._api_wrapper(
137+
method="post",
138+
url=f"{CLOUD_URL}/device_settings/{device}",
139+
data={"settings": {"lidLockSetting": setting}},
140+
headers=auth
141+
)
142+
LOGGER.debug(f"Lid lock setting set to {setting}: {results}")
143+
144+
125145
async def async_set_cycle(self, device, cycle_state):
126146
"""Set the cycle."""
127147
await self.async_update_token()

custom_components/mill/binary_sensor.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@
2828
name="Bucket Missing",
2929
device_class=BinarySensorDeviceClass.PROBLEM,
3030
),
31-
BinarySensorEntityDescription(
32-
key="childLockEnabled",
33-
name="Child Lock",
34-
device_class=BinarySensorDeviceClass.LOCK,
35-
),
3631
BinarySensorEntityDescription(
3732
key="online",
3833
name="Online",

custom_components/mill/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"issue_tracker": "https://github.com/theOrakle/mill/issues",
99
"loggers": ["custom_components.mill"],
1010
"requirements": [],
11-
"version": "1.0.24",
11+
"version": "1.1.1",
1212
"dependencies": [],
1313
"codeowners": ["@theOrakle"]
1414
}

custom_components/mill/select.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""Select platform for mill."""
2+
from __future__ import annotations
3+
4+
from homeassistant.components.select import SelectEntity, SelectEntityDescription
5+
6+
from .const import DOMAIN, LOGGER
7+
from .coordinator import MillDataUpdateCoordinator
8+
from .entity import MillEntity
9+
10+
ENTITY_DESCRIPTIONS = (
11+
SelectEntityDescription(
12+
key="lidLockSetting",
13+
name="Lid Lock Setting",
14+
icon="mdi:lock-outline",
15+
options=["AlwaysLocked", "AlwaysUnlocked", "LockedWhenHot"],
16+
),
17+
)
18+
19+
20+
async def async_setup_entry(hass, entry, async_add_devices):
21+
"""Set up the select platform."""
22+
coordinator = hass.data[DOMAIN][entry.entry_id]
23+
entities = []
24+
25+
for device in coordinator.data:
26+
for entity_description in ENTITY_DESCRIPTIONS:
27+
# Only add lid lock setting if the device supports it
28+
if coordinator.data[device].get(entity_description.key) is not None:
29+
entities.append(
30+
MillSelect(
31+
coordinator=coordinator,
32+
entity_description=entity_description,
33+
device=device
34+
)
35+
)
36+
37+
async_add_devices(entities)
38+
39+
40+
class MillSelect(MillEntity, SelectEntity):
41+
"""Mill Select class."""
42+
43+
def __init__(
44+
self,
45+
coordinator: MillDataUpdateCoordinator,
46+
entity_description: SelectEntityDescription,
47+
device,
48+
) -> None:
49+
"""Initialize the select class."""
50+
super().__init__(coordinator, entity_description, device)
51+
self.entity_description = entity_description
52+
self.device = device
53+
self._attr_options = entity_description.options
54+
55+
@property
56+
def current_option(self) -> str | None:
57+
"""Return the selected entity option."""
58+
desc = self.entity_description
59+
lid_lock_data = self.coordinator.data[self.device].get(desc.key)
60+
61+
if isinstance(lid_lock_data, dict):
62+
desired = lid_lock_data.get('desired')
63+
reported = lid_lock_data.get('reported')
64+
# Use desired state if it differs from reported and is not None
65+
if desired != reported and desired is not None:
66+
current_setting = desired
67+
else:
68+
current_setting = reported
69+
else:
70+
current_setting = lid_lock_data
71+
72+
# Make sure the current setting is in our options list
73+
if current_setting in self._attr_options:
74+
return current_setting
75+
else:
76+
LOGGER.warning(f"Unknown lid lock setting: {current_setting}")
77+
return None
78+
79+
async def async_select_option(self, option: str) -> None:
80+
"""Change the selected option."""
81+
if option not in self._attr_options:
82+
LOGGER.error(f"Invalid option: {option}. Valid options: {self._attr_options}")
83+
return
84+
85+
await self.coordinator.client.async_set_lock(self.device, option)
86+
await self.coordinator.async_request_refresh()
87+
88+
async def delayed_refresh():
89+
import asyncio
90+
await asyncio.sleep(5) # Wait 5 seconds
91+
await self.coordinator.async_request_refresh()
92+
await asyncio.sleep(10) # Wait another 10 seconds
93+
await self.coordinator.async_request_refresh()
94+
95+
# Run delayed refreshes in background
96+
self.coordinator.hass.async_create_task(delayed_refresh())

0 commit comments

Comments
 (0)