Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows multiple entries to be added for the integration #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions custom_components/uponor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
from datetime import timedelta
import math
import ipaddress
import requests
import voluptuous as vol
import logging

from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry

from homeassistant.const import CONF_HOST
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import async_load_platform
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from UponorJnap import UponorJnap

from .const import (
Expand All @@ -37,32 +30,38 @@
DEFAULT_TEMP
)

from .helper import (
get_unique_id_from_config_entry
)

_LOGGER = logging.getLogger(__name__)


async def async_setup(hass: HomeAssistant, config: dict):
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN]["config"] = config.get(DOMAIN) or {}
# just return True, we're not using this!
# hass.data.setdefault(DOMAIN, {})
# hass.data[DOMAIN]["config"] = config.get(DOMAIN) or {}
return True


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
host = config_entry.data[CONF_HOST]
unique_id = get_unique_id_from_config_entry(config_entry)
store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY)

state_proxy = await hass.async_add_executor_job(lambda: UponorStateProxy(hass, host, store))
state_proxy = await hass.async_add_executor_job(lambda: UponorStateProxy(hass, host, store, unique_id))
await state_proxy.async_update(0)
thermostats = state_proxy.get_active_thermostats()

hass.data[DOMAIN] = {
hass.data[unique_id] = {
"state_proxy": state_proxy,
"thermostats": thermostats
}

def handle_set_variable(call):
var_name = call.data.get('var_name')
var_value = call.data.get('var_value')
hass.data[DOMAIN]['state_proxy'].set_variable(var_name, var_value)
hass.data[unique_id]['state_proxy'].set_variable(var_name, var_value)

hass.services.async_register(DOMAIN, "set_variable", handle_set_variable)

Expand All @@ -75,12 +74,13 @@ def handle_set_variable(call):


class UponorStateProxy:
def __init__(self, hass, host, store):
def __init__(self, hass, host, store, unique_id):
self._hass = hass
self._client = UponorJnap(host)
self._store = store
self._data = {}
self._storage_data = {}
self._unique_id = unique_id

# Thermostats config

Expand Down Expand Up @@ -217,7 +217,7 @@ def get_status(self, thermostat):
# HVAC modes

async def async_switch_to_cooling(self):
for thermostat in self._hass.data[DOMAIN]['thermostats']:
for thermostat in self._hass.data[self._unique_id]['thermostats']:
if self.get_setpoint(thermostat) == self.get_min_limit(thermostat):
await self._hass.async_add_executor_job(
lambda: self.set_setpoint(thermostat, self.get_max_limit(thermostat)))
Expand All @@ -227,7 +227,7 @@ async def async_switch_to_cooling(self):
async_dispatcher_send(self._hass, SIGNAL_UPONOR_STATE_UPDATE)

async def async_switch_to_heating(self):
for thermostat in self._hass.data[DOMAIN]['thermostats']:
for thermostat in self._hass.data[self._unique_id]['thermostats']:
if self.get_setpoint(thermostat) == self.get_max_limit(thermostat):
await self._hass.async_add_executor_job(
lambda: self.set_setpoint(thermostat, self.get_min_limit(thermostat)))
Expand Down
18 changes: 12 additions & 6 deletions custom_components/uponor/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,37 @@
)

from .const import (
DOMAIN,
SIGNAL_UPONOR_STATE_UPDATE,
DEVICE_MANUFACTURER
)

from .helper import (
get_unique_id_from_config_entry
)

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass, entry, async_add_entities):
state_proxy = hass.data[DOMAIN]["state_proxy"]
unique_id = get_unique_id_from_config_entry(entry)

state_proxy = hass.data[unique_id]["state_proxy"]

entities = []
for thermostat in hass.data[DOMAIN]["thermostats"]:
for thermostat in hass.data[unique_id]["thermostats"]:
if thermostat.lower() in entry.data:
name = entry.data[thermostat.lower()]
else:
name = state_proxy.get_room_name(thermostat)
entities.append(UponorClimate(state_proxy, thermostat, name))
entities.append(UponorClimate(unique_id, state_proxy, thermostat, name))
if entities:
async_add_entities(entities, update_before_add=False)


class UponorClimate(ClimateEntity):

def __init__(self, state_proxy, thermostat, name):
def __init__(self, unique_instance_id, state_proxy, thermostat, name):
self._unique_instance_id = unique_instance_id
self._state_proxy = state_proxy
self._thermostat = thermostat
self._name = name
Expand Down Expand Up @@ -164,7 +170,7 @@ def unique_id(self):
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self._state_proxy.get_thermostat_id(self._thermostat))},
"identifiers": {(self._unique_instance_id, self._state_proxy.get_thermostat_id(self._thermostat))},
"name": self._name,
"manufacturer": DEVICE_MANUFACTURER,
"model": self._state_proxy.get_model(),
Expand Down
15 changes: 12 additions & 3 deletions custom_components/uponor/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from homeassistant import config_entries
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
import logging

from UponorJnap import UponorJnap
Expand All @@ -12,10 +11,15 @@

from .const import (
DOMAIN,
SIGNAL_UPONOR_STATE_UPDATE,
CONF_UNIQUE_ID,
DEVICE_MANUFACTURER
)

from .helper import (
create_unique_id_from_user_input,
generate_unique_id_from_user_input_conf_name,
)

_LOGGER = logging.getLogger(__name__)


Expand All @@ -30,13 +34,18 @@ def schema(self):
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_NAME, default=DEVICE_MANUFACTURER): str,
vol.Optional(CONF_UNIQUE_ID): str
}
)

async def async_step_user(self, user_input=None):
"""Handle the initial step."""
if user_input is not None:
await self.async_set_unique_id(DOMAIN)
unique_id = create_unique_id_from_user_input(user_input)
if unique_id is None:
unique_id = generate_unique_id_from_user_input_conf_name(user_input)

await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()

try:
Expand Down
2 changes: 2 additions & 0 deletions custom_components/uponor/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import timedelta

CONF_UNIQUE_ID = "unique_id"

DOMAIN = "uponor"

SIGNAL_UPONOR_STATE_UPDATE = "uponor_state_update"
Expand Down
28 changes: 28 additions & 0 deletions custom_components/uponor/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from .const import (
DOMAIN,
CONF_UNIQUE_ID
)

from homeassistant.config_entries import ConfigEntry

from homeassistant.const import (
CONF_NAME
)


def create_unique_id_from_user_input(user_input):
if CONF_UNIQUE_ID not in user_input and user_input[CONF_UNIQUE_ID] != "":
return user_input[CONF_UNIQUE_ID]

return None


def generate_unique_id_from_user_input_conf_name(user_input):
conf_name = user_input[CONF_NAME]
raw_unique_id = DOMAIN + "_" + conf_name
cleaned_unique_id = raw_unique_id.replace(" ", "_").lower()
return cleaned_unique_id


def get_unique_id_from_config_entry(config_entry : ConfigEntry):
return config_entry.unique_id
23 changes: 15 additions & 8 deletions custom_components/uponor/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@

from homeassistant.const import CONF_NAME
from .const import (
DOMAIN,
SIGNAL_UPONOR_STATE_UPDATE,
DEVICE_MANUFACTURER
)

from .helper import (
get_unique_id_from_config_entry
)


async def async_setup_entry(hass, entry, async_add_entities):
state_proxy = hass.data[DOMAIN]["state_proxy"]
entities = [AwaySwitch(state_proxy, entry.data[CONF_NAME])]
unique_id = get_unique_id_from_config_entry(entry)

state_proxy = hass.data[unique_id]["state_proxy"]
entities = [AwaySwitch(unique_id, state_proxy, entry.data[CONF_NAME])]

if state_proxy.is_cool_available():
entities.append(CoolSwitch(state_proxy, entry.data[CONF_NAME]))
entities.append(CoolSwitch(unique_id, state_proxy, entry.data[CONF_NAME]))

async_add_entities(entities)


class AwaySwitch(SwitchEntity):
def __init__(self, state_proxy, name):
def __init__(self, unique_instance_id, state_proxy, name):
self._state_proxy = state_proxy
self._name = name
self._unique_instance_id = unique_instance_id

@property
def name(self) -> str:
Expand Down Expand Up @@ -63,17 +69,18 @@ def unique_id(self):
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, "c")},
"identifiers": {(self._unique_instance_id, "c")},
"name": self._name,
"manufacturer": DEVICE_MANUFACTURER,
"model": self._state_proxy.get_model(),
}


class CoolSwitch(SwitchEntity):
def __init__(self, state_proxy, name):
def __init__(self, unique_instance_id, state_proxy, name):
self._state_proxy = state_proxy
self._name = name
self._unique_instance_id = unique_instance_id

@property
def name(self) -> str:
Expand Down Expand Up @@ -113,7 +120,7 @@ def unique_id(self):
@property
def device_info(self):
return {
"identifiers": {(DOMAIN, "c")},
"identifiers": {(self._unique_instance_id, "c")},
"name": self._name,
"manufacturer": DEVICE_MANUFACTURER,
"model": self._state_proxy.get_model(),
Expand Down
3 changes: 2 additions & 1 deletion custom_components/uponor/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"description": "Set up Uponor heating/cooling system",
"data": {
"host": "Host or IP address of the Uponor controller",
"name": "Name of the integration instance"
"name": "Name of the integration instance",
"unique_id": "Optional unique identifier (must be unique across entries)"
}
},
"rooms": {
Expand Down