def addDeviceLink(self, targetLocation: int, deviceId: int = None, deviceUid: str = None) -> Optional[DeviceLink]: """ Add a new device link, from deviceId or deviceUid to targetLocation :param targetLocation: int :param deviceId: int :param deviceUid: str :return: """ device = self.getDevice(deviceId=deviceId, uid=deviceUid) if not device: raise Exception('Unknown device requested for link.') if targetLocation <= 0: raise Exception('Cannot add a device link to no location') if device.linkedTo(targetLocation): raise Exception('This device is already linked to that location') data = { 'deviceId' : device.id, 'targetLocation': targetLocation } link = DeviceLink(data) self._deviceLinks[link.id] = link return link
def addLink(self, deviceId: int, locationId: int): device = self.getDeviceById(deviceId) deviceType = device.getDeviceType() if not deviceType.allowLocationLinks: raise Exception( f'Device type {deviceType.name} can\'t be linked to other rooms' ) if self.getLink(deviceId=deviceId, locationId=locationId): raise Exception( f'There is already a link from {deviceId} to {locationId}') #todo check if adding locSettings here is required values = { 'deviceID': deviceId, 'locationID': locationId, 'locSettings': json.dumps(deviceType._locSettings) } # noinspection SqlResolve values['id'] = self.databaseInsert( tableName=self.DB_LINKS, query= 'INSERT INTO :__table__ (deviceID, locationID, locSettings) VALUES (:deviceID, :locationID, :locSettings)', values=values) self.logInfo( f'Added link from device {deviceId} to location {locationId}') self._deviceLinks[values['id']] = DeviceLink(data=values)
def loadLinks(self): """ Loads location links from database :return: None """ for row in self.databaseFetch(tableName=self.DB_LINKS): link: DeviceLink = DeviceLink(row) if link.invalid: self.logWarning(f'Device link id **{link.id}** seems deprecated, removing') self.deleteDeviceLinks(linkId=link.id) self.loadLinks() else: self._deviceLinks[row['id']] = link
def loadLinks(self): for row in self.databaseFetch(tableName=self.DB_LINKS, method='all'): self._deviceLinks[row['id']] = DeviceLink(row)