Skip to content

Commit

Permalink
Merge pull request #372 from jwillemsen/jwi-refreshbutton
Browse files Browse the repository at this point in the history
Add button to let the user refresh the daikin onecta data manually fr…
  • Loading branch information
jwillemsen authored Dec 10, 2024
2 parents 60b29bc + 551932b commit 6e5665c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion custom_components/daikin_onecta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
SIGNAL_DELETE_ENTITY = "daikin_delete"
SIGNAL_UPDATE_ENTITY = "daikin_update"

COMPONENT_TYPES = ["climate", "sensor", "water_heater", "switch", "select", "binary_sensor"]
COMPONENT_TYPES = ["climate", "sensor", "water_heater", "switch", "select", "binary_sensor", "button"]


async def async_setup(hass, config):
Expand Down
63 changes: 63 additions & 0 deletions custom_components/daikin_onecta/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Component to interface with binary sensors."""
from __future__ import annotations

import logging

from homeassistant.components.button import ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import COORDINATOR
from .const import DAIKIN_DEVICES
from .const import DOMAIN as DAIKIN_DOMAIN

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
entities = []

coordinator = hass.data[DAIKIN_DOMAIN][COORDINATOR]

for dev_id, device in hass.data[DAIKIN_DOMAIN][DAIKIN_DEVICES].items():
entities.append(DaikinRefreshButton(device, config_entry, coordinator))

if entities:
async_add_entities(entities)


class DaikinRefreshButton(CoordinatorEntity, ButtonEntity):
"""Button to request an immediate device data update."""

def __init__(self, device, config_entry, coordinator):
super().__init__(coordinator)
self._device = device
self._attr_unique_id = f"{self._device.id}_refresh"
self._attr_entity_category = EntityCategory.CONFIG
self._attr_icon = "mdi:refresh"
self._attr_name = "Refresh"
self._attr_device_info = self._device.device_info()
self._attr_has_entity_name = True
self._config_entry = config_entry

_LOGGER.info("Device '%s' has refresh button", self._device.name)

@property
def available(self) -> bool:
return self._device.available

@callback
def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()

async def async_press(self) -> None:
await self.coordinator._async_update_data()
self.coordinator.async_update_listeners()
31 changes: 31 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import homeassistant.helpers.device_registry as dr
import homeassistant.helpers.entity_registry as er
import responses
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
from homeassistant.components.button import SERVICE_PRESS
from homeassistant.components.climate import ATTR_FAN_MODE
from homeassistant.components.climate import ATTR_HVAC_MODE
from homeassistant.components.climate import ATTR_PRESET_MODE
Expand Down Expand Up @@ -1228,3 +1230,32 @@ async def test_gas(
await snapshot_platform_entities(hass, config_entry, Platform.SENSOR, entity_registry, snapshot, "gas")

assert hass.states.get("climate.my_living_room_room_temperature").attributes["temperature"] == 25


@responses.activate
async def test_button(
hass: HomeAssistant,
config_entry: MockConfigEntry,
onecta_auth: AsyncMock,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
) -> None:
"""Test entities."""
await snapshot_platform_entities(hass, config_entry, Platform.SENSOR, entity_registry, snapshot, "dry")

with patch(
"custom_components.daikin_onecta.DaikinApi.async_get_access_token",
return_value="XXXXXX",
):
responses.get(DAIKIN_API_URL + "/v1/gateway-devices", status=200, json=load_fixture_json("dry"))

# Call button service
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.lounge_refresh"},
blocking=True,
)
await hass.async_block_till_done()

assert len(responses.calls) == 1

0 comments on commit 6e5665c

Please sign in to comment.