Skip to content

Commit

Permalink
Merge pull request #27 from Pyhass/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Khole authored May 8, 2021
2 parents b6211fb + d35be2d commit 5c8379c
Show file tree
Hide file tree
Showing 25 changed files with 1,774 additions and 698 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
push:
branches:
- master
- dev
pull_request: ~

env:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Here are examples and documentation on how to use the library independently.

https://pyhass.github.io/pyhiveapi.docs/ [WIP]


32 changes: 0 additions & 32 deletions azure-pipelines.yml

This file was deleted.

60 changes: 48 additions & 12 deletions pyhiveapi/apyhiveapi/action.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
"""Hive Action Module."""


class Action:
"""Hive Action Code."""
class HiveAction:
"""Hive Action Code.
Returns:
object: Return hive action object.
"""

actionType = "Actions"

def __init__(self, session=None):
"""Initialise Action."""
def __init__(self, session: object = None):
"""Initialise Action.
Args:
session (object, optional): session to interact with hive account. Defaults to None.
"""
self.session = session

async def getAction(self, device):
"""Get smart plug current power usage."""
async def getAction(self, device: dict):
"""Action device to update.
Args:
device (dict): Device to be updated.
Returns:
dict: Updated device.
"""
dev_data = {}

if device["hiveID"] in self.data["action"]:
Expand All @@ -35,8 +50,15 @@ async def getAction(self, device):
return "REMOVE"
return device

async def getState(self, device):
"""Get action state."""
async def getState(self, device: dict):
"""Get action state.
Args:
device (dict): Device to get state of.
Returns:
str: Return state.
"""
final = None

try:
Expand All @@ -47,8 +69,15 @@ async def getState(self, device):

return final

async def turnOn(self, device):
"""Set action turn on."""
async def setStatusOn(self, device: dict):
"""Set action turn on.
Args:
device (dict): Device to set state of.
Returns:
boolean: True/False if successful.
"""
import json

final = False
Expand All @@ -65,8 +94,15 @@ async def turnOn(self, device):

return final

async def turnOff(self, device):
"""Set action to turn off."""
async def setStatusOff(self, device: dict):
"""Set action to turn off.
Args:
device (dict): Device to set state of.
Returns:
boolean: True/False if successful.
"""
import json

final = False
Expand Down
127 changes: 127 additions & 0 deletions pyhiveapi/apyhiveapi/alarm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""Hive Alarm Module."""


class HiveHomeShield:
"""Hive homeshield alarm.
Returns:
object: Hive homeshield
"""

alarmType = "Alarm"

async def getMode(self):
"""Get current mode of the alarm.
Returns:
str: Mode if the alarm [armed_home, armed_away, armed_night]
"""
state = None

try:
data = self.session.data.alarm
state = data["mode"]
except KeyError as e:
await self.session.log.error(e)

return state

async def getState(self, device: dict):
"""Get the alarm triggered state.
Returns:
boolean: True/False if alarm is triggered.
"""
state = None

try:
data = self.session.data.devices[device["hiveID"]]
state = data["state"]["alarmActive"]
except KeyError as e:
await self.session.log.error(e)

return state

async def setMode(self, device: dict, mode: str):
"""Set the alarm mode.
Args:
device (dict): Alarm device.
Returns:
boolean: True/False if successful.
"""
final = False

if (
device["hiveID"] in self.session.data.devices
and device["deviceData"]["online"]
):
await self.session.hiveRefreshTokens()
resp = await self.session.api.setAlarm(mode=mode)
if resp["original"] == 200:
final = True
await self.session.getAlarm()

return final


class Alarm(HiveHomeShield):
"""Home assistant alarm.
Args:
HiveHomeShield (object): Class object.
"""

def __init__(self, session: object = None):
"""Initialise alarm.
Args:
session (object, optional): Used to interact with the hive account. Defaults to None.
"""
self.session = session

async def getAlarm(self, device: dict):
"""Get alarm data.
Args:
device (dict): Device to update.
Returns:
dict: Updated device.
"""
device["deviceData"].update(
{"online": await self.session.attr.onlineOffline(device["device_id"])}
)
dev_data = {}

if device["deviceData"]["online"]:
self.session.helper.deviceRecovered(device["device_id"])
data = self.session.data.devices[device["device_id"]]
dev_data = {
"hiveID": device["hiveID"],
"hiveName": device["hiveName"],
"hiveType": device["hiveType"],
"haName": device["haName"],
"haType": device["haType"],
"device_id": device["device_id"],
"device_name": device["device_name"],
"status": {
"state": await self.getState(device),
"mode": await self.getMode(),
},
"deviceData": data.get("props", None),
"parentDevice": data.get("parent", None),
"custom": device.get("custom", None),
"attributes": await self.session.attr.stateAttributes(
device["device_id"], device["hiveType"]
),
}

self.session.devices.update({device["hiveID"]: dev_data})
return self.session.devices[device["hiveID"]]
else:
await self.session.log.errorCheck(
device["device_id"], "ERROR", device["deviceData"]["online"]
)
return device
21 changes: 18 additions & 3 deletions pyhiveapi/apyhiveapi/api/hive_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, hiveSession=None, websession=None, token=None):
"weather": "https://weather.prod.bgchprod.info/weather",
"holiday_mode": "/holiday-mode",
"all": "/nodes/all?products=true&devices=true&actions=true",
"alarm": "/security-lite?homeId=",
"devices": "/devices",
"products": "/products",
"actions": "/actions",
Expand Down Expand Up @@ -111,9 +112,23 @@ def getAll(self):
"""Build and query all endpoint."""
url = self.urls["base"] + self.urls["all"]
try:
response = self.request("GET", url)
self.json_return.update({"original": response.status_code})
self.json_return.update({"parsed": response.json()})
info = self.request("GET", url)
self.json_return.update({"original": info.status_code})
self.json_return.update({"parsed": info.json()})
except (OSError, RuntimeError, ZeroDivisionError):
self.error()

return self.json_return

def getAlarm(self, homeID=None):
"""Build and query alarm endpoint."""
if self.session is not None:
homeID = self.session.config.homeID
url = self.urls["base"] + self.urls["alarm"] + homeID
try:
info = self.request("GET", url)
self.json_return.update({"original": info.status_code})
self.json_return.update({"parsed": info.json()})
except (OSError, RuntimeError, ZeroDivisionError):
self.error()

Expand Down
Loading

0 comments on commit 5c8379c

Please sign in to comment.