Skip to content

Commit

Permalink
add entities for tempo and hchp
Browse files Browse the repository at this point in the history
  • Loading branch information
Aohzan committed May 29, 2024
1 parent 1dcd50a commit cbead9b
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 372 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 5.1.0

- Add binary sensor in Tempo and HCHP modes to know if currently in HC or not
- Add sensor in Tempo mode to know today and tomorrow colors
- Teleinfo instant power is now in VA instead of W
- Remove warning message for HCHP and Tempo total entity when equal to 0

## 5.0.0

- Add Tempo support
Expand Down
1 change: 1 addition & 0 deletions custom_components/ecodevices/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""GCE Eco-Devices integration."""

import asyncio
from datetime import timedelta
import logging
Expand Down
107 changes: 107 additions & 0 deletions custom_components/ecodevices/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""Support for the GCE Eco-Devices binary sensors."""

import logging

from pyecodevices import EcoDevices

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.util import slugify

from .const import (
CONF_T1_ENABLED,
CONF_T1_TYPE,
CONF_T2_ENABLED,
CONF_T2_TYPE,
CONF_TI_TYPE_HCHP,
CONF_TI_TYPE_TEMPO,
CONTROLLER,
COORDINATOR,
DEFAULT_T1_NAME,
DEFAULT_T2_NAME,
DOMAIN,
)
from .entity import get_device_info

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the GCE Eco-Devices platform."""
data = hass.data[DOMAIN][entry.entry_id]
controller = data[CONTROLLER]
coordinator = data[COORDINATOR]
config = entry.data
options = entry.options

t1_enabled = options.get(CONF_T1_ENABLED, config.get(CONF_T1_ENABLED))
t1_type = options.get(CONF_T1_TYPE, config.get(CONF_T1_TYPE))
t2_enabled = options.get(CONF_T2_ENABLED, config.get(CONF_T2_ENABLED))
t2_type = options.get(CONF_T2_TYPE, config.get(CONF_T2_TYPE))

entities: list[BinarySensorEntity] = []

for input_number in (1, 2):
if t1_enabled and input_number == 1 or t2_enabled and input_number == 2:
_LOGGER.debug("Add the teleinfo %s binary_sensor entities", input_number)
if t1_type in (CONF_TI_TYPE_HCHP, CONF_TI_TYPE_TEMPO) or t2_type in (
CONF_TI_TYPE_HCHP,
CONF_TI_TYPE_TEMPO,
):
prefix_name = DEFAULT_T1_NAME if input_number == 1 else DEFAULT_T2_NAME
entities.append(
TeleinfoInputHeuresCreuses(
controller,
coordinator,
input_number=input_number,
input_name="PTEC",
name=f"{prefix_name} Heures Creuses",
)
)

if entities:
async_add_entities(entities)


class TeleinfoInputHeuresCreuses(CoordinatorEntity, BinarySensorEntity):
"""Representation of a Eco-Devices HC binary sensor."""

_attr_icon = "mdi:cash-clock"

def __init__(
self,
controller: EcoDevices,
coordinator: DataUpdateCoordinator,
input_number: int,
input_name: str,
name: str,
) -> None:
"""Initialize the binary sensor."""
super().__init__(coordinator)
self.controller = controller
self._input_number = input_number
self._input_name = input_name
self._attr_name = name
self._attr_unique_id = slugify(
f"{DOMAIN}_{self.controller.mac_address}_binary_sensor_{self._input_number}_{self._input_name}"
)
self._attr_device_info = get_device_info(self.controller)

@property
def is_on(self) -> bool | None:
"""Return the state."""
if type_heure := self.coordinator.data.get(
f"T{self._input_number}_{self._input_name}"
):
if type_heure.startswith("HC"):
return True
return False
return None
18 changes: 12 additions & 6 deletions custom_components/ecodevices/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Config flow to configure the GCE Eco-Devices integration."""

from typing import Any

from pyecodevices import (
Expand All @@ -9,7 +10,13 @@
import voluptuous as vol

from homeassistant.components.sensor import DEVICE_CLASSES as SENSOR_DEVICE_CLASSES
from homeassistant.config_entries import HANDLERS, ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.config_entries import (
HANDLERS,
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.const import (
CONF_HOST,
CONF_PASSWORD,
Expand All @@ -18,7 +25,6 @@
CONF_USERNAME,
)
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .const import (
Expand Down Expand Up @@ -67,7 +73,7 @@ def __init__(self) -> None:
"""Initialize class variables."""
self.base_input: dict[str, Any] = {}

async def async_step_user(self, user_input=None) -> FlowResult:
async def async_step_user(self, user_input=None) -> ConfigFlowResult:
"""Handle a flow initialized by the user."""
errors: dict[str, str] = {}
if user_input is None:
Expand All @@ -94,7 +100,7 @@ async def async_step_user(self, user_input=None) -> FlowResult:
self.base_input = user_input
return await self.async_step_params()

async def async_step_params(self, user_input=None) -> FlowResult:
async def async_step_params(self, user_input=None) -> ConfigFlowResult:
"""Handle the param flow to customize the device accordly to enabled inputs."""
if user_input is not None:
user_input.update(self.base_input)
Expand Down Expand Up @@ -123,7 +129,7 @@ def __init__(self, config_entry: ConfigEntry) -> None:
self.config_entry: ConfigEntry = config_entry
self.base_input: dict[str, Any] = {}

async def async_step_init(self, user_input) -> FlowResult:
async def async_step_init(self, user_input) -> ConfigFlowResult:
"""Manage the options."""
errors = {}
if user_input is not None:
Expand Down Expand Up @@ -160,7 +166,7 @@ async def async_step_init(self, user_input) -> FlowResult:
step_id="init", data_schema=vol.Schema(options_schema), errors=errors
)

async def async_step_params(self, user_input=None) -> FlowResult:
async def async_step_params(self, user_input=None) -> ConfigFlowResult:
"""Handle the param flow to customize the device accordly to enabled inputs."""
if user_input is not None:
user_input.update(self.base_input)
Expand Down
7 changes: 4 additions & 3 deletions custom_components/ecodevices/const.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Constant for the eco-devices integration."""

DOMAIN = "ecodevices"

CONTROLLER = "controller"
COORDINATOR = "coordinator"
PLATFORMS = ["sensor"]
PLATFORMS = ["binary_sensor", "sensor"]
UNDO_UPDATE_LISTENER = "undo_update_listener"

CONF_T1_ENABLED = "t1_enabled"
Expand Down Expand Up @@ -76,6 +77,6 @@
"Jour Bleu HP": "BBRHPJB",
"Jour Blanc HC": "BBRHCJW",
"Jour Blanc HP": "BBRHPJW",
"Jour Rouge HC": "BBRHCJR",
"Jour Rouge HP": "BBRHPJR",
"Jour Ro" + "uge HC": "BBRHCJR", # bypass codespell
"Jour Ro" + "uge HP": "BBRHPJR", # bypass codespell
}
20 changes: 20 additions & 0 deletions custom_components/ecodevices/entity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Generic entity for EcoDevices."""

from pyecodevices import EcoDevices

from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo

from .const import DOMAIN


def get_device_info(controller: EcoDevices) -> DeviceInfo:
"""Get device info."""
return DeviceInfo(
identifiers={(DOMAIN, controller.mac_address)},
manufacturer="GCE Electronics",
model="Eco-Devices",
name=f"Eco-Devices {controller.host}:{controller.port!s}",
sw_version=controller.version,
connections={(CONNECTION_NETWORK_MAC, controller.mac_address)},
configuration_url=f"http://{controller.host}:{controller.port}",
)
2 changes: 1 addition & 1 deletion custom_components/ecodevices/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"xmltodict==0.13.0",
"pyecodevices==1.6.1"
],
"version": "4.7.0"
"version": "5.1.0"
}
Loading

0 comments on commit cbead9b

Please sign in to comment.