forked from Arbuzov/home_assistant_delonghi_primadonna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.py
386 lines (329 loc) · 13 KB
/
device.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""Delongi primadonna device description"""
import asyncio
import logging
import uuid
from binascii import hexlify
from bleak import BleakClient
from bleak.exc import BleakDBusError, BleakError
from homeassistant.backports.enum import StrEnum
from homeassistant.components import bluetooth
from homeassistant.const import CONF_MAC, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from .const import (AMERICANO_OFF, AMERICANO_ON, BASE_COMMAND,
BYTES_GENERAL_COMMAND, BYTES_POWER, COFFE_OFF, COFFE_ON,
COFFEE_GROUNDS_CONTAINER_DETACHED,
COFFEE_GROUNDS_CONTAINER_FULL, CONTROLL_CHARACTERISTIC,
DEBUG, DEVICE_READY, DEVICE_TURNOFF, DOMAIN, DOPPIO_OFF,
DOPPIO_ON, ESPRESSO2_OFF, ESPRESSO2_ON, ESPRESSO_OFF,
ESPRESSO_ON, HOTWATER_OFF, HOTWATER_ON, LONG_OFF, LONG_ON,
NAME_CHARACTERISTIC, START_COFFEE, STEAM_OFF, STEAM_ON,
WATER_SHORTAGE, WATER_TANK_DETACHED)
_LOGGER = logging.getLogger(__name__)
class AvailableBeverage(StrEnum):
"""Coffee machine available beverages"""
NONE = 'none'
STEAM = 'steam'
LONG = 'long'
COFFEE = 'coffee'
DOPIO = 'dopio'
HOTWATER = 'hot_water'
ESPRESSO = 'espresso'
AMERICANO = 'americano'
ESPRESSO2 = 'espresso2'
NOZZLE_STATE = {
-1: 'UNKNOWN',
0: 'DETACHED',
1: 'STEAM',
2: 'UNKNOWN_2', # May also be Detached
4: 'MILK'
}
# Skipable maintanence states
SERVICE_STATE = {0: 'OK', 4: 'DESCALING'}
DEVICE_STATUS = {
3: 'COOKING',
4: 'NOZZLE_DETACHED',
5: 'OK',
13: 'COFFEE_GROUNDS_CONTAINER_DETACHED',
21: 'WATER_TANK_DETACHED',
}
class NotificationType(StrEnum):
"""Coffee machine notification types"""
STATUS = 'status'
PROCESS = 'process'
class BeverageCommand:
"""Coffee machine beverage commands"""
def __init__(self, on, off):
self.on = on
self.off = off
class BeverageNotify:
"""Coffee machine beverage notifications"""
def __init__(self, kind, description):
self.kind = str(kind)
self.description = str(description)
class DeviceSwitches:
"""All binary switches for the device"""
def __init__(self):
self.sounds = False
self.energy_save = False
self.cup_light = False
self.filter = False
self.is_on = False
BEVERAGE_COMMANDS = {
AvailableBeverage.STEAM: BeverageCommand(STEAM_ON, STEAM_OFF),
AvailableBeverage.LONG: BeverageCommand(LONG_ON, LONG_OFF),
AvailableBeverage.COFFEE: BeverageCommand(COFFE_ON, COFFE_OFF),
AvailableBeverage.DOPIO: BeverageCommand(DOPPIO_ON, DOPPIO_OFF),
AvailableBeverage.HOTWATER: BeverageCommand(HOTWATER_ON, HOTWATER_OFF),
AvailableBeverage.ESPRESSO: BeverageCommand(ESPRESSO_ON, ESPRESSO_OFF),
AvailableBeverage.AMERICANO: BeverageCommand(AMERICANO_ON, AMERICANO_OFF),
AvailableBeverage.ESPRESSO2: BeverageCommand(ESPRESSO2_ON, ESPRESSO2_OFF),
}
DEVICE_NOTIFICATION = {
str(bytearray(DEVICE_READY)): BeverageNotify(
NotificationType.STATUS, 'DeviceOK'
),
str(bytearray(DEVICE_TURNOFF)): BeverageNotify(
NotificationType.STATUS, 'DeviceOFF'
),
str(bytearray(WATER_TANK_DETACHED)): BeverageNotify(
NotificationType.STATUS, 'NoWaterTank'
),
str(bytearray(WATER_SHORTAGE)): BeverageNotify(
NotificationType.STATUS, 'NoWater'
),
str(bytearray(COFFEE_GROUNDS_CONTAINER_DETACHED)): BeverageNotify(
NotificationType.STATUS, 'NoGroundsContainer'
),
str(bytearray(COFFEE_GROUNDS_CONTAINER_FULL)): BeverageNotify(
NotificationType.STATUS, 'GroundsContainerFull'
),
str(bytearray(START_COFFEE)): BeverageNotify(
NotificationType.STATUS, 'START_COFFEE'
),
}
class DelonghiDeviceEntity:
"""Entity class for the Delonghi devices"""
_attr_has_entity_name = True
def __init__(self, delongh_device, hass: HomeAssistant):
"""Init entity with the device"""
self._attr_unique_id = f'{delongh_device.mac}_{self.__class__.__name__}' # noqa: E501
self.device: DelongiPrimadonna = delongh_device
self.hass = hass
@property
def device_info(self):
"""Shared device info information"""
return {
'identifiers': {(DOMAIN, self.device.mac)},
'connections': {(dr.CONNECTION_NETWORK_MAC, self.device.mac)},
'name': self.device.name,
'manufacturer': 'Delongi',
'model': self.device.model,
}
def sign_request(message):
"""Request signer"""
deviser = 0x1D0F
for item in message[: len(message) - 2]:
i3 = (((deviser << 8) | (deviser >> 8)) & 0x0000FFFF) ^ (item & 0xFFFF)
i4 = i3 ^ ((i3 & 0xFF) >> 4)
i5 = i4 ^ ((i4 << 12) & 0x0000FFFF)
deviser = i5 ^ (((i5 & 0xFF) << 5) & 0x0000FFFF)
signature = list((deviser & 0x0000FFFF).to_bytes(2, byteorder='big'))
message[len(message) - 2] = signature[0]
message[len(message) - 1] = signature[1]
return message
class DelongiPrimadonna:
"""Delongi Primadonna class"""
def __init__(self, config: dict, hass: HomeAssistant) -> None:
"""Initialize device"""
self._device_status = None
self._client = None
self._hass = hass
self._device = None
self._connecting = False
self.mac = config.get(CONF_MAC)
self.name = config.get(CONF_NAME)
self.hostname = ''
self.model = 'Prima Donna'
self.friendly_name = ''
self.cooking = AvailableBeverage.NONE
self.connected = False
self.notify = False
self.steam_nozzle = NOZZLE_STATE[-1]
self.service = 0
self.status = DEVICE_STATUS[5]
self.switches = DeviceSwitches()
async def disconnect(self):
"""Disconnect from the device"""
_LOGGER.info('Disconnect from %s', self.mac)
if (self._client is not None) and self._client.is_connected:
await self._client.disconnect()
async def _connect(self):
"""
Connect to the device
:raises BleakError: if the device is not found
"""
self._connecting = True
try:
if (self._client is None) or (not self._client.is_connected):
self._device = bluetooth.async_ble_device_from_address(
self._hass, self.mac, connectable=True
)
if not self._device:
raise BleakError(
f'A device with address {self.mac} \
could not be found.'
)
self._client = BleakClient(self._device)
_LOGGER.info('Connect to %s', self.mac)
await self._client.connect()
await self._client.start_notify(
uuid.UUID(CONTROLL_CHARACTERISTIC), self._handle_data
)
except Exception as error:
self._connecting = False
raise error
self._connecting = False
def _make_command(self):
"""Make hex command"""
base_command = list(BASE_COMMAND)
base_command[3] = '1' if self.switches.energy_save else '0'
base_command[4] = '1' if self.switches.cup_light else '0'
base_command[5] = '1' if self.switches.sounds else '0'
if self.notify:
_LOGGER.warning('Command bin: %s', ''.join(base_command))
bytes_general_command = BYTES_GENERAL_COMMAND
bytes_general_command[9] = int(''.join(base_command), 2)
if self.notify:
_LOGGER.warning('Command bytes: %s', bytes_general_command)
return bytes_general_command
async def _event_trigger(self, value):
"""
Trigger event
:param value: event value
"""
event_data = {'data': str(hexlify(value, ' '))}
notification_message = (
str(hexlify(value, ' '))
.replace(' ', ', 0x')
.replace("b'", '[0x')
.replace("'", ']')
)
if str(bytearray(value)) in DEVICE_NOTIFICATION:
notification_message = DEVICE_NOTIFICATION.get(
str(bytearray(value))
).description
event_data.setdefault(
'type', DEVICE_NOTIFICATION.get(str(bytearray(value))).kind
)
event_data.setdefault(
'description',
DEVICE_NOTIFICATION.get(str(bytearray(value))).description,
)
self._hass.bus.async_fire(f'{DOMAIN}_event', event_data)
if self.notify:
await self._hass.services.async_call(
'persistent_notification',
'create',
{
'message': notification_message,
'title': f'{self.name} {self.mac}',
'notification_id': f'{self.mac}_err_{uuid.uuid4()}',
},
)
_LOGGER.info('Event triggered: %s', event_data)
async def _handle_data(self, sender, value):
if len(value) > 9:
self.switches.is_on = value[9] > 0
if len(value) > 4:
self.steam_nozzle = NOZZLE_STATE.get(value[4], value[4])
if len(value) > 7:
self.service = value[7]
if len(value) > 5:
self.status = DEVICE_STATUS.get(value[5], DEVICE_STATUS.get(5))
if self._device_status != hexlify(value, ' '):
_LOGGER.info('Received data: %s from %s', hexlify(value, ' '), sender) # noqa: E501
await self._event_trigger(value)
self._device_status = hexlify(value, ' ')
async def power_on(self) -> None:
"""Turn the device on."""
await self.send_command(BYTES_POWER)
async def cup_light_on(self) -> None:
"""Turn the cup light on."""
self.switches.cup_light = True
await self.send_command(self._make_command())
async def cup_light_off(self) -> None:
"""Turn the cup light off."""
self.switches.cup_light = False
await self.send_command(self._make_command())
async def energy_save_on(self):
"""Enable energy save mode"""
self.switches.energy_save = True
await self.send_command(self._make_command())
async def energy_save_off(self):
"""Enable energy save mode"""
self.switches.energy_save = False
await self.send_command(self._make_command())
async def sound_alarm_on(self):
"""Enable sound alarm"""
self.switches.sounds = True
await self.send_command(self._make_command())
async def sound_alarm_off(self):
"""Disable sound alarm"""
self.switches.sounds = False
await self.send_command(self._make_command())
async def beverage_start(self, beverage: AvailableBeverage) -> None:
"""Start beverage"""
await self.send_command(BEVERAGE_COMMANDS.get(beverage).on)
async def beverage_cancel(self) -> None:
"""Cancel beverage"""
if self.cooking != AvailableBeverage.NONE:
await self.send_command(BEVERAGE_COMMANDS.get(self.cooking).off)
async def debug(self):
"""Send command which causes status reply"""
await self.send_command(DEBUG)
async def get_device_name(self):
"""
Get device name
:return: device name
"""
try:
await self._connect()
self.hostname = bytes(
await self._client.read_gatt_char(uuid.UUID(NAME_CHARACTERISTIC)) # noqa: E501
).decode('utf-8')
await self._client.write_gatt_char(
uuid.UUID(CONTROLL_CHARACTERISTIC), bytearray(DEBUG)
)
self.connected = True
except BleakDBusError as error:
self.connected = False
_LOGGER.warning('BleakDBusError: %s', error)
except BleakError as error:
self.connected = False
_LOGGER.warning('BleakError: %s', error)
except asyncio.exceptions.TimeoutError as error:
self.connected = False
_LOGGER.info('TimeoutError: %s at device connection', error)
except asyncio.exceptions.CancelledError as error:
self.connected = False
_LOGGER.warning('CancelledError: %s', error)
async def select_profile(self, profile_id) -> None:
"""select a profile."""
message = [0x0D, 0x06, 0xA9, 0xF0, profile_id, 0xD7, 0xC0]
await self.send_command(message)
async def common_command(self, command: str) -> None:
"""Send custom BLE command"""
message = [int(x, 16) for x in command.split(' ')]
await self.send_command(message)
async def send_command(self, message):
await self._connect()
try:
sign_request(message)
_LOGGER.info('Send command: %s', hexlify(bytearray(message), ' '))
await self._client.write_gatt_char(
CONTROLL_CHARACTERISTIC, bytearray(message)
)
except BleakError as error:
self.connected = False
_LOGGER.warning('BleakError: %s', error)