Skip to content

Commit

Permalink
refactor: unqiue_id and name
Browse files Browse the repository at this point in the history
  • Loading branch information
ml1nk committed Dec 29, 2023
1 parent fa142af commit 0ec91ca
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 98 deletions.
5 changes: 4 additions & 1 deletion custom_components/mypyllant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ async def _async_update_data(self) -> dict[str, SystemWithDeviceData]:
):
if len(system.devices) == 0:
continue
data[system.id] = {"home_name": system.home.name, "devices_data": []}
data[system.id] = {
"home_name": system.home.home_name or system.home.nomenclature,
"devices_data": [],
}
for device in system.devices:
device_data = self.api.get_data_by_device(
device, DeviceDataBucketResolution.DAY, start, end
Expand Down
43 changes: 29 additions & 14 deletions custom_components/mypyllant/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from . import SystemCoordinator
from .const import DOMAIN
from .utils import get_unique_id_prefix

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -62,9 +61,17 @@ def system(self) -> System:
def entity_category(self) -> EntityCategory | None:
return EntityCategory.DIAGNOSTIC

@property
def id_infix(self) -> str:
return f"{self.system.id}_home"

@property
def name_prefix(self) -> str:
return f"{self.system.home.home_name or self.system.home.nomenclature}"

@property
def device_info(self) -> DeviceInfo | None:
return {"identifiers": {(DOMAIN, f"home_{self.system.id}")}}
return {"identifiers": {(DOMAIN, self.id_infix)}}


class ControlError(SystemControlEntity):
Expand All @@ -88,11 +95,11 @@ def is_on(self) -> bool | None:

@property
def name(self) -> str:
return f"{self.system.home.name} Trouble Codes on {self.system.system_name}"
return f"{self.name_prefix} Trouble Codes"

@property
def unique_id(self) -> str:
return f"{get_unique_id_prefix(self.system.id)}control_error"
return f"{DOMAIN}_{self.id_infix}_control_error"

@property
def device_class(self) -> BinarySensorDeviceClass | None:
Expand All @@ -113,11 +120,11 @@ def is_on(self) -> bool:

@property
def name(self) -> str:
return f"{self.system.home.name} Online Status"
return f"{self.name_prefix} Online Status"

@property
def unique_id(self) -> str:
return f"{get_unique_id_prefix(self.system.id)}control_online"
return f"{DOMAIN}_{self.id_infix}_control_online"

@property
def device_class(self) -> BinarySensorDeviceClass | None:
Expand All @@ -138,11 +145,11 @@ def is_on(self) -> bool | None:

@property
def name(self) -> str:
return f"{self.system.home.name} Firmware Update Required"
return f"{self.name_prefix} Firmware Update Required"

@property
def unique_id(self) -> str:
return f"{get_unique_id_prefix(self.system.id)}firmware_update_required"
return f"{DOMAIN}_{self.id_infix}_firmware_update_required"

@property
def device_class(self) -> BinarySensorDeviceClass | None:
Expand All @@ -163,11 +170,11 @@ def is_on(self) -> bool | None:

@property
def name(self) -> str:
return f"{self.system.home.name} Firmware Update Enabled"
return f"{self.name_prefix} Firmware Update Enabled"

@property
def unique_id(self) -> str:
return f"{get_unique_id_prefix(self.system.id)}firmware_update_enabled"
return f"{DOMAIN}_{self.id_infix}_firmware_update_enabled"


class CircuitEntity(CoordinatorEntity, BinarySensorEntity):
Expand All @@ -189,11 +196,19 @@ def system(self) -> System:
def circuit(self) -> Circuit:
return self.coordinator.data[self.system_index].circuits[self.circuit_index]

@property
def name_prefix(self) -> str:
return f"{self.system.home.home_name or self.system.home.nomenclature} Circuit {self.circuit_index}"

@property
def id_infix(self) -> str:
return f"{self.system.id}_circuit_{self.circuit.index}"

@property
def device_info(self) -> DeviceInfo | None:
return DeviceInfo(
identifiers={(DOMAIN, f"circuit_{self.system.id}_{self.circuit.index}")},
name=f"{self.system.home.name} Circuit {self.circuit_index}",
identifiers={(DOMAIN, self.id_infix)},
name=self.name_prefix,
manufacturer=self.system.brand_name,
)

Expand All @@ -213,8 +228,8 @@ def is_on(self) -> bool | None:

@property
def name(self) -> str:
return f"{self.system.home.name} Cooling Allowed in {self.circuit_index}"
return f"{self.name_prefix} Cooling Allowed"

@property
def unique_id(self) -> str:
return f"{get_unique_id_prefix(self.system.id)}cooling_allowed_{self.circuit_index}"
return f"{DOMAIN} {self.id_infix}_cooling_allowed"
24 changes: 15 additions & 9 deletions custom_components/mypyllant/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
ZoneTimeProgram,
)

from custom_components.mypyllant.utils import get_unique_id_prefix

from . import SystemCoordinator
from .const import (
DEFAULT_TIME_PROGRAM_OVERWRITE,
Expand Down Expand Up @@ -277,21 +275,29 @@ def circuit_name_suffix(self) -> str:
else:
return f" of Circuit {self.zone.associated_circuit_index}"

@property
def name_prefix(self) -> str:
return f"{self.system.home.home_name or self.system.home.nomenclature} Zone {self.zone.name}{self.circuit_name_suffix}"

@property
def id_infix(self) -> str:
return f"{self.system.id}_zone_{self.zone.index}"

@property
def device_info(self) -> DeviceInfo:
return DeviceInfo(
identifiers={(DOMAIN, f"zone_{self.system.id}_{self.zone.index}")},
name=f"{self.system.home.name} Zone {self.zone.name}{self.circuit_name_suffix}",
identifiers={(DOMAIN, self.id_infix)},
name=self.name_prefix,
manufacturer=self.system.brand_name,
)

@property
def unique_id(self) -> str:
return f"{get_unique_id_prefix(self.system.id)}climate_zone_{self.zone_index}"
return f"{DOMAIN}_{self.id_infix}_climate"

@property
def name(self) -> str:
return f"{self.system.home.name} Climate Zone {self.zone.name}"
return f"{self.name_prefix} Climate"

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
Expand Down Expand Up @@ -514,22 +520,22 @@ def ventilation(self) -> Ventilation:
def device_info(self) -> DeviceInfo:
return DeviceInfo(
identifiers={
(DOMAIN, f"ventilation_{self.system.id}_{self.ventilation.index}")
(DOMAIN, f"{self.system.id}_ventilation_{self.ventilation.index}")
},
name=self.name,
manufacturer=self.system.brand_name,
)

@property
def unique_id(self) -> str:
return f"{get_unique_id_prefix(self.system.id)}climate_ventilation_{self.ventilation_index}"
return f"{DOMAIN}_{self.system.id}_ventilation_{self.ventilation_index}_climate"

@property
def name(self) -> str:
vname = [d for d in self.system.devices if d.type == "ventilation"][
0
].name_display
return f"{self.system.home.name} Ventilation {vname}"
return f"{self.system.home.home_name or self.system.home.nomenclature} Ventilation {vname}"

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
Expand Down
Loading

0 comments on commit 0ec91ca

Please sign in to comment.