Skip to content

Commit

Permalink
implement connection test, translations and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bj00rn committed Jan 20, 2023
1 parent 08cb3ba commit a44cbba
Show file tree
Hide file tree
Showing 21 changed files with 66 additions and 45 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_ftx: info
custom_components.saleryd_hrv: info

# If you need to debug uncomment the line below (doc: https://www.home-assistant.io/integrations/debugpy/)
debugpy:
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"9123:8123"
],
"mounts": [
"type=bind,source=/etc/localtime,target=/etc/localtime,readonly"
//"type=bind,source=/etc/localtime,target=/etc/localtime,readonly"
],
"containerEnv": {
"TZ": "Europe/Stockholm"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
"""Adds config flow for SalerydLoke."""
import logging
import asyncio

from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.const import CONF_NAME
import voluptuous as vol

from .gateway import Gateway
from .gateway import Gateway, State
from .const import (
CONF_WEBSOCKET_PORT,
CONF_WEBSOCKET_IP,
DOMAIN,
PLATFORMS,
MANUFACTURER,
NAME,
)


_LOGGER: logging.Logger = logging.getLogger(__package__)


class SalerydLokeFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for SalerydLoke."""

VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH

def __init__(self):
"""Initialize."""
Expand Down Expand Up @@ -50,7 +54,7 @@ async def async_step_user(self, user_input=None):
# Provide defaults for form
user_input[CONF_WEBSOCKET_IP] = "192.168.1.151"
user_input[CONF_WEBSOCKET_PORT] = 3001
user_input[CONF_NAME] = MANUFACTURER
user_input[CONF_NAME] = NAME

return await self._show_config_form(user_input)

Expand Down Expand Up @@ -79,18 +83,24 @@ 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"""
return True

# try:
# session = async_create_clientsession(self.hass)
# gateway = Gateway(session, ip, port)
# data = await gateway.send_command("CV", "")
# if data:
# return True
# except Exception as e: # pylint: disable=broad-except
# pass

# return False

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

try:
session = async_create_clientsession(self.hass)
gateway = Gateway(session, ip, port)
await asyncio.wait_for(connected(gateway), 10)
return True
except Exception as e: # pylint: disable=broad-except
_LOGGER.error("Could not connect", exc_info=True)
pass

return False


# @staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Base component constants
NAME = "Saleryd HRV integration"
MANUFACTURER = "Saleryd"
DOMAIN = "saleryd_ftx"
DOMAIN = "saleryd_hrv"
DOMAIN_DATA = f"{DOMAIN}_data"
VERSION = "0.0.1"
ATTRIBUTION = "Data provided by http://jsonplaceholder.typicode.com/"
ATTRIBUTION = "Data provided by Saleryd HRV"
ISSUE_URL = "https://github.com/bj00rn/ha-saleryd-ftx/issues"

# Icons
Expand All @@ -20,7 +20,7 @@
SWITCH = "switch"
FAN = "fan"
CLIMATE = "climate"
PLATFORMS = [SENSOR, FAN, CLIMATE]
PLATFORMS = [SENSOR, CLIMATE]


# Configuration and options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DEFAULT_NAME, DOMAIN, ATTRIBUTION
from .const import DEFAULT_NAME, DOMAIN, ATTRIBUTION, MANUFACTURER


class SalerydLokeEntity(CoordinatorEntity):
Expand All @@ -29,5 +29,5 @@ def __init__(
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, entry_id)},
name=DEFAULT_NAME,
manufacturer="Saleryd",
)
manufacturer=MANUFACTURER,
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class SalerydFan(FanEntity, SalerydLokeEntity):
"""Representation of a HRV as fan."""

@property
def is_on(self) -> str:
def is_on(self) -> bool:
"""Return the current preset_mode."""
return self.coordinator.data.get(self.entity_description.key)[0] == 1
return self.coordinator.data.get(self.entity_description.key)[0] is not None


async def async_setup_entry(hass, entry, async_add_entities: AddEntitiesCallback):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from .websocket import WSClient, Signal
from .websocket import WSClient, Signal, State

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand All @@ -11,19 +11,22 @@ def __init__(self, session, url, port):
self._url = url
self._port = port
self._session = session
self._state = State.NONE
self._handlers = []
self._ws = WSClient(self._session, self._url, self._port, self._handler)
self._ws.start()
self._handlers = []

def add_handler(self, handler):
"""Add event handler"""
self._handlers.append(handler)

async def _handler(self, signal: Signal):
"""Call handlers"""
"""Call handlers if data"""
if signal == Signal.DATA:
for handler in self._handlers:
handler(self.data)
elif signal == Signal.CONNECTION_STATE:
self._state = self._ws.state

def _parse_message(self, msg):
"""parse socket message"""
Expand All @@ -50,6 +53,10 @@ def _parse_message(self, msg):
_LOGGER.warning("Failed to parse message %s", msg, exc_info=True)
return parsed

@property
def state(self):
return self._state

@property
def data(self):
"""Get data"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"domain": "saleryd_ftx",
"domain": "saleryd_hrv",
"name": "Saleryd HRV",
"documentation": "https://github.com/bj00rn/ha-saleryd-ftx",
"iot_class": "local_polling",
"iot_class": "local_push",
"issue_tracker": "https://github.com/bj00rn/ha-saleryd-ftx",
"version": "0.0.5",
"config_flow": true,
"codeowners": [
"@bj00rn"
]
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
"config": {
"step": {
"user": {
"description": "Follow instructions in manual to connect HRV to local network. Enter connections details below. If you need help with the configuration have a look here: https://github.com/bj00rn/ha-saleryd-ftx",
"description": "Follow instructions in manual to connect the HRV unit to local network and enter connection details below. If you need help with the configuration have a look here: https://github.com/bj00rn/ha-saleryd-ftx",
"data": {
"name": "Name",
"websocket_ip": "Websocket Ip adress for the HRV web interface, ie 192.168.1.151",
"websocket_ip": "Websocket IP adress for the HRV web interface, ie 192.168.1.151",
"websocket_port": "Port"
}
}
},
"error": {
"auth": "Could not connect. Maybe Websocket URL/Port is wrong?"
"connect": "Could not connect. Verify connection details"
},
"abort": {
"single_instance_allowed": "Only a single instance is allowed."
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
"config": {
"step": {
"user": {
"description": "Follow instructions in manual to connect HRV to local network. Enter connections details below. If you need help with the configuration have a look here: https://github.com/bj00rn/ha-saleryd-ftx",
"description": "Follow instructions in manual to connect the HRV unit to local network and enter connection details below. If you need help with the configuration have a look here: https://github.com/bj00rn/ha-saleryd-ftx",
"data": {
"name": "Name",
"websocket_ip": "Websocket Ip adress for the HRV web interface, ie 192.168.1.151",
"websocket_ip": "Websocket IP adress for the HRV web interface, ie 192.168.1.151",
"websocket_port": "Port"
}
}
},
"error": {
"auth": "Could not connect. Maybe Websocket URL/Port is wrong?"
"connect": "Could not connect. Verify connection details"
},
"abort": {
"single_instance_allowed": "Only a single instance is allowed."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class Signal(enum.Enum):

class State(enum.Enum):
"""State of the connection."""

NONE = ""
RETRYING = "retrying"
RUNNING = "running"
Expand Down
11 changes: 8 additions & 3 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"name": "Saleryd HRV",
"hacs": "1.6.0",
"homeassistant": "2022.8.7",
"country": ["FI", "NO", "SE", "DK"]
}
"homeassistant": "2023.1.5",
"country": [
"FI",
"NO",
"SE",
"DK"
]
}
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import aiohttp
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from custom_components.saleryd_ftx.api import SalerydLokeApiClient
from custom_components.saleryd_hrv.api import SalerydLokeApiClient


async def test_api(hass, aioclient_mock, caplog):
Expand Down

0 comments on commit a44cbba

Please sign in to comment.