Skip to content

Commit

Permalink
Add VPD mode trigger settings. Move timers that were text to numbers …
Browse files Browse the repository at this point in the history
…using boxes instead of sliders.. (#25)

* Split port entity base class into property and setting entities.  Create tuple port sensor entity.

* move timers to numbers now that I know boxes exist. Tests. vpd settings
  • Loading branch information
dalinicus authored Sep 17, 2023
1 parent 0060d34 commit b6e0b01
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 244 deletions.
83 changes: 79 additions & 4 deletions custom_components/ac_infinity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import logging
from datetime import timedelta
from typing import Tuple

import async_timeout
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -44,7 +45,11 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:


class ACInfinityEntity(CoordinatorEntity):
def __init__(self, coordinator: ACInfinityDataUpdateCoordinator, data_key: str):
def __init__(
self,
coordinator: ACInfinityDataUpdateCoordinator,
data_key: str,
):
super().__init__(coordinator)
self._data_key = data_key

Expand Down Expand Up @@ -89,31 +94,101 @@ def __init__(

self._attr_icon = icon
self._attr_device_info = port.device_info
self._attr_name = f"{device.device_name} {port.port_name} {label}"
self._attr_unique_id = (
f"{DOMAIN}_{device.mac_addr}_port_{port.port_id}_{data_key}"
)
self._attr_name = f"{device.device_name} {port.port_name} {label}"


class ACInfinityPortPropertyEntity(ACInfinityPortEntity):
def __init__(
self,
coordinator: ACInfinityDataUpdateCoordinator,
device: ACInfinityDevice,
port: ACInfinityDevicePort,
data_key: str,
label: str,
icon: str,
) -> None:
super().__init__(coordinator, device, port, data_key, label, icon)

def get_property_value(self):
coordinator: ACInfinityDataUpdateCoordinator = self.coordinator
return coordinator.ac_infinity.get_device_port_property(
self._device.device_id, self._port.port_id, self._data_key
)

def get_setting_value(self, default=None):

class ACInfinityPortSettingEntity(ACInfinityPortEntity):
def __init__(
self,
coordinator: ACInfinityDataUpdateCoordinator,
device: ACInfinityDevice,
port: ACInfinityDevicePort,
data_key: str,
label: str,
icon: str,
) -> None:
super().__init__(coordinator, device, port, data_key, label, icon)

def get_setting_value(self, default=None) -> int:
coordinator: ACInfinityDataUpdateCoordinator = self.coordinator
return coordinator.ac_infinity.get_device_port_setting(
self._device.device_id, self._port.port_id, self._data_key, default
)

async def set_setting_value(self, value) -> None:
async def set_setting_value(self, value: int) -> None:
coordinator: ACInfinityDataUpdateCoordinator = self.coordinator
await coordinator.ac_infinity.set_device_port_setting(
self._device.device_id, self._port.port_id, self._data_key, value
)
await coordinator.async_request_refresh()


class ACInfinityPortTupleSettingEntity(ACInfinityPortEntity):
def __init__(
self,
coordinator: ACInfinityDataUpdateCoordinator,
device: ACInfinityDevice,
port: ACInfinityDevicePort,
tuple_key: Tuple[str, str],
label: str,
icon: str,
) -> None:
"""The first tuple value will be used as the "primary" data key used for ids.
Values will be fetched from api using both keys.
"""
(leftKey, _) = tuple_key
super().__init__(coordinator, device, port, leftKey, label, icon)
self._tuple_key = tuple_key

def get_setting_value(self, default=None) -> Tuple[int, int]:
coordinator: ACInfinityDataUpdateCoordinator = self.coordinator

(leftKey, rightKey) = self._tuple_key
leftValue = coordinator.ac_infinity.get_device_port_setting(
self._device.device_id, self._port.port_id, leftKey, default
)
rightValue = coordinator.ac_infinity.get_device_port_setting(
self._device.device_id, self._port.port_id, rightKey, default
)

return (leftValue, rightValue)

async def set_setting_value(self, value: Tuple[int, int]) -> None:
coordinator: ACInfinityDataUpdateCoordinator = self.coordinator

(leftKey, rightKey) = self._tuple_key
(leftValue, rightValue) = value
await coordinator.ac_infinity.set_device_port_settings(
self._device.device_id,
self._port.port_id,
[(leftKey, leftValue), (rightKey, rightValue)],
)

await coordinator.async_request_refresh()


class ACInfinityDataUpdateCoordinator(DataUpdateCoordinator):
"""Handles updating data for the integration"""

Expand Down
14 changes: 11 additions & 3 deletions custom_components/ac_infinity/ac_infinity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from datetime import timedelta
from typing import Any
from typing import Any, Tuple

from homeassistant.helpers.entity import DeviceInfo

Expand All @@ -20,6 +21,8 @@
PROPERTY_PORT_KEY_PORT,
)

_LOGGER = logging.getLogger(__name__)


class ACInfinityDevice:
def __init__(self, device_json) -> None:
Expand Down Expand Up @@ -110,7 +113,7 @@ def __init__(self, email: str, password: str) -> None:
self._devices: dict[str, dict[str, Any]] = {}
self._port_settings: dict[str, dict[int, Any]] = {}

async def update(self):
async def update(self, tryNum=0):
"""refreshes the values of properties and settings from the AC infinity API"""

if not self._client.is_logged_in():
Expand Down Expand Up @@ -193,5 +196,10 @@ async def set_device_port_setting(
self, device_id: (str | int), port_id: int, setting: str, value: int
):
"""set a desired value for a given device setting"""
self._port_settings[str(device_id)][port_id][setting] = value
await self._client.set_device_port_setting(device_id, port_id, setting, value)

async def set_device_port_settings(
self, device_id: (str | int), port_id: int, keyValues: list[Tuple[str, int]]
):
"""set a desired value for a given device setting"""
await self._client.set_device_port_settings(device_id, port_id, keyValues)
6 changes: 4 additions & 2 deletions custom_components/ac_infinity/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from custom_components.ac_infinity import (
ACInfinityDataUpdateCoordinator,
ACInfinityPortEntity,
ACInfinityPortPropertyEntity,
)
from custom_components.ac_infinity.const import DOMAIN, SENSOR_PORT_KEY_ONLINE

Expand All @@ -19,7 +19,9 @@
_LOGGER = logging.getLogger(__name__)


class ACInfinityPortBinarySensorEntity(ACInfinityPortEntity, BinarySensorEntity):
class ACInfinityPortBinarySensorEntity(
ACInfinityPortPropertyEntity, BinarySensorEntity
):
def __init__(
self,
coordinator: ACInfinityDataUpdateCoordinator,
Expand Down
13 changes: 11 additions & 2 deletions custom_components/ac_infinity/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Tuple

import aiohttp
import async_timeout
from homeassistant.exceptions import HomeAssistantError
Expand Down Expand Up @@ -49,7 +51,12 @@ async def get_device_port_settings(self, device_id: (str | int), port_id: int):
return json["data"]

async def set_device_port_setting(
self, device_id: (str | int), port_id: int, setting: str, value: int
self, device_id: (str | int), port_id: int, key: str, value: int
):
await self.set_device_port_settings(device_id, port_id, [(key, value)])

async def set_device_port_settings(
self, device_id: (str | int), port_id: int, keyValues: list[Tuple[str, int]]
):
active_settings = await self.get_device_port_settings(device_id, port_id)
payload = {
Expand Down Expand Up @@ -96,7 +103,9 @@ async def set_device_port_setting(
"vpdSettingMode": active_settings["vpdSettingMode"],
}

payload[setting] = int(value)
for key, value in keyValues:
payload[key] = int(value)

headers = self.__create_headers(use_auth_token=True)
_ = await self.__post(API_URL_ADD_DEV_MODE, payload, headers)

Expand Down
9 changes: 6 additions & 3 deletions custom_components/ac_infinity/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
Platform.SELECT,
Platform.NUMBER,
Platform.TIME,
Platform.TEXT,
]
HOST = "http://www.acinfinityserver.com"

Expand Down Expand Up @@ -42,5 +41,9 @@
SETTING_KEY_AT_TYPE = "atType"
SETTING_KEY_SCHEDULED_START_TIME = "schedStartTime"
SETTING_KEY_SCHEDULED_END_TIME = "schedEndtTime"
SETTING_KEY_ACTIVE_TIMER_ON = "acitveTimerOn"
SETTING_KEY_ACTIVE_TIMER_OFF = "acitveTimerOff"
SETTING_KEY_TIMER_DURATION_TO_ON = "acitveTimerOn"
SETTING_KEY_TIMER_DURATION_TO_OFF = "acitveTimerOff"
SETTING_KEY_VPD_HIGH_ENABLED = "activeHtVpd"
SETTING_KEY_VPD_HIGH_TRIGGER = "activeHtVpdNums"
SETTING_KEY_VPD_LOW_ENABLED = "activeLtVpd"
SETTING_KEY_VPD_LOW_TRIGGER = "activeLtVpdNums"
Loading

0 comments on commit b6e0b01

Please sign in to comment.