Skip to content

Commit

Permalink
fix default config for renewable-energy sensor;
Browse files Browse the repository at this point in the history
updated README;
  • Loading branch information
thematrixdev committed Jul 5, 2024
1 parent 94c4462 commit ff97d66
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
61 changes: 35 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# CLP (HK) Statistic Home-Assistant Custom-Component
# CLP (HK) Statistic Home-Assistant Custom-Component

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png)](https://buymeacoffee.com/thematrixdev)

## Prerequisite

- CLP Subscriber
- CLP smart meter installed
- CLP website credentials

## Install

1. Setup `HACS` https://hacs.xyz/docs/setup/prerequisites
2. In `Home Assistant`, click `HACS` on the menu on the left
3. Select `integrations`
Expand All @@ -19,45 +21,50 @@
9. Restart Home-Assistant ***(You have to restart before proceeding)***

## Configure in Home-Assistant
1. Add these in `configuration.yaml`

### Minimal configuration in `configuration.yaml`

```
sensor:
- platform: clp
name: 'CLP' # whatever name you like
username: '' # CLP web site username
password: '' # CLP web site password
timeout: 30 # connection timeout in second
type: '' # type of data to be shown in state
get_account: false # get account summary
get_bill: false # get bills
get_estimation: false # get usage estimation
get_daily: false # get daily usage
get_hourly: false # get hourly usage
renewable_energy_sensor_enable: false # enable renewable energy sensor
renewable_energy_sensor_name: 'CLP Renewable Energy' # renewable energy sensor name
renewable_energy_sensor_type: '' # type of data to be shown in state
renewable_energy_sensor_get_bill: false # get generation in bills
renewable_energy_sensor_get_daily: false # get daily generation
renewable_energy_sensor_get_hourly: false # get hourly generation
name: 'CLP'
username: ''
password: ''
```
- Possible values for `type`:
- BIMONTHLY
- DAILY
- HOURLY
- (EMPTY: best accurate value)
2. Restart Home-Assistant

| Key | Type | Required | Accepted Values | Default | Description |
|--------------------------------------|---------|----------|----------------------------------------------|--------------------------|-------------------------------------------------------------------------------------|
| `name` | string | * | Any string | `CLP` | Name of the sensor |
| `username` | string | * | Any string | (N/A) | CLP account username |
| `password` | string | * | Any string | (N/A) | CLP account password |
| `timeout` | int | | Any integer | `30` | Connection timeout in second |
| `type` | string | | ` `<br/>`BIMONTHLY`<br/>`DAILY`<br/>`HOURLY` | ` ` | Type of data to be shown in state<br/>If not specified, best accurate value is used |
| `get_account` | boolean | | `True`<br/>`False` | `False` | Get account summary |
| `get_bill` | boolean | | `True`<br/>`False` | `False` | Get bills |
| `get_estimation` | boolean | | `True`<br/>`False` | `False` | Get usage estimation |
| `get_daily` | boolean | | `True`<br/>`False` | `False` | Get daily usage |
| `get_hourly` | boolean | | `True`<br/>`False` | `False` | Get hourly usage |
| `renewable_energy_sensor_enable` | boolean | | `True`<br/>`False` | `False` | Enable renewable energy sensor |
| `renewable_energy_sensor_name` | string | | `True`<br/>`False` | `'CLP Renewable Energy'` | Name of the renewable energy sensor |
| `renewable_energy_sensor_type` | string | | ` `<br/>`BIMONTHLY`<br/>`DAILY`<br/>`HOURLY` | ` ` | Type of data to be shown in state<br/>If not specified, best accurate value is used |
| `renewable_energy_sensor_get_bill` | boolean | | `True`<br/>`False` | `False` | Get energy generation in bills |
| `renewable_energy_sensor_get_daily` | boolean | | `True`<br/>`False` | `False` | Get daily energy generation |
| `renewable_energy_sensor_get_hourly` | boolean | | `True`<br/>`False` | `False` | Get hourly energy generation |

## Common problem

- Single entity only. More than one `clp` entry will cause problems
- For slower hardware device, `TIMEOUT` may happen. Increase `timeout` in `configuration.yaml`

## Debug

- Configure `debug` level https://www.home-assistant.io/integrations/logger/
- SSH
- `docker logs -f homeassistant`
- Look for `CLP` wordings

## Use SSH on Home Assistant Operating System

1. Click on your username on UI
2. Turn on `Advanced Mode` on the right
3. Go to `Add-ons` -> `Add-on store`
Expand All @@ -69,8 +76,10 @@ sensor:
9. SSH to `hassio@IP` with the configured password

## Tested on
- Ubuntu 22.04
- Home Assistant Container 2024.6.2

- Ubuntu 24.04
- Home Assistant Container 2024.7

## Unofficial support

https://t.me/smarthomehk
2 changes: 1 addition & 1 deletion custom_components/clp/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "10.6.0",
"version": "10.6.2",
"domain": "clp",
"name": "CLP",
"documentation": "https://github.com/thematrixdev/home-assistant-clp",
Expand Down
30 changes: 15 additions & 15 deletions custom_components/clp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

vol.Optional(CONF_RES_ENABLE, default=False): cv.boolean,
vol.Optional(CONF_RES_NAME, default='CLP Renewable Energy'): cv.string,
vol.Optional(CONF_RES_TYPE, default=None): cv.string,
vol.Optional(CONF_RES_TYPE, default=''): cv.string,
vol.Optional(CONF_RES_GET_BILL, default=False): cv.boolean,
vol.Optional(CONF_RES_GET_DAILY, default=False): cv.boolean,
vol.Optional(CONF_RES_GET_HOURLY, default=False): cv.boolean,
Expand Down Expand Up @@ -461,7 +461,6 @@ async def async_update(self) -> None:
if self._get_hourly or self._type == '' or self._type.upper() == 'HOURLY':
_LOGGER.debug("CLP HOURLY")

self._hourly = []
async with async_timeout.timeout(self._timeout):
for i in range(2):
if i == 0:
Expand Down Expand Up @@ -496,22 +495,23 @@ async def async_update(self) -> None:
_LOGGER.debug(data)

if data['results']:
for row in data['results']:
start = None
if row['START_DT']:
start = datetime.datetime.strptime(row['START_DT'], '%Y%m%d%H%M%S')
if i == 0 and (self._type == '' or self._type.upper() == 'HOURLY'):
self._state_data_type = 'HOURLY'
self._attr_native_value = data['results'][-1]['KWH_TOTAL']
self._attr_last_reset = datetime.datetime.strptime(data['results'][-1]['START_DT'], '%Y%m%d%H%M%S')

self._hourly.append({
'start': start,
'kwh': row['KWH_TOTAL'],
})
if self._get_hourly:
for row in data['results']:
start = None
if row['START_DT']:
start = datetime.datetime.strptime(row['START_DT'], '%Y%m%d%H%M%S')

self._hourly = sorted(self._hourly, key=lambda x: x['start'], reverse=True)
self._hourly.append({
'start': start,
'kwh': row['KWH_TOTAL'],
})

if self._type == '' or self._type.upper() == 'HOURLY':
self._state_data_type = 'HOURLY'
self._attr_native_value = self._hourly[0]['kwh']
self._attr_last_reset = self._hourly[0]['start']
self._hourly = sorted(self._hourly, key=lambda x: x['start'], reverse=True)

elif self._sensor_type == 'renewable_energy':
_LOGGER.debug("CLP Renewable-Energy")
Expand Down

0 comments on commit ff97d66

Please sign in to comment.