Skip to content

Commit 82b45c2

Browse files
committed
Rework device registry
1 parent 0e27935 commit 82b45c2

File tree

6 files changed

+59
-46
lines changed

6 files changed

+59
-46
lines changed

custom_components/plugwise-beta/__init__.py

Lines changed: 4 additions & 2 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",
6365
name="{} - {} Gateway".format(entry.title, api.smile_name),
64-
model=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: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
4040
]
4141
all_devices = api.get_all_devices()
4242
for dev_id, device in all_devices.items():
43-
if device["class"] in binary_sensor_classes:
43+
if device["class"] in binary_sensor_classes:
4444
_LOGGER.debug("Plugwise device_class %s found", device["class"])
4545
data = api.get_device_data(dev_id)
4646
for binary_sensor in BINARY_SENSOR_LIST:
@@ -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

@@ -96,7 +92,7 @@ def _update_callback(self):
9692
@property
9793
def name(self):
9894
"""Return the name of the thermostat, if any."""
99-
return f"{self._name.replace('_', ' ')}"
95+
return self._name
10096

10197
@property
10298
def should_poll(self):

custom_components/plugwise-beta/climate.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def hvac_action(self):
135135
return CURRENT_HVAC_IDLE
136136
else:
137137
if (
138-
self._central_heating_state is not None
138+
self._central_heating_state is not None
139139
or self._boiler_state is not None
140140
):
141141
if self._thermostat > self._temperature:
@@ -145,7 +145,7 @@ def hvac_action(self):
145145
@property
146146
def name(self):
147147
"""Return the name of the thermostat, if any."""
148-
return self._name.replace('_', ' ')
148+
return self._name
149149

150150
@property
151151
def device_info(self) -> Dict[str, any]:
@@ -190,10 +190,7 @@ def preset_modes(self):
190190
@property
191191
def hvac_modes(self):
192192
"""Return the available hvac modes list."""
193-
if (
194-
self._central_heating_state is not None
195-
or self._boiler_state is not None
196-
):
193+
if self._central_heating_state is not None or self._boiler_state is not None:
197194
if self._cooling_state is not None:
198195
return HVAC_MODES_2
199196
return HVAC_MODES_1
@@ -328,5 +325,3 @@ def update(self):
328325
self._hvac_mode = HVAC_MODE_COOL
329326
else:
330327
self._hvac_mode = HVAC_MODE_OFF
331-
332-

custom_components/plugwise-beta/sensor.py

Lines changed: 29 additions & 14 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,21 +193,30 @@ 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+
211+
self._via_id = self._api.gateway_id
212+
if self._dev_id == self._via_id:
213+
self._via_id = None
214+
self._name = f"Smile {self._name}"
209215

210216
@property
211217
def unique_id(self):
212218
"""Return a unique ID."""
219+
_LOGGER.debug("Uniq id = %s",self._unique_id)
213220
return self._unique_id
214221

215222
async def async_added_to_hass(self):
@@ -239,7 +246,7 @@ def should_poll(self):
239246
@property
240247
def name(self):
241248
"""Return the name of the thermostat, if any."""
242-
return self._name.replace('_', ' ')
249+
return self._sensorname
243250

244251
@property
245252
def state(self):
@@ -253,7 +260,7 @@ def device_info(self) -> Dict[str, any]:
253260
"identifiers": {(DOMAIN, self._dev_id)},
254261
"name": self._name,
255262
"manufacturer": "Plugwise",
256-
"via_device": (DOMAIN, self._api.gateway_id),
263+
"via_device": (DOMAIN, self._via_id),
257264
}
258265

259266
@property
@@ -299,8 +306,16 @@ def __init__(self, api, updater, name, dev_id, sensor, sensor_type):
299306
self._class = sensor_type[2]
300307
self._sensor = sensor
301308
self._state = None
302-
self._unique_id = f"{dev_id}-{name}-{sensor_type[2]}"
303-
_LOGGER.debug("Registering Plugwise %s", self._unique_id)
309+
self._unique_id = f"{dev_id}-{name}-{sensor}"
310+
311+
sensorname = sensor.replace("_"," ").title()
312+
self._sensorname = f"{name} {sensorname}"
313+
314+
self._via_id = self._api.gateway_id
315+
if self._dev_id == self._via_id:
316+
self._via_id = None
317+
self._name = f"Smile {self._name}"
318+
304319

305320
@property
306321
def unique_id(self):
@@ -329,7 +344,7 @@ def should_poll(self):
329344
@property
330345
def name(self):
331346
"""Return the name of the sensor."""
332-
return self._name.replace('_', ' ')
347+
return self._sensorname
333348

334349
@property
335350
def icon(self):
@@ -353,7 +368,7 @@ def device_info(self) -> Dict[str, any]:
353368
"identifiers": {(DOMAIN, self._dev_id)},
354369
"name": self._name,
355370
"manufacturer": "Plugwise",
356-
"via_device": (DOMAIN, self._api.gateway_id),
371+
"via_device": (DOMAIN, self._via_id),
357372
}
358373

359374
@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)
@@ -102,7 +95,7 @@ async def turn_off(self, **kwargs):
10295
@property
10396
def name(self):
10497
"""Return the name of the thermostat, if any."""
105-
return self._name.replace('_', ' ')
98+
return self._name
10699

107100
@property
108101
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]:
@@ -108,7 +112,11 @@ def state(self):
108112
"""Return the state of the water_heater."""
109113
if self._cooling_state:
110114
return CURRENT_HVAC_COOL
111-
elif self._central_heating_state or self._boiler_state or self._domestic_hot_water_state:
115+
elif (
116+
self._central_heating_state
117+
or self._boiler_state
118+
or self._domestic_hot_water_state
119+
):
112120
return CURRENT_HVAC_HEAT
113121
else:
114122
return CURRENT_HVAC_IDLE
@@ -127,7 +135,11 @@ def icon(self):
127135
"""Return the icon to use in the frontend."""
128136
if self._cooling_state:
129137
return COOL_ICON
130-
elif self._central_heating_state or self._boiler_state or self._domestic_hot_water_state:
138+
elif (
139+
self._central_heating_state
140+
or self._boiler_state
141+
or self._domestic_hot_water_state
142+
):
131143
return FLAME_ICON
132144
else:
133145
return IDLE_ICON

0 commit comments

Comments
 (0)