Skip to content

Commit 8995c16

Browse files
authored
Merge pull request #34 from plugwise/s_m_t_devicenames
Rework device registry
2 parents 907be20 + 820f422 commit 8995c16

File tree

6 files changed

+72
-59
lines changed

6 files changed

+72
-59
lines changed

custom_components/plugwise-beta/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
SINGLE = ["binary_sensor", "climate", "sensor", "switch"]
2424
MULTI = ["binary_sensor", "climate", "sensor", "switch", "water_heater"]
2525

26+
2627
async def async_setup(hass: HomeAssistant, config: dict):
2728
"""Set up the Plugwise platform."""
2829
return True
@@ -44,6 +45,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
4445
else:
4546
update_interval = timedelta(seconds=60)
4647

48+
api.get_all_devices()
49+
4750
_LOGGER.debug("Plugwise async update interval %s", update_interval)
4851

4952
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
@@ -55,13 +58,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
5558

5659
_LOGGER.debug("Plugwise gateway is %s", api.gateway_id)
5760
device_registry = await dr.async_get_registry(hass)
58-
_LOGGER.debug("Plugwise device registry %s", device_registry)
5961
result = device_registry.async_get_or_create(
6062
config_entry_id=entry.entry_id,
6163
identifiers={(DOMAIN, api.gateway_id)},
6264
manufacturer="Plugwise",
63-
name=entry.title,
64-
model=api.smile_name,
65+
name="{} - {} Gateway".format(entry.title, api.smile_name),
66+
model=f"Smile {api.smile_name}",
6567
sw_version=api.smile_version[0],
6668
)
6769
_LOGGER.debug("Plugwise device registry %s", result)

custom_components/plugwise-beta/binary_sensor.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
4949
_LOGGER.debug("Plugwise binary_sensor Dev %s", device["name"])
5050
devices.append(
5151
PwBinarySensor(
52-
api,
53-
updater,
54-
"{}_{}".format(device["name"], binary_sensor),
55-
binary_sensor,
56-
dev_id,
52+
api, updater, device["name"], binary_sensor, dev_id,
5753
)
5854
)
59-
_LOGGER.info("Added binary_sensor.%s", binary_sensor)
55+
_LOGGER.info("Added binary_sensor.%s", device["name"])
6056

6157
async_add_entities(devices, True)
6258

@@ -72,7 +68,15 @@ def __init__(self, api, updater, name, binary_sensor, dev_id):
7268
self._name = name
7369
self._binary_sensor = binary_sensor
7470
self._is_on = False
75-
self._unique_id = f"bs-{dev_id}-{name}"
71+
self._unique_id = f"bs-{dev_id}-{name}-{binary_sensor}"
72+
73+
sensorname = binary_sensor.replace("_", " ").title()
74+
self._sensorname = f"{name} {sensorname}"
75+
76+
self._via_id = self._api.gateway_id
77+
if self._dev_id == self._via_id:
78+
self._via_id = None
79+
self._name = f"Smile {self._name}"
7680

7781
@property
7882
def unique_id(self):
@@ -96,7 +100,7 @@ def _update_callback(self):
96100
@property
97101
def name(self):
98102
"""Return the name of the thermostat, if any."""
99-
return f"{self._name.replace('_', ' ')}"
103+
return self._sensorname
100104

101105
@property
102106
def should_poll(self):
@@ -118,15 +122,11 @@ def state(self):
118122
@property
119123
def device_info(self) -> Dict[str, any]:
120124
"""Return the device information."""
121-
via_device = None
122-
dev_name = f"{self._name.split('_')[0]} Sensors"
123-
if self._dev_id is not self._api.gateway_id:
124-
via_device = (DOMAIN, self._api.gateway_id)
125125
return {
126126
"identifiers": {(DOMAIN, self._dev_id)},
127-
"name": dev_name,
127+
"name": self._name,
128128
"manufacturer": "Plugwise",
129-
"via_device": via_device,
129+
"via_device": (DOMAIN, self._via_id),
130130
}
131131

132132
@property

custom_components/plugwise-beta/climate.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def hvac_action(self):
134134
return CURRENT_HVAC_IDLE
135135
else:
136136
if (
137-
self._central_heating_state is not None
137+
self._central_heating_state is not None
138138
or self._boiler_state is not None
139139
):
140140
if self._thermostat > self._temperature:
@@ -144,7 +144,7 @@ def hvac_action(self):
144144
@property
145145
def name(self):
146146
"""Return the name of the thermostat, if any."""
147-
return self._name.replace('_', ' ')
147+
return self._name
148148

149149
@property
150150
def device_info(self) -> Dict[str, any]:
@@ -193,10 +193,7 @@ def preset_modes(self):
193193
@property
194194
def hvac_modes(self):
195195
"""Return the available hvac modes list."""
196-
if (
197-
self._central_heating_state is not None
198-
or self._boiler_state is not None
199-
):
196+
if self._central_heating_state is not None or self._boiler_state is not None:
200197
if self._cooling_state is not None:
201198
return HVAC_MODES_2
202199
return HVAC_MODES_1
@@ -326,7 +323,8 @@ def update(self):
326323
self._hvac_mode = HVAC_MODE_HEAT_COOL
327324
self._hvac_mode = HVAC_MODE_HEAT
328325
elif self._cooling_state is not None:
329-
if self._central_heating_state is not None or self._boiler_state is not None:
326+
if (
327+
self._central_heating_state is not None
328+
or self._boiler_state is not None
329+
):
330330
self._hvac_mode = HVAC_MODE_HEAT_COOL
331-
332-

custom_components/plugwise-beta/sensor.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
164164
PwPowerSensor(
165165
api,
166166
updater,
167-
"{}_{}".format(device["name"], sensor),
167+
device["name"],
168168
dev_id,
169169
sensor,
170170
sensor_type,
@@ -175,15 +175,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
175175
PwThermostatSensor(
176176
api,
177177
updater,
178-
"{}_{}".format(device["name"], sensor),
178+
device["name"],
179179
dev_id,
180180
sensor,
181181
sensor_type,
182182
)
183183
)
184-
_LOGGER.info(
185-
"Added sensor.%s", "{}_{}".format(device["name"], sensor)
186-
)
184+
_LOGGER.info("Added sensor.%s", device["name"])
187185

188186
async_add_entities(devices, True)
189187

@@ -195,17 +193,24 @@ def __init__(self, api, updater, name, dev_id, sensor, sensor_type):
195193
"""Set up the Plugwise API."""
196194
self._api = api
197195
self._updater = updater
198-
self._name = name
199196
self._dev_id = dev_id
200197
self._device = sensor_type[2]
198+
self._name = name
201199
self._sensor = sensor
202200
self._sensor_type = sensor_type
203201
self._unit_of_measurement = sensor_type[1]
204202
self._icon = sensor_type[3]
205203
self._class = sensor_type[2]
206204
self._state = None
207-
self._unique_id = f"sr-{dev_id}-{name}"
208-
_LOGGER.debug("Registering Plugwise %s", self._unique_id)
205+
self._unique_id = f"cl-{dev_id}-{name}-{sensor}"
206+
207+
sensorname = sensor.replace("_", " ").title()
208+
self._sensorname = f"{name} {sensorname}"
209+
210+
self._via_id = self._api.gateway_id
211+
if self._dev_id == self._via_id:
212+
self._via_id = None
213+
self._name = f"Smile {self._name}"
209214

210215
@property
211216
def unique_id(self):
@@ -239,7 +244,7 @@ def should_poll(self):
239244
@property
240245
def name(self):
241246
"""Return the name of the thermostat, if any."""
242-
return self._name.replace('_', ' ')
247+
return self._sensorname
243248

244249
@property
245250
def state(self):
@@ -249,15 +254,11 @@ def state(self):
249254
@property
250255
def device_info(self) -> Dict[str, any]:
251256
"""Return the device information."""
252-
via_device = None
253-
dev_name = f"{self._name.split('_')[0]} Sensors"
254-
if self._dev_id is not self._api.gateway_id:
255-
via_device = (DOMAIN, self._api.gateway_id)
256257
return {
257258
"identifiers": {(DOMAIN, self._dev_id)},
258-
"name": dev_name,
259+
"name": self._name,
259260
"manufacturer": "Plugwise",
260-
"via_device": via_device,
261+
"via_device": (DOMAIN, self._via_id),
261262
}
262263

263264
@property
@@ -303,8 +304,15 @@ def __init__(self, api, updater, name, dev_id, sensor, sensor_type):
303304
self._class = sensor_type[2]
304305
self._sensor = sensor
305306
self._state = None
306-
self._unique_id = f"{dev_id}-{name}-{sensor_type[2]}"
307-
_LOGGER.debug("Registering Plugwise %s", self._unique_id)
307+
self._unique_id = f"{dev_id}-{name}-{sensor}"
308+
309+
sensorname = sensor.replace("_", " ").title()
310+
self._sensorname = f"{name} {sensorname}"
311+
312+
self._via_id = self._api.gateway_id
313+
if self._dev_id == self._via_id:
314+
self._via_id = None
315+
self._name = f"Smile {self._name}"
308316

309317
@property
310318
def unique_id(self):
@@ -333,7 +341,7 @@ def should_poll(self):
333341
@property
334342
def name(self):
335343
"""Return the name of the sensor."""
336-
return self._name.replace('_', ' ')
344+
return self._sensorname
337345

338346
@property
339347
def icon(self):
@@ -357,7 +365,7 @@ def device_info(self) -> Dict[str, any]:
357365
"identifiers": {(DOMAIN, self._dev_id)},
358366
"name": self._name,
359367
"manufacturer": "Plugwise",
360-
"via_device": (DOMAIN, self._api.gateway_id),
368+
"via_device": (DOMAIN, self._via_id),
361369
}
362370

363371
@property

custom_components/plugwise-beta/switch.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
2121
for dev_id, device in all_devices.items():
2222
if "plug" in device["types"]:
2323
_LOGGER.debug("Plugwise switch Dev %s", device["name"])
24-
devices.append(
25-
PwSwitch(
26-
api,
27-
updater,
28-
device["name"],
29-
dev_id,
30-
)
31-
)
24+
devices.append(PwSwitch(api, updater, device["name"], dev_id,))
3225
_LOGGER.info("Added switch.%s", "{}".format(device["name"]))
3326

3427
async_add_entities(devices, True)
@@ -106,7 +99,7 @@ async def turn_off(self, **kwargs):
10699
@property
107100
def name(self):
108101
"""Return the name of the thermostat, if any."""
109-
return self._name.replace('_', ' ')
102+
return self._name
110103

111104
@property
112105
def icon(self):

custom_components/plugwise-beta/water_heater.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
4444
if state in data:
4545
if idx == 1:
4646
_LOGGER.debug("Plugwise water_heater Dev %s", device["name"])
47-
water_heater = PwWaterHeater(api, updater, device["name"], dev_id)
47+
water_heater = PwWaterHeater(
48+
api, updater, device["name"], dev_id
49+
)
4850
devices.append(water_heater)
49-
_LOGGER.info("Added water_heater.%s", "{}".format(device["name"]))
51+
_LOGGER.info(
52+
"Added water_heater.%s", "{}".format(device["name"])
53+
)
5054
idx += 1
5155

5256
async_add_entities(devices, True)
@@ -91,7 +95,7 @@ def _update_callback(self):
9195
@property
9296
def name(self):
9397
"""Return the name of the thermostat, if any."""
94-
return self._name.replace('_', ' ')
98+
return self._name
9599

96100
@property
97101
def device_info(self) -> Dict[str, any]:
@@ -112,7 +116,11 @@ def state(self):
112116
"""Return the state of the water_heater."""
113117
if self._cooling_state:
114118
return CURRENT_HVAC_COOL
115-
elif self._central_heating_state or self._boiler_state or self._domestic_hot_water_state:
119+
elif (
120+
self._central_heating_state
121+
or self._boiler_state
122+
or self._domestic_hot_water_state
123+
):
116124
return CURRENT_HVAC_HEAT
117125
else:
118126
return CURRENT_HVAC_IDLE
@@ -131,7 +139,11 @@ def icon(self):
131139
"""Return the icon to use in the frontend."""
132140
if self._cooling_state:
133141
return COOL_ICON
134-
elif self._central_heating_state or self._boiler_state or self._domestic_hot_water_state:
142+
elif (
143+
self._central_heating_state
144+
or self._boiler_state
145+
or self._domestic_hot_water_state
146+
):
135147
return FLAME_ICON
136148
else:
137149
return IDLE_ICON

0 commit comments

Comments
 (0)