Skip to content

Commit

Permalink
add support for local api
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaasland committed Sep 15, 2021
1 parent 6f0ac46 commit 3962777
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
27 changes: 22 additions & 5 deletions custom_components/tahoma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from homeassistant.components.switch import DOMAIN as SWITCH
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_EXCLUDE, CONF_PASSWORD, CONF_SOURCE, CONF_USERNAME
from homeassistant.const import CONF_API_TOKEN, CONF_URL
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import (
Expand Down Expand Up @@ -126,16 +127,26 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up TaHoma from a config entry."""
hass.data.setdefault(DOMAIN, {})

username = entry.data.get(CONF_USERNAME)
password = entry.data.get(CONF_PASSWORD)
username = entry.data.get(CONF_USERNAME, '')
password = entry.data.get(CONF_PASSWORD, '')
base_url = entry.data.get(CONF_URL, '')
api_token = entry.data.get(CONF_API_TOKEN, '')
hub = entry.data.get(CONF_HUB, DEFAULT_HUB)
server = SUPPORTED_SERVERS[entry.data.get(CONF_HUB, DEFAULT_HUB)]

server = SUPPORTED_SERVERS[hub]
api_url = server.endpoint

if hub == "somfy_local":
api_url = base_url + server.endpoint

session = async_get_clientsession(hass)
client = TahomaClient(
username,
password,
session=session,
api_url=server.endpoint,
api_url=api_url,
api_token=api_token
)

try:
Expand All @@ -145,9 +156,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
client.get_devices(),
client.get_scenarios(),
client.get_gateways(),
client.get_places(),
]
devices, scenarios, gateways, places = await asyncio.gather(*tasks)

if hub != "somfy_local":
tasks.append(client.get_places())
devices, scenarios, gateways, places = await asyncio.gather(*tasks)
else:
devices, scenarios, gateways = await asyncio.gather(*tasks)
places = None

except BadCredentialsException as exception:
raise ConfigEntryAuthFailed from exception
except TooManyRequestsException as exception:
Expand Down
27 changes: 19 additions & 8 deletions custom_components/tahoma/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from homeassistant import config_entries
from homeassistant.components.dhcp import HOSTNAME
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.const import CONF_URL, CONF_API_TOKEN
from homeassistant.core import callback
from homeassistant.data_entry_flow import AbortFlow, FlowResult
from homeassistant.helpers import config_validation as cv, device_registry as dr
Expand Down Expand Up @@ -46,17 +47,25 @@ def async_get_options_flow(config_entry):
def __init__(self) -> None:
"""Start the Overkiz config flow."""
self._reauth_entry = None
self._default_username = None
self._default_username = ''
self._default_password = ''
self._default_hub = DEFAULT_HUB

async def async_validate_input(self, user_input: dict[str, Any]) -> FlowResult:
"""Validate user credentials."""
username = user_input.get(CONF_USERNAME)
password = user_input.get(CONF_PASSWORD)

server = SUPPORTED_SERVERS[user_input.get(CONF_HUB, DEFAULT_HUB)]

async with TahomaClient(username, password, api_url=server.endpoint) as client:
username = user_input.get(CONF_USERNAME, '')
password = user_input.get(CONF_PASSWORD, '')
base_url = user_input.get(CONF_URL, '')
api_token = user_input.get(CONF_API_TOKEN, '')
hub = user_input.get(CONF_HUB, DEFAULT_HUB)

server = SUPPORTED_SERVERS[hub]
api_url = server.endpoint

if hub == "somfy_local":
api_url = base_url + server.endpoint

async with TahomaClient(username, password, api_url=api_url, api_token=api_token) as client:
await client.login()

# Set first gateway as unique id
Expand Down Expand Up @@ -113,10 +122,12 @@ async def async_step_user(
data_schema=vol.Schema(
{
vol.Required(CONF_USERNAME, default=self._default_username): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_PASSWORD, default=self._default_password): str,
vol.Required(CONF_HUB, default=self._default_hub): vol.In(
{key: hub.name for key, hub in SUPPORTED_SERVERS.items()}
),
vol.Required(CONF_URL): str,
vol.Required(CONF_API_TOKEN): str,
}
),
errors=errors,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/tahoma/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(
self.client = client
self.devices: Dict[str, Device] = {d.deviceurl: d for d in devices}
self.executions: Dict[str, Dict[str, str]] = {}
self.areas = self.places_to_area(places)
self.areas = self.places_to_area(places) if places else None
self._config_entry_id = config_entry_id

async def _async_update_data(self) -> Dict[str, Device]:
Expand Down
6 changes: 5 additions & 1 deletion custom_components/tahoma/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,17 @@ def device_info(self) -> dict[str, Any]:
or self.device.widget
)

suggested_area = None
if self.device.placeoid:
suggested_area = self.coordinator.areas[self.device.placeoid]

return DeviceInfo(
identifiers={(DOMAIN, self.executor.base_device_url)},
name=self.device.label,
manufacturer=manufacturer,
model=model,
sw_version=self.executor.select_attribute(CORE_FIRMWARE_REVISION),
suggested_area=self.coordinator.areas[self.device.placeoid],
suggested_area=suggested_area,
via_device=self.executor.get_gateway_id(),
)

Expand Down

0 comments on commit 3962777

Please sign in to comment.