Skip to content

Commit 899ce08

Browse files
committed
Add binary_sensors and cooling to water_heater
1 parent ae6a0c1 commit 899ce08

File tree

5 files changed

+160
-14
lines changed

5 files changed

+160
-14
lines changed

custom_components/plugwise-beta/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
_LOGGER = logging.getLogger(__name__)
2121

22-
PLATFORMS = ["climate", "sensor", "switch", "water_heater"]
22+
PLATFORMS = ["binart_sensor", "climate", "sensor", "switch", "water_heater"]
2323

2424

2525
async def async_setup(hass: HomeAssistant, config: dict):
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"""Plugwise Binary Sensor component for Home Assistant."""
2+
3+
import logging
4+
from typing import Dict
5+
6+
from homeassistant.components.binary_sensor import DEVICE_CLASS_HEAT, BinarySensorDevice
7+
from homeassistant.const import STATE_OFF, STATE_ON
8+
from homeassistant.core import callback
9+
10+
from homeassistant.components.climate.const import (
11+
CURRENT_HVAC_COOL,
12+
CURRENT_HVAC_HEAT,
13+
CURRENT_HVAC_IDLE,
14+
)
15+
16+
from .const import (
17+
CURRENT_HVAC_DHW,
18+
DOMAIN,
19+
COOL_ICON,
20+
FLAME_ICON,
21+
IDLE_ICON,
22+
WATER_ICON,
23+
)
24+
25+
BINARY_SENSOR_LIST = [
26+
"domestic_hot_water_state",
27+
"slave_boiler_state",
28+
]
29+
30+
_LOGGER = logging.getLogger(__name__)
31+
32+
33+
async def async_setup_entry(hass, config_entry, async_add_entities):
34+
"""Set up the Smile binary_sensors from a config entry."""
35+
api = hass.data[DOMAIN][config_entry.entry_id]["api"]
36+
updater = hass.data[DOMAIN][config_entry.entry_id]["updater"]
37+
38+
devices = []
39+
all_devices = api.get_all_devices()
40+
for dev_id, device in all_devices.items():
41+
if device["class"] == "heater_central":
42+
_LOGGER.debug("Plugwise device_class found")
43+
data = api.get_device_data(dev_id)
44+
for binary_sensor in BINARY_SENSOR_LIST:
45+
_LOGGER.debug("Binary_sensor: %s", binary_sensor)
46+
if binary_sensor in data:
47+
_LOGGER.debug("Plugwise binary_sensor Dev %s", device["name"])
48+
devices.append(PwBinarySensor(api, updater, binary_sensor, dev_id))
49+
_LOGGER.info("Added binary_sensor.%s", binary_sensor)
50+
51+
async_add_entities(devices, True)
52+
53+
54+
class PwBinarySensor(BinarySensorDevice):
55+
"""Representation of a Plugwise binary_sensor."""
56+
57+
def __init__(self, api, updater, binary_sensor, dev_id):
58+
"""Set up the Plugwise API."""
59+
self._api = api
60+
self._updater = updater
61+
self._dev_id = dev_id
62+
self._binary_sensor = binary_sensor
63+
self._is_on = False
64+
self._unique_id = f"{dev_id}-binary_sensor"
65+
66+
@property
67+
def unique_id(self):
68+
"""Return a unique ID."""
69+
return self._unique_id
70+
71+
async def async_added_to_hass(self):
72+
"""Register callbacks."""
73+
self._updater.async_add_listener(self._update_callback)
74+
75+
async def async_will_remove_from_hass(self):
76+
"""Disconnect callbacks."""
77+
self._updater.async_remove_listener(self._update_callback)
78+
79+
@callback
80+
def _update_callback(self):
81+
"""Call update method."""
82+
self.update()
83+
self.async_write_ha_state()
84+
85+
@property
86+
def name(self):
87+
"""Return the name of the thermostat, if any."""
88+
return self._binary_sensor
89+
90+
@property
91+
def device_info(self) -> Dict[str, any]:
92+
"""Return the device information."""
93+
return {
94+
"identifiers": {(DOMAIN, self._dev_id)},
95+
"name": self._binary_sensor,
96+
"manufacturer": "Plugwise",
97+
"via_device": (DOMAIN, self._api.gateway_id),
98+
}
99+
100+
@property
101+
def should_poll(self):
102+
"""No need to poll. Coordinator notifies entity of updates."""
103+
return False
104+
105+
@property
106+
def is_on(self):
107+
"""Return true if the binary sensor is on."""
108+
return self.is_on
109+
110+
@property
111+
def state(self):
112+
"""Return the state of the binary sensor."""
113+
if self._is_on:
114+
return STATE_ON
115+
return STATE_OFF
116+
117+
@property
118+
def icon(self):
119+
"""Return the icon to use in the frontend."""
120+
if self._binary_sensor == "domestic_hot_water_state":
121+
return WATER_ICON
122+
return FLAME_ICON
123+
124+
@property
125+
def device_class(self):
126+
"""Return the class of this device, from component DEVICE_CLASSES."""
127+
return DEVICE_CLASS_HEAT
128+
129+
def update(self):
130+
"""Update the entity."""
131+
_LOGGER.debug("Update binary_sensor called")
132+
data = self._api.get_device_data(self._dev_id)
133+
134+
if data is None:
135+
_LOGGER.error("Received no data for device %s.", self._binary_sensor)
136+
else:
137+
if self._binary_sensor in data:
138+
self._is_on = data[self._binary_sensor]

custom_components/plugwise-beta/const.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
# Icons
3030
SWITCH_ICON = "mdi:electric-switch"
3131
THERMOSTAT_ICON = "mdi:thermometer"
32-
WATER_HEATER_ICON = "mdi:water-pump"
32+
WATER_ICON = "mdi:water-pump"
3333
FLAME_ICON = "mdi:fire"
34+
COOL_ICON = "mdi:snowflake"
3435
IDLE_ICON = "mdi:circle-off-outline"
3536
GAS_ICON = "mdi:fire"
3637
POWER_ICON = "mdi:flash"

custom_components/plugwise-beta/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"domain": "plugwise-beta",
33
"name": "Plugwise Beta for Home Assistant",
44
"documentation": "https://github.com/plugwise/plugwise-beta",
5-
"requirements": ["Plugwise_Smile==0.1.12"],
5+
"requirements": ["Plugwise_Smile==0.1.13"],
66
"dependencies": [],
77
"codeowners": ["@CoMPaTech","@bouwew"],
88
"config_flow": true

custom_components/plugwise-beta/water_heater.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
from homeassistant.helpers.entity import Entity
88

99
from homeassistant.components.climate.const import (
10+
CURRENT_HVAC_COOL,
1011
CURRENT_HVAC_HEAT,
1112
CURRENT_HVAC_IDLE,
1213
)
1314

1415
from .const import (
1516
CURRENT_HVAC_DHW,
1617
DOMAIN,
18+
COOL_ICON,
1719
FLAME_ICON,
1820
IDLE_ICON,
19-
WATER_HEATER_ICON,
2021
)
2122

2223
_LOGGER = logging.getLogger(__name__)
@@ -50,11 +51,12 @@ def __init__(self, api, updater, name, dev_id):
5051
self._updater = updater
5152
self._name = name
5253
self._dev_id = dev_id
53-
self._boiler_state = False
5454
self._boiler_temp = None
55+
self._boiler_state = False
5556
self._central_heating_state = False
56-
self._central_heater_water_pressure = None
57+
self._cooling_state = False
5758
self._domestic_hot_water_state = False
59+
self._central_heater_water_pressure = None
5860
self._unique_id = f"{dev_id}-water_heater"
5961

6062
@property
@@ -94,11 +96,12 @@ def device_info(self) -> Dict[str, any]:
9496
@property
9597
def state(self):
9698
"""Return the state of the water_heater."""
97-
if self._central_heating_state or self._boiler_state:
99+
if self._cooling_state:
100+
return CURRENT_HVAC_COOL
101+
elif self._central_heating_state or self._boiler_state or self._domestic_hot_water_state:
98102
return CURRENT_HVAC_HEAT
99-
if self._domestic_hot_water_state:
100-
return CURRENT_HVAC_DHW
101-
return CURRENT_HVAC_IDLE
103+
else:
104+
return CURRENT_HVAC_IDLE
102105

103106
@property
104107
def device_state_attributes(self):
@@ -112,11 +115,12 @@ def device_state_attributes(self):
112115
@property
113116
def icon(self):
114117
"""Return the icon to use in the frontend."""
115-
if self._central_heating_state or self._boiler_state:
118+
if self._cooling_state:
119+
return COOL_ICON
120+
elif self._central_heating_state or self._boiler_state or self._domestic_hot_water_state:
116121
return FLAME_ICON
117-
if self._domestic_hot_water_state:
118-
return WATER_HEATER_ICON
119-
return IDLE_ICON
122+
else:
123+
return IDLE_ICON
120124

121125
@property
122126
def should_poll(self):
@@ -143,5 +147,8 @@ def update(self):
143147
if "central_heating_state" in data:
144148
if data["central_heating_state"] is not None:
145149
self._central_heating_state = data["central_heating_state"]
150+
if "cooling_state" in data:
151+
if data["cooling_state"] is not None:
152+
self._cooling_state = data["boiler_state"]
146153
if "domestic_hot_water_state" in data:
147154
self._domestic_hot_water_state = data["domestic_hot_water_state"]

0 commit comments

Comments
 (0)