-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added sensor for setting home pro text (1.5 hours dev)
- Loading branch information
1 parent
90e1045
commit 9c40dc5
Showing
12 changed files
with
209 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Home Pro | ||
|
||
To support the Home Pro device. Once configured, the following entities will retrieve data locally from your Octopus Home Pro instead of via the Octopus Energy APIs at a target rate of every 10 seconds. | ||
|
||
* [Electricity - Current Demand](./electricity.md#current-demand) | ||
* [Electricity - Current Total Consumption](./electricity.md#current-total-consumption) | ||
* [Gas - Current Total Consumption kWh](./gas.md#current-total-consumption-kwh) | ||
* [Gas - Current Total Consumption m3](./gas.md#current-total-consumption-m3) | ||
|
||
## Home Pro Screen | ||
|
||
`text.octopus_energy_{{ACCOUNT_ID}}_home_pro_screen` | ||
|
||
Allows you to set scrolling text for the home pro device. |
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
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
Empty file.
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,63 @@ | ||
import logging | ||
|
||
from homeassistant.core import HomeAssistant | ||
|
||
from homeassistant.components.text import TextEntity | ||
|
||
from homeassistant.helpers.restore_state import RestoreEntity | ||
|
||
from homeassistant.helpers.entity import generate_entity_id | ||
|
||
from ..api_client_home_pro import OctopusEnergyHomeProApiClient | ||
|
||
from ..utils.attributes import dict_to_typed_dict | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
class OctopusEnergyHomeProScreenText(TextEntity, RestoreEntity): | ||
"""Sensor for determining the text on the home pro""" | ||
|
||
def __init__(self, hass: HomeAssistant, account_id: str, client: OctopusEnergyHomeProApiClient): | ||
"""Init sensor.""" | ||
self._hass = hass | ||
self._client = client | ||
self._account_id = account_id | ||
self._attr_native_value = None | ||
|
||
self.entity_id = generate_entity_id("text.{}", self.unique_id, hass=hass) | ||
|
||
@property | ||
def unique_id(self): | ||
"""The id of the sensor.""" | ||
return f"octopus_energy_{self._account_id}_home_pro_screen" | ||
|
||
@property | ||
def name(self): | ||
"""Name of the sensor.""" | ||
return f"Home Pro Screen ({self._account_id})" | ||
|
||
@property | ||
def icon(self): | ||
"""Icon of the sensor.""" | ||
return "mdi:led-strip" | ||
|
||
async def async_set_value(self, value: str) -> None: | ||
"""Update the value.""" | ||
self._attr_native_value = value | ||
await self._client.async_set_screen(self._attr_native_value, "scroll", "text", 200, 100) | ||
self.async_write_ha_state() | ||
|
||
async def async_added_to_hass(self): | ||
"""Call when entity about to be added to hass.""" | ||
# If not None, we got an initial value. | ||
await super().async_added_to_hass() | ||
state = await self.async_get_last_state() | ||
|
||
if state is not None: | ||
if state.state is not None: | ||
self._attr_native_value = state.state | ||
self._attr_state = state.state | ||
|
||
self._attributes = dict_to_typed_dict(state.attributes) | ||
|
||
_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeElectricityCostTariffOverride state: {self._attr_state}') |
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,37 @@ | ||
import logging | ||
|
||
from homeassistant.core import HomeAssistant | ||
|
||
from .home_pro.screen_text import OctopusEnergyHomeProScreenText | ||
|
||
from .const import ( | ||
CONFIG_ACCOUNT_ID, | ||
DATA_HOME_PRO_CLIENT, | ||
DOMAIN, | ||
|
||
CONFIG_MAIN_API_KEY | ||
) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
async def async_setup_entry(hass, entry, async_add_entities): | ||
"""Setup sensors based on our entry""" | ||
config = dict(entry.data) | ||
|
||
if entry.options: | ||
config.update(entry.options) | ||
|
||
if CONFIG_MAIN_API_KEY in config: | ||
await async_setup_default_sensors(hass, config, async_add_entities) | ||
|
||
async def async_setup_default_sensors(hass: HomeAssistant, config, async_add_entities): | ||
account_id = config[CONFIG_ACCOUNT_ID] | ||
|
||
home_pro_client = hass.data[DOMAIN][account_id][DATA_HOME_PRO_CLIENT] if DATA_HOME_PRO_CLIENT in hass.data[DOMAIN][account_id] else None | ||
|
||
entities = [] | ||
|
||
if home_pro_client is not None: | ||
entities.append(OctopusEnergyHomeProScreenText(hass, account_id, home_pro_client)) | ||
|
||
async_add_entities(entities) |
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
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
32 changes: 32 additions & 0 deletions
32
tests/local_integration/api_client_home_pro/test_async_set_screen.py
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,32 @@ | ||
import pytest | ||
|
||
from .. import get_test_context | ||
from custom_components.octopus_energy.api_client import AuthenticationException | ||
from custom_components.octopus_energy.api_client_home_pro import OctopusEnergyHomeProApiClient | ||
|
||
@pytest.mark.asyncio | ||
async def test_when_set_screen_is_called_and_api_key_is_invalid_then_exception_is_raised(): | ||
# Arrange | ||
context = get_test_context() | ||
|
||
client = OctopusEnergyHomeProApiClient(context.base_url, "invalid-api-key") | ||
|
||
# Act | ||
exception_raised = False | ||
try: | ||
await client.async_set_screen("hello world", "scroll", "text", 200, 100) | ||
except AuthenticationException: | ||
exception_raised = True | ||
|
||
# Assert | ||
assert exception_raised == True | ||
|
||
@pytest.mark.asyncio | ||
async def test_when_set_screen_is_called_then_successful(): | ||
# Arrange | ||
context = get_test_context() | ||
|
||
client = OctopusEnergyHomeProApiClient(context.base_url, context.api_key) | ||
|
||
# Act | ||
await client.async_set_screen("hello world", "scroll", "text", 200, 100) |