-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #374 from WillCodeForCats/button-refresh
Add refresh button entity
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |