Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refresh button entity #374

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions custom_components/solaredge_modbus_multi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

PLATFORMS: list[str] = [
Platform.BINARY_SENSOR,
Platform.BUTTON,
Platform.NUMBER,
Platform.SELECT,
Platform.SENSOR,
Expand Down
82 changes: 82 additions & 0 deletions custom_components/solaredge_modbus_multi/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""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 HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
hub = hass.data[DOMAIN][config_entry.entry_id]["hub"]
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]

entities = []

for inverter in hub.inverters:
entities.append(SolarEdgeRefreshButton(inverter, config_entry, coordinator))

if entities:
async_add_entities(entities)


class SolarEdgeButtonBase(CoordinatorEntity, ButtonEntity):
_attr_has_entity_name = True

def __init__(self, platform, config_entry, coordinator):
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator)
"""Initialize the sensor."""
self._platform = platform
self._config_entry = config_entry

@property
def device_info(self):
return self._platform.device_info

@property
def config_entry_id(self):
return self._config_entry.entry_id

@property
def config_entry_name(self):
return self._config_entry.data["name"]

@property
def available(self) -> bool:
return super().available and self._platform.online

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


class SolarEdgeRefreshButton(SolarEdgeButtonBase):
entity_category = EntityCategory.CONFIG

def __init__(self, platform, config_entry, coordinator):
super().__init__(platform, config_entry, coordinator)
"""Initialize the sensor."""

@property
def unique_id(self) -> str:
return f"{self._platform.uid_base}_refresh"

@property
def name(self) -> str:
return "Refresh"

async def async_press(self) -> None:
await self.async_update()