Skip to content

Commit

Permalink
use library from pypu
Browse files Browse the repository at this point in the history
  • Loading branch information
bj00rn committed Feb 7, 2023
1 parent 09a01ff commit 8d4fd90
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 300 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ default_config:
logger:
default: info
logs:
custom_components.saleryd_hrv: debug
custom_components.saleryd_hrv: info

# If you need to debug uncomment the line below (doc: https://www.home-assistant.io/integrations/debugpy/)
debugpy:
11 changes: 7 additions & 4 deletions custom_components/saleryd_hrv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from homeassistant.util import Throttle
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.exceptions import ConfigEntryNotReady
from pysaleryd.client import Client

from .gateway import Gateway
from .coordinator import SalerydLokeDataUpdateCoordinator

from .const import (
Expand All @@ -39,11 +39,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
url = entry.data.get(CONF_WEBSOCKET_IP)
port = entry.data.get(CONF_WEBSOCKET_PORT)

async def get_data():
return client.data

session = async_get_clientsession(hass)
gateway = Gateway(session, url, port)
client = Client(url, port, session)
coordinator = SalerydLokeDataUpdateCoordinator(
hass,
update_method=gateway.async_get_data,
update_method=get_data,
update_interval=SCAN_INTERVAL,
)

Expand All @@ -62,7 +65,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
# Register services
async def control_request(key, value=None):
_LOGGER.info("Sending control request %s with payload %s", key, value)
await gateway.send_command(key, value)
await client.send_command(key, value)

async def set_fireplace_mode(call):
value = call.data.get("value")
Expand Down
81 changes: 44 additions & 37 deletions custom_components/saleryd_hrv/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
ClimateEntity,
ClimateEntityFeature,
ClimateEntityDescription,
PRESET_HOME,
PRESET_AWAY,
PRESET_BOOST,
PRESET_ECO,
PRESET_COMFORT,
HVACMode,
HVACAction,
)

PRESET_COOL = "cool"

FAN_MODE_AUTO = "auto"
FAN_MODE_FIREPLACE = "fireplace"
FAN_MODE_HOME = "home"
FAN_MODE_AWAY = "away"
FAN_MODE_BOOST = "boost"

from .const import DOMAIN
from .entity import SalerydLokeEntity
Expand All @@ -36,17 +37,47 @@ class SalerydVentilation(_SalerydClimate):
ClimateEntityFeature.PRESET_MODE | ClimateEntityFeature.FAN_MODE
)
_attr_assumed_state = True
_attr_preset_modes = [PRESET_HOME, PRESET_AWAY, PRESET_BOOST]
_attr_fan_modes = [FAN_MODE_AUTO, FAN_MODE_FIREPLACE]
_attr_preset_modes = [PRESET_COMFORT, PRESET_ECO, PRESET_COOL]
_attr_fan_modes = [FAN_MODE_HOME, FAN_MODE_AWAY, FAN_MODE_BOOST]
_attr_hvac_modes = [HVACMode.FAN_ONLY, HVACMode.COOL]
_attr_temperature_unit = TEMP_CELSIUS

def set_hvac_mode(self, hvac_mode: HVACMode) -> None:
self.hass.services.call(
DOMAIN,
"set_cooling_mode",
{"value": self.hvac_modes.index(hvac_mode)},
blocking=True,
limit=10,
)
self.schedule_update_ha_state(force_refresh=True)

@property
def hvac_mode(self) -> HVACMode | str | None:
value = self.coordinator.data.get("MK")
if not isinstance(value, list):
return None
if value[0] == 0:
return HVACMode.FAN_ONLY
if value[0] == 1:
return HVACMode.COOL

@property
def hvac_action(self) -> HVACAction | str | None:
value = self.coordinator.data.get("MK")
if not isinstance(value, list):
return None
if value[0] == 0:
return HVACAction.IDLE
if value[0] == 1:
return HVACAction.COOLING

@property
def preset_mode(self) -> str | None:
"""Get current preset mode"""
if not self.coordinator.data.get("MF"):
if not self.coordinator.data.get("MT"):
return None
value = self.coordinator.data.get("MF")[0]
value = self.coordinator.data.get("MT")[0]
return self.preset_modes[value]

def set_preset_mode(self, preset_mode: str) -> None:
Expand All @@ -63,44 +94,20 @@ def set_preset_mode(self, preset_mode: str) -> None:

@property
def fan_mode(self) -> str | None:
if not self.coordinator.data.get("MB"):
return None
value = self.coordinator.data.get("MB")[0]
if value == 0:
return FAN_MODE_AUTO
if value == 1:
return FAN_MODE_FIREPLACE
fan_mode = self.coordinator.data.get("MF")
if isinstance(fan_mode, list):
return self.fan_modes[fan_mode[0]]

def set_fan_mode(self, fan_mode: str) -> None:
self.hass.services.call(
DOMAIN,
"set_fireplace_mode",
"set_ventilation_mode",
{"value": self.fan_modes.index(fan_mode)},
blocking=True,
limit=10,
)
self.schedule_update_ha_state(force_refresh=True)

@property
def hvac_action(self) -> HVACAction | str | None:
value = self.coordinator.data.get("MK")
if not isinstance(value, list):
return None
if value[0] == 0:
return HVACAction.IDLE
if value[0] == 1:
return HVACAction.COOLING

@property
def hvac_mode(self) -> HVACMode | str | None:
value = self.coordinator.data.get("MK")
if not isinstance(value, list):
return None
if value[0] == 0:
return HVACMode.FAN_ONLY
if value[0] == 1:
return HVACMode.COOL

@property
def current_temperature(self):
if not self.coordinator.data.get("*TC"):
Expand Down
12 changes: 6 additions & 6 deletions custom_components/saleryd_hrv/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from homeassistant.const import CONF_NAME
import voluptuous as vol

from .gateway import Gateway, State
from pysaleryd.client import Client
from .const import (
CONF_WEBSOCKET_PORT,
CONF_WEBSOCKET_IP,
Expand Down Expand Up @@ -84,17 +84,17 @@ async def _show_config_form(self, user_input): # pylint: disable=unused-argumen
async def _test_connection(self, ip, port):
"""Return true if connection is working"""

async def connected(gateway: Gateway):
"""Is gateway connected"""
async def connected(client: Client):
"""Is client connected"""
while True:
await asyncio.sleep(1)
if gateway.state == State.RUNNING:
if client.connected():
return True

try:
session = async_create_clientsession(self.hass)
gateway = Gateway(session, ip, port)
await asyncio.wait_for(connected(gateway), 10)
client = Client(ip, port, session)
await asyncio.wait_for(connected(client), 10)
return True
except Exception as e: # pylint: disable=broad-except
_LOGGER.error("Could not connect", exc_info=True)
Expand Down
2 changes: 0 additions & 2 deletions custom_components/saleryd_hrv/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from homeassistant.helpers.update_coordinator import (
DataUpdateCoordinator,
)

from .gateway import Gateway
from .const import DOMAIN


Expand Down
84 changes: 0 additions & 84 deletions custom_components/saleryd_hrv/gateway.py

This file was deleted.

3 changes: 3 additions & 0 deletions custom_components/saleryd_hrv/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"config_flow": true,
"codeowners": [
"@bj00rn"
],
"requirements": [
"pysaleryd>=2.0.0"
]
}
4 changes: 3 additions & 1 deletion custom_components/saleryd_hrv/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
SensorDeviceClass,
SensorStateClass,
)

from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
DataUpdateCoordinator,
Expand Down Expand Up @@ -132,6 +132,7 @@ def is_on(self) -> bool | None:
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.WATT,
entity_category=EntityCategory.DIAGNOSTIC,
),
},
"supply_fan_speed": {
Expand Down Expand Up @@ -195,6 +196,7 @@ def is_on(self) -> bool | None:
icon="mdi:wrench-clock",
name="Filter months left",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
},
}
Expand Down
Loading

0 comments on commit 8d4fd90

Please sign in to comment.