|
| 1 | +"""Hive Action Module.""" |
| 2 | +from .hive_session import Session |
| 3 | +from .hive_data import Data |
| 4 | + |
| 5 | + |
| 6 | +class Action(Session): |
| 7 | + """Hive Action Code.""" |
| 8 | + actionType = 'Actions' |
| 9 | + |
| 10 | + async def get_action(self, device): |
| 11 | + """Get smart plug current power usage.""" |
| 12 | + await self.logger.log(device["hiveID"], self.actionType, "Getting action data.") |
| 13 | + dev_data = {} |
| 14 | + |
| 15 | + if device["hiveID"] in Data.actions: |
| 16 | + dev_data = {"hiveID": device["hiveID"], |
| 17 | + "hiveName": device["hiveName"], |
| 18 | + "hiveType": device["hiveType"], |
| 19 | + "haName": device["haName"], |
| 20 | + "haType": device["haType"], |
| 21 | + "status": { |
| 22 | + "state": await self.get_state(device)}, |
| 23 | + "power_usage": None, |
| 24 | + "deviceData": {}, |
| 25 | + "custom": device.get("custom", None) |
| 26 | + } |
| 27 | + |
| 28 | + await self.logger.log(device["hiveID"], self.actionType, |
| 29 | + "action update {0}", info=[dev_data["status"]]) |
| 30 | + Data.ha_devices.update({device['hiveID']: dev_data}) |
| 31 | + return dev_data |
| 32 | + else: |
| 33 | + exists = Data.actions.get('hiveID', False) |
| 34 | + if exists == False: |
| 35 | + return 'REMOVE' |
| 36 | + return device |
| 37 | + |
| 38 | + async def get_state(self, device): |
| 39 | + """Get action state.""" |
| 40 | + await self.logger.log(device["hiveID"], self.actionType + "_Extra", "Getting state") |
| 41 | + state = None |
| 42 | + final = None |
| 43 | + |
| 44 | + if device["hiveID"] in Data.actions: |
| 45 | + data = Data.actions[device["hiveID"]] |
| 46 | + final = data["enabled"] |
| 47 | + await self.logger.log(device["hiveID"], self.actionType + "_Extra", "Status is {0}", info=[final]) |
| 48 | + if device["hiveID"] in Data.errorList: |
| 49 | + Data.errorList.pop(device["hiveID"]) |
| 50 | + else: |
| 51 | + await self.logger.error_check(device["hiveID"], "ERROR", "Failed") |
| 52 | + |
| 53 | + return final |
| 54 | + |
| 55 | + async def turn_on(self, device): |
| 56 | + """Set action turn on.""" |
| 57 | + import json |
| 58 | + await self.logger.log(device["hiveID"], self.actionType + "_Extra", "Enabling action") |
| 59 | + final = False |
| 60 | + |
| 61 | + if device["hiveID"] in Data.actions: |
| 62 | + await self.hiveRefreshTokens() |
| 63 | + data = Data.actions[device["hiveID"]] |
| 64 | + data.update({"enabled": True}) |
| 65 | + send = json.dumps(data) |
| 66 | + resp = await self.api.set_action(device["hiveID"], send) |
| 67 | + if resp["original"] == 200: |
| 68 | + final = True |
| 69 | + await self.getDevices(device["hiveID"]) |
| 70 | + await self.logger.log(device["hiveID"], "API", "Enabled action - " + device["hiveName"]) |
| 71 | + else: |
| 72 | + await self.logger.error_check( |
| 73 | + device["hiveID"], "ERROR", "Failed_API", resp=resp["original"]) |
| 74 | + |
| 75 | + return final |
| 76 | + |
| 77 | + async def turn_off(self, device): |
| 78 | + """Set action to turn off.""" |
| 79 | + import json |
| 80 | + await self.logger.log(device["hiveID"], self.actionType + "_Extra", "Disabling action") |
| 81 | + final = False |
| 82 | + |
| 83 | + if device["hiveID"] in Data.actions: |
| 84 | + await self.hiveRefreshTokens() |
| 85 | + data = Data.actions[device["hiveID"]] |
| 86 | + data.update({"enabled": False}) |
| 87 | + send = json.dumps(data) |
| 88 | + resp = await self.api.set_action(device["hiveID"], send) |
| 89 | + if resp["original"] == 200: |
| 90 | + final = True |
| 91 | + await self.getDevices(device["hiveID"]) |
| 92 | + await self.logger.log(device["hiveID"], "API", "Disabled action - " + device["hiveName"]) |
| 93 | + else: |
| 94 | + await self.logger.error_check( |
| 95 | + device["hiveID"], "ERROR", "Failed_API", resp=resp["original"]) |
| 96 | + |
| 97 | + return final |
0 commit comments