Skip to content

Commit d79da44

Browse files
committed
Add button to let the user refresh the daikin onecta data manually from the cloud
* custom_components/daikin_onecta/button.py: Added. * custom_components/daikin_onecta/__init__.py: * tests/test_init.py:
1 parent 60b29bc commit d79da44

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

custom_components/daikin_onecta/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
SIGNAL_DELETE_ENTITY = "daikin_delete"
2626
SIGNAL_UPDATE_ENTITY = "daikin_update"
2727

28-
COMPONENT_TYPES = ["climate", "sensor", "water_heater", "switch", "select", "binary_sensor"]
28+
COMPONENT_TYPES = ["climate", "sensor", "water_heater", "switch", "select", "binary_sensor", "button"]
2929

3030

3131
async def async_setup(hass, config):
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Component to interface with binary sensors."""
2+
from __future__ import annotations
3+
4+
import logging
5+
6+
from homeassistant.components.button import ButtonEntity
7+
from homeassistant.config_entries import ConfigEntry
8+
from homeassistant.core import callback
9+
from homeassistant.core import HomeAssistant
10+
from homeassistant.helpers.entity import EntityCategory
11+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
12+
from homeassistant.helpers.update_coordinator import CoordinatorEntity
13+
14+
from .const import COORDINATOR
15+
from .const import DAIKIN_DEVICES
16+
from .const import DOMAIN as DAIKIN_DOMAIN
17+
18+
_LOGGER = logging.getLogger(__name__)
19+
20+
21+
async def async_setup_entry(
22+
hass: HomeAssistant,
23+
config_entry: ConfigEntry,
24+
async_add_entities: AddEntitiesCallback,
25+
) -> None:
26+
entities = []
27+
28+
coordinator = hass.data[DAIKIN_DOMAIN][COORDINATOR]
29+
30+
for dev_id, device in hass.data[DAIKIN_DOMAIN][DAIKIN_DEVICES].items():
31+
entities.append(DaikinRefreshButton(device, config_entry, coordinator))
32+
33+
if entities:
34+
async_add_entities(entities)
35+
36+
37+
class DaikinRefreshButton(CoordinatorEntity, ButtonEntity):
38+
"""Button to request an immediate device data update."""
39+
40+
def __init__(self, device, config_entry, coordinator):
41+
super().__init__(coordinator)
42+
self._device = device
43+
self._attr_unique_id = f"{self._device.id}_refresh"
44+
self._attr_entity_category = EntityCategory.CONFIG
45+
self._attr_icon = "mdi:refresh"
46+
self._attr_name = "Refresh"
47+
self._attr_device_info = self._device.device_info()
48+
self._attr_has_entity_name = True
49+
self._config_entry = config_entry
50+
51+
_LOGGER.info("Device '%s' has refresh button", self._device.name)
52+
53+
@property
54+
def available(self) -> bool:
55+
return self._device.available
56+
57+
@callback
58+
def _handle_coordinator_update(self) -> None:
59+
self.async_write_ha_state()
60+
61+
async def async_press(self) -> None:
62+
await self.coordinator._async_update_data()
63+
self.coordinator.async_update_listeners()

tests/test_init.py

+28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import homeassistant.helpers.device_registry as dr
88
import homeassistant.helpers.entity_registry as er
99
import responses
10+
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
11+
from homeassistant.components.button import SERVICE_PRESS
1012
from homeassistant.components.climate import ATTR_FAN_MODE
1113
from homeassistant.components.climate import ATTR_HVAC_MODE
1214
from homeassistant.components.climate import ATTR_PRESET_MODE
@@ -1228,3 +1230,29 @@ async def test_gas(
12281230
await snapshot_platform_entities(hass, config_entry, Platform.SENSOR, entity_registry, snapshot, "gas")
12291231

12301232
assert hass.states.get("climate.my_living_room_room_temperature").attributes["temperature"] == 25
1233+
1234+
1235+
async def test_button(
1236+
hass: HomeAssistant,
1237+
config_entry: MockConfigEntry,
1238+
onecta_auth: AsyncMock,
1239+
snapshot: SnapshotAssertion,
1240+
entity_registry: er.EntityRegistry,
1241+
) -> None:
1242+
"""Test entities."""
1243+
await snapshot_platform_entities(hass, config_entry, Platform.SENSOR, entity_registry, snapshot, "dry")
1244+
1245+
with patch(
1246+
"custom_components.daikin_onecta.DaikinApi.async_get_access_token",
1247+
return_value="XXXXXX",
1248+
):
1249+
# Call button service
1250+
await hass.services.async_call(
1251+
BUTTON_DOMAIN,
1252+
SERVICE_PRESS,
1253+
{ATTR_ENTITY_ID: "button.altherma_refresh"},
1254+
blocking=True,
1255+
)
1256+
await hass.async_block_till_done()
1257+
1258+
assert len(responses.calls) == 0

0 commit comments

Comments
 (0)