Skip to content

Commit

Permalink
first proper success
Browse files Browse the repository at this point in the history
  • Loading branch information
YeonV committed Nov 11, 2020
1 parent 4d3c375 commit 3f42601
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 48 deletions.
44 changes: 22 additions & 22 deletions custom_components/ledfxrm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from custom_components.ledfxrm.const import (
CONF_PORT,
CONF_HOST,
DOMAIN,
PLATFORMS,
STARTUP_MESSAGE,
Expand Down Expand Up @@ -68,23 +66,25 @@ async def async_setup(hass: HomeAssistant, config: Config):

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up this integration using UI."""

str_mydict = ''.join('{}{}'.format(key, val) for key, val in entry.data.items())
#logging.warning('ENTRY: %s', entry)
#str_mydict = ''.join('{}{}'.format(key, val) for key, val in entry.data.items())
#logging.warning('ENTRY: %s', str_mydict)
if hass.data.get(DOMAIN) is None:
hass.data.setdefault(DOMAIN, {})
_LOGGER.info(STARTUP_MESSAGE)

#thehost = entry.data.get('thehost')
#theport = entry.data.get('theport')
theurl = entry.data.get('rest_info').get('url')
thehost, theport = split_host_port(theurl)
logging.warning('URL %s ', theurl )
logging.warning('thehost %s ', thehost )
logging.warning('theport %s ', theport )
thehost = entry.data.get('host')
theport = entry.data.get('port')
theversion = entry.data.get('version')
#theurl = entry.data.get('rest_info').get('url')
#thehost, theport = split_host_port(theurl)
#logging.warning('URL %s ', theurl )
#logging.warning('thehost %s ', thehost )
#logging.warning('theport %s ', theport )
logging.warning('Version %s ', theversion )

coordinator = LedfxrmDataUpdateCoordinator(
hass, thehost, theport
hass, thehost, theport, theversion
)
await coordinator.async_refresh()

Expand All @@ -109,10 +109,10 @@ def __init__(self, thehost, theport):
self.thehost = thehost
self.theport = theport
async def update(self):
logging.warning('2222 host %s port: %s', self.thehost, str(self.theport))
url = self.thehost + ":" + str(self.theport) + "/api/info"
url2 = self.thehost + ":" + str(self.theport) + "/api/devices"
url3 = self.thehost + ":" + str(self.theport) + "/api/scenes"
#logging.warning('2222 host %s port: %s', self.thehost, str(self.theport))
url = "http://" + self.thehost + ":" + str(self.theport) + "/api/info"
url2 = "http://" + self.thehost + ":" + str(self.theport) + "/api/devices"
url3 = "http://" + self.thehost + ":" + str(self.theport) + "/api/scenes"
yz = {}
yz['rest_info'] = {}
yz['rest_devices'] = {}
Expand All @@ -130,7 +130,7 @@ async def update(self):
async with session.get(url3, ssl=False) as resp_scenes:
rest_scenes = await resp_scenes.json()
yz['rest_scenes'] = rest_scenes
logging.warning('REST_API: %s', yz)
#logging.warning('REST_API: %s', yz)
#service_data = {'entity_id': 'input_select.ledfx_seceneselector' ,'options': ['off' 'on']}
#hass.services.call('input_select', 'set_options', service_data)
logging.warning('REST_API: %s', yz)
Expand All @@ -140,12 +140,12 @@ async def update(self):

class LedfxrmDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching data from the API."""
def __init__(self, hass: HomeAssistant, thehost, theport):
def __init__(self, hass: HomeAssistant, thehost, theport, theversion):
"""Initialize."""

self.theversion = theversion
self.thehost = thehost
self.theport = theport
logging.warning('host port ::: %s ::: %s', thehost, str(theport))
#logging.warning('host port ::: %s ::: %s', thehost, str(theport))
self.api = myClient(thehost, theport)
self.platforms = []
#logging.warning('Good things done! Bad things start now:')
Expand All @@ -156,10 +156,10 @@ async def _async_update_data(self):
try:
data = await self.api.update()
#logging.warning('BOOOOM %s', data)
return
return data
except Exception as exception:
raise UpdateFailed(exception)
return {'host': '192.168.1.56'}
#return {'host': '192.168.1.56'}


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
Expand Down
29 changes: 8 additions & 21 deletions custom_components/ledfxrm/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ async def async_step_user( self, user_input=None ):
if user_input is not None:
#logging.warning('UserInput: %s', user_input['host'])
#logging.warning('UserInput: %s', user_input['port'])
api = await self.get_rest_status(
name, version = await self.get_rest_status(
user_input['host'], user_input['port']
)
if api:
if name:
#service_data = {'entity_id': 'input_select.ledfx_seceneselector' ,'options': api['rest_scenes']}
#hass.services.call('input_select', 'set_options', service_data)
data_attr = {'host': user_input['host'], 'port': user_input['port'], 'version': version, 'name': name}
return self.async_create_entry(
title=api['rest_info']['name'], data=api
title=name, data= data_attr
)
else:
self._errors["base"] = "auth"
Expand Down Expand Up @@ -80,28 +81,14 @@ async def get_rest_status(self, thehost, theport):
# logging.warning('Port: %s', str(theport))
loop = asyncio.get_event_loop()
url = "http://" + thehost + ":" + str(theport) + "/api/info"
url2 = "http://" + thehost + ":" + str(theport) + "/api/devices"
url3 = "http://" + thehost + ":" + str(theport) + "/api/scenes"
yz = {}
yz['rest_info'] = {}
yz['rest_devices'] = {}
yz['rest_scenes'] = {}
async with aiohttp.ClientSession(loop=loop, trust_env = True) as session:
async with session.get(url, ssl=False) as resp:
rest_info = await resp.json()
yz['rest_info'] = rest_info

async with session.get(url2, ssl=False) as resp_devices:
rest_devices = await resp_devices.json()
yz['rest_devices'] = rest_devices

async with session.get(url3, ssl=False) as resp_scenes:
rest_scenes = await resp_scenes.json()
yz['rest_scenes'] = rest_scenes

logging.warning('REST_API: %s', yz)
name = rest_info['name']
version = rest_info['version']
logging.warning('Config for %s | Version: %s', name, version)
#return yz
return yz
return name, version



Expand Down
4 changes: 2 additions & 2 deletions custom_components/ledfxrm/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

# Configuration and options
CONF_ENABLED = "enabled"
CONF_HOST = "192.168.1.56"
CONF_PORT = 8888
CONF_HOST = "host"
CONF_PORT = "port"

# Defaults
DEFAULT_NAME = DOMAIN
Expand Down
9 changes: 6 additions & 3 deletions custom_components/ledfxrm/entity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""LedfxrmEntity class"""
from homeassistant.helpers.update_coordinator import CoordinatorEntity
import logging

from custom_components.ledfxrm.const import DOMAIN, NAME, VERSION, MANUFACTURER

Expand All @@ -16,17 +17,19 @@ def unique_id(self):

@property
def device_info(self):
logging.warning('YZ: %s', self.coordinator.data)
return {
"identifiers": {(DOMAIN, self.unique_id)},
"name": NAME,
"model": VERSION,
"manufacturer": MANUFACTURER
"model": self.coordinator.data.get("info").get("name"),
"manufacturer": MANUFACTURER,
"sw_version": self.coordinator.data.get("info").get("version"),
}

@property
def device_state_attributes(self):
"""Return the state attributes."""
return {
"time": str(self.coordinator.data.get("time")),
"static": self.coordinator.data.get("static"),
"static": self.coordinator.data.get("version"),
}

0 comments on commit 3f42601

Please sign in to comment.