Skip to content

Commit acd3dcc

Browse files
authored
Merge pull request #1 from Refoss/fix/poll
Add poll
2 parents 767d718 + 5078f7c commit acd3dcc

File tree

8 files changed

+49
-52
lines changed

8 files changed

+49
-52
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# refoss_rpc
2-
Only support R11
2+
- Home Assistant version: 2025.2.5 or above
3+
4+
## Installation
5+
6+
### Custom Repositories in HACS (recommended)
7+
- Make sure the [HACS integration](https://hacs.xyz/) is properly installed for your instance of home assistant.
8+
- Reference [Custom Repositories](https://hacs.xyz/docs/faq/custom_repositories),In the HACS UI go to "Integrations", click on "+" in the lower right corner".
9+
- Paste https://github.com/Refoss/refoss_rpc into the field that says "Add custom repository URL", select "Integration" from "Category" dropdown and click "Add".
10+
- You should now see a card with the Refoss RPC integration in the HACS -> "Integrations" section. Click "Install".
11+
- Select the latest version from the dropdown and click "Install".
12+
- Restart Home Assistant.
13+
14+
### Manual installation
15+
- Using the tool of choice open the directory for your HA configuration (where you find configuration.yaml).
16+
- If you do not have a custom_components directory there, you need to create it.
17+
- In releases(https://github.com/Refoss/refoss_rpc/releases), download the version you need.
18+
- In the downloaded file, locate the refoss_rpc directory and copy it to the custom_components directory.
19+
- Restart Home Assistant.
20+
21+
## Configuration
22+
- In the HA UI go to "Configuration" -> "Integrations", click "+", search for "Refoss RPC", and select the "Refoss RPC" integration from the list.
23+
Or click here: [![Start Config Flow](https://my.home-assistant.io/badges/config_flow_start.svg)](https://my.home-assistant.io/redirect/config_flow_start?domain=refoss_rpc)
24+
25+
## Supported device models
26+
27+
| Model | Version |
28+
|-------------------------------------|--------------------|
29+
| `Refoss Smart Wi-Fi Switch, R11` | `all` |

custom_components/refoss_rpc/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
RefossConfigEntry,
3232
RefossCoordinator,
3333
RefossEntryData,
34-
RefossPollingCoordinator,
3534
)
3635

3736
PLATFORMS: Final = [
@@ -73,7 +72,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: RefossConfigEntry) -> bo
7372

7473
runtime_data.coordinator = RefossCoordinator(hass, entry, device)
7574
runtime_data.coordinator.async_setup()
76-
runtime_data.poll_coordinator = RefossPollingCoordinator(hass, entry, device)
7775
await hass.config_entries.async_forward_entry_setups(entry, runtime_data.platforms)
7876

7977
return True

custom_components/refoss_rpc/const.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99

1010
LOGGER: Logger = getLogger(__package__)
1111

12-
13-
# Refresh interval for polling
14-
REFOSS_SENSORS_POLLING_INTERVAL: Final = 60
15-
16-
# Reconnect interval for devices
17-
REFOSS_RECONNECT_INTERVAL = 60
12+
#Check interval for devices
13+
REFOSS_CHECK_INTERVAL = 60
1814

1915

2016
# Button Click events for devices
@@ -41,7 +37,7 @@
4137
UPTIME_DEVIATION: Final = 5
4238

4339
# Time to wait before reloading entry when device config change
44-
ENTRY_RELOAD_COOLDOWN = 60
40+
ENTRY_RELOAD_COOLDOWN = 30
4541

4642

4743
OTA_BEGIN = "ota_begin"

custom_components/refoss_rpc/coordinator.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
OTA_ERROR,
4343
OTA_PROGRESS,
4444
OTA_SUCCESS,
45-
REFOSS_RECONNECT_INTERVAL,
46-
REFOSS_SENSORS_POLLING_INTERVAL,
45+
REFOSS_CHECK_INTERVAL,
4746
)
4847
from .utils import get_host, update_device_fw_info
4948

@@ -54,7 +53,6 @@ class RefossEntryData:
5453

5554
platforms: list[Platform]
5655
coordinator: RefossCoordinator | None = None
57-
poll_coordinator: RefossPollingCoordinator | None = None
5856

5957

6058
RefossConfigEntry = ConfigEntry[RefossEntryData]
@@ -174,7 +172,7 @@ def __init__(
174172
) -> None:
175173
"""Initialize the Refoss coordinator."""
176174
self.entry = entry
177-
super().__init__(hass, entry, device, REFOSS_RECONNECT_INTERVAL)
175+
super().__init__(hass, entry, device, REFOSS_CHECK_INTERVAL)
178176

179177
self.connected = False
180178
self._connection_lock = asyncio.Lock()
@@ -255,11 +253,22 @@ async def _async_update_data(self) -> None:
255253
"""Fetch data."""
256254
if self.hass.is_stopping:
257255
return
256+
258257
async with self._connection_lock:
259-
if self.device.connected: # Already connected
258+
if not self.device.connected:
259+
if not await self._async_device_connect_task():
260+
raise UpdateFailed("Device reconnect error")
260261
return
261-
if not await self._async_device_connect_task():
262-
raise UpdateFailed("Device reconnect error")
262+
try:
263+
LOGGER.debug("Polling Refoss Device - %s", self.name)
264+
await self.device.poll()
265+
except DeviceConnectionError as err:
266+
raise UpdateFailed(f"Device disconnected: {err!r}") from err
267+
except RpcCallError as err:
268+
raise UpdateFailed(f"RPC call failed: {err!r}") from err
269+
except InvalidAuthError:
270+
await self.async_shutdown_device_and_start_reauth()
271+
return
263272

264273
async def _async_disconnected(self, reconnect: bool) -> None:
265274
"""Handle device disconnected."""
@@ -346,29 +355,6 @@ async def shutdown(self) -> None:
346355
await self._async_disconnected(False)
347356

348357

349-
class RefossPollingCoordinator(RefossCoordinatorBase):
350-
"""Polling coordinator for a Refoss device."""
351-
352-
def __init__(
353-
self, hass: HomeAssistant, entry: RefossConfigEntry, device: RpcDevice
354-
) -> None:
355-
"""Initialize the polling coordinator."""
356-
super().__init__(hass, entry, device, REFOSS_SENSORS_POLLING_INTERVAL)
357-
358-
async def _async_update_data(self) -> None:
359-
"""Fetch data."""
360-
if not self.device.connected:
361-
raise UpdateFailed("Device disconnected")
362-
363-
LOGGER.debug("Polling Refoss Device - %s", self.name)
364-
try:
365-
await self.device.poll()
366-
except (DeviceConnectionError, RpcCallError) as err:
367-
raise UpdateFailed(f"Device disconnected: {err!r}") from err
368-
except InvalidAuthError:
369-
await self.async_shutdown_device_and_start_reauth()
370-
371-
372358
def get_refoss_coordinator_by_device_id(
373359
hass: HomeAssistant, device_id: str
374360
) -> RefossCoordinator | None:

custom_components/refoss_rpc/entity.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ def async_setup_entry_refoss(
3939
if not coordinator.device.initialized:
4040
return
4141

42-
polling_coordinator = config_entry.runtime_data.poll_coordinator
43-
4442
entities = []
4543
for sensor_id in sensors:
4644
description = sensors[sensor_id]
@@ -63,10 +61,6 @@ def async_setup_entry_refoss(
6361
domain = sensor_class.__module__.split(".")[-1]
6462
unique_id = f"{coordinator.mac}-{key}-{sensor_id}"
6563
async_remove_refoss_entity(hass, domain, unique_id)
66-
elif description.use_polling_coordinator:
67-
entities.append(
68-
sensor_class(polling_coordinator, key, sensor_id, description)
69-
)
7064
else:
7165
entities.append(sensor_class(coordinator, key, sensor_id, description))
7266
if not entities:
@@ -84,7 +78,6 @@ class RefossEntityDescription(EntityDescription):
8478

8579
value: Callable[[Any, Any], Any] | None = None
8680
removal_condition: Callable[[dict, dict, str], bool] | None = None
87-
use_polling_coordinator: bool = False
8881
supported: Callable = lambda _: False
8982

9083

custom_components/refoss_rpc/sensor.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ class RefossSensorDescription(RefossEntityDescription, SensorEntityDescription):
9393
state_class=SensorStateClass.MEASUREMENT,
9494
entity_category=EntityCategory.DIAGNOSTIC,
9595
entity_registry_enabled_default=False,
96-
use_polling_coordinator=True,
9796
),
9897
"rssi": RefossSensorDescription(
9998
key="wifi",
@@ -105,7 +104,6 @@ class RefossSensorDescription(RefossEntityDescription, SensorEntityDescription):
105104
removal_condition=is_refoss_wifi_stations_disabled,
106105
entity_category=EntityCategory.DIAGNOSTIC,
107106
entity_registry_enabled_default=False,
108-
use_polling_coordinator=True,
109107
),
110108
"uptime": RefossSensorDescription(
111109
key="sys",
@@ -115,7 +113,6 @@ class RefossSensorDescription(RefossEntityDescription, SensorEntityDescription):
115113
device_class=SensorDeviceClass.TIMESTAMP,
116114
entity_category=EntityCategory.DIAGNOSTIC,
117115
entity_registry_enabled_default=False,
118-
use_polling_coordinator=True,
119116
),
120117
}
121118

custom_components/refoss_rpc/strings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"flow_title": "{name}",
44
"step": {
55
"user": {
6-
"description": "Before setup, devices must be connected to the network.\n\nThis path can be configured for refoss product models including R11, etc. \n\nFor more information, please refer to 'Help'.",
6+
"description": "Before setup, devices must be connected to the network.\n\nThis path can be configured for Refoss product models including R11, etc. \n\nFor more information, please refer to 'Help'.",
77
"data": {
88
"host": "[%key:common::config_flow::data::host%]"
99
},

custom_components/refoss_rpc/translations/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"data_description": {
5252
"host": "The hostname or IP address of the Refoss device to connect to."
5353
},
54-
"description": "Before setup, devices must be connected to the network.\n\nThis path can be configured for refoss product models including R11, etc. \n\nFor more information, please refer to 'Help'."
54+
"description": "Before setup, devices must be connected to the network.\n\nThis path can be configured for Refoss product models including R11, etc. \n\nFor more information, please refer to 'Help'."
5555
}
5656
}
5757
},

0 commit comments

Comments
 (0)