From d820be91d02ea118b5ed3141bbeba00ff0d96c2e Mon Sep 17 00:00:00 2001 From: Jonh Sady Date: Sun, 2 Feb 2025 18:03:21 -0300 Subject: [PATCH] adjustments --- homeassistant/components/redgtech/__init__.py | 8 +- .../components/redgtech/manifest.json | 3 +- homeassistant/components/redgtech/switch.py | 88 ++++++------------- requirements_all.txt | 6 ++ 4 files changed, 39 insertions(+), 66 deletions(-) diff --git a/homeassistant/components/redgtech/__init__.py b/homeassistant/components/redgtech/__init__.py index 9a4ea30f2da..867c3d85217 100644 --- a/homeassistant/components/redgtech/__init__.py +++ b/homeassistant/components/redgtech/__init__.py @@ -5,10 +5,11 @@ from homeassistant.const import Platform from .const import DOMAIN, API_URL from homeassistant.helpers.aiohttp_client import async_get_clientsession import aiohttp +from redgtech_api import RedgtechAPI _LOGGER = logging.getLogger(__name__) -PLATFORMS: list[Platform] = [Platform.SWITCH, Platform.LIGHT] +PLATFORMS: list[Platform] = [Platform.SWITCH] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Redgtech from a config entry.""" @@ -36,8 +37,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: "id": item.get('endpointId', ''), "name": item.get("name", f"Entity {item.get('endpointId', '')}"), "state": "on" if item.get("value", False) else "off", - "brightness": item.get("bright", 0), - "type": 'light' if 'dim' in item.get('endpointId', '').lower() else 'switch' + "type": 'switch' } for item in data.get("boards", []) ] @@ -60,4 +60,4 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" _LOGGER.debug("Unloading Redgtech entry: %s", entry.entry_id) - return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) \ No newline at end of file diff --git a/homeassistant/components/redgtech/manifest.json b/homeassistant/components/redgtech/manifest.json index 409fb7e4927..9838525ee2f 100644 --- a/homeassistant/components/redgtech/manifest.json +++ b/homeassistant/components/redgtech/manifest.json @@ -1,7 +1,6 @@ { "domain": "redgtech", "name": "Redgtech", - "version": "1.0.0", "codeowners": [], "documentation": "https://www.home-assistant.io/integrations/redgtech", "iot_class": "cloud_polling", @@ -9,5 +8,5 @@ "integration_type": "service", "config_flow": true, "quality_scale": "bronze", - "requirements": [] + "requirements": ["redgtech-api==0.1.0"] } \ No newline at end of file diff --git a/homeassistant/components/redgtech/switch.py b/homeassistant/components/redgtech/switch.py index 59524932ab3..59abd471908 100644 --- a/homeassistant/components/redgtech/switch.py +++ b/homeassistant/components/redgtech/switch.py @@ -1,43 +1,38 @@ import logging -import aiohttp from homeassistant.components.switch import SwitchEntity +from redgtech_api import RedgtechAPI from .const import DOMAIN, API_URL _LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass, config_entry, async_add_entities): - """Set up the light platform.""" + """Set up the switch platform.""" access_token = config_entry.data.get("access_token") if access_token: + api = RedgtechAPI(access_token) try: - async with aiohttp.ClientSession() as session: - async with session.get(f'{API_URL}/home_assistant?access_token={access_token}') as response: - if response.status == 200: - data = await response.json() - entities = [] - for item in data.get("boards", []): - categories = item.get("displayCategories", "") - if "SWITCH" in categories: - - entities.append(RedgtechSwitch(item, access_token)) - async_add_entities(entities) - else: - _LOGGER.error("Error fetching data from API: %s", response.status) - except aiohttp.ClientError as e: - _LOGGER.error("Error connecting to API: %s", e) + data = await api.get_data() + entities = [] + for item in data.get("boards", []): + categories = item.get("displayCategories", "") + if "SWITCH" in categories: + entities.append(RedgtechSwitch(item, api)) + async_add_entities(entities) + except Exception as e: + _LOGGER.error("Error fetching data from API: %s", e) else: _LOGGER.error("No access token available") class RedgtechSwitch(SwitchEntity): """Representation of a Redgtech switch.""" - def __init__(self, data, token): + def __init__(self, data, api): self._state = data.get("value", False) self._name = data.get("friendlyName") self._endpoint_id = data.get("endpointId") self._description = data.get("description") self._manufacturer = data.get("manufacturerName") - self._token = token + self._api = api @property def name(self): @@ -59,45 +54,18 @@ class RedgtechSwitch(SwitchEntity): async def _set_state(self, state): """Send the state to the API to update the switch.""" - id_part, after_id = self._endpoint_id.split("-", 1) - value = ''.join(filter(str.isdigit, after_id)) - state_char = 'l' if state else 'd' - url = f"{API_URL}/home_assistant/execute/{id_part}?cod=?{value}{state_char}" - headers = {"Authorization": f"{self._token}"} - payload = {"state": state} + success = await self._api.set_switch_state(self._endpoint_id, state) + if success: + self._state = state + self.async_write_ha_state() + else: + _LOGGER.error("Failed to set state for %s", self._name) - async with aiohttp.ClientSession() as session: - async with session.get(url, headers=headers, json=payload) as response: - if response.status == 200: - self._state = state - self.async_write_ha_state() - else: - _LOGGER.error("Failed to set state for %s, status code: %s", self._name, response.status) - - async def async_update(self): - """Get the latest state of the switch.""" - id_part, after_id = self._endpoint_id.split("-", 1) - value = after_id - url = f"{API_URL}/home_assistant?access_token={self._token}" - headers = {"Authorization": f"{self._token}"} - - try: - async with aiohttp.ClientSession() as session: - async with session.get(url, headers=headers) as response: - if response.status == 200: - data = await response.json() - - for board in data.get("boards", []): - if board.get("endpointId") == self._endpoint_id: - value = board.get("value", False) - self._state = bool(value) - self.async_write_ha_state() - break - else: - _LOGGER.error( - "Failed to update state for %s, status code: %s", - self._name, - response.status, - ) - except Exception as e: - _LOGGER.error("Error updating state for %s: %s", self._name, str(e)) \ No newline at end of file + @property + def extra_state_attributes(self): + """Return the state attributes.""" + return { + "endpoint_id": self._endpoint_id, + "description": self._description, + "manufacturer": self._manufacturer, + } \ No newline at end of file diff --git a/requirements_all.txt b/requirements_all.txt index e7e1b767fe4..4b6528ad0ea 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -266,6 +266,9 @@ aiohasupervisor==0.2.2b5 # homeassistant.components.homekit_controller aiohomekit==3.2.7 +# homeassistant.components.redgtech +aiohttp==3.8.1 + # homeassistant.components.mcp_server aiohttp_sse==2.2.0 @@ -2584,6 +2587,9 @@ rapt-ble==0.1.2 # homeassistant.components.raspyrfm raspyrfm-client==1.2.8 +# homeassistant.components.redgtech +redgtech-api==0.1.0 + # homeassistant.components.refoss refoss-ha==1.2.5