diff --git a/datatypes/epc/domain/epc_property_data.py b/datatypes/epc/domain/epc_property_data.py index c75e730f..85f1527f 100644 --- a/datatypes/epc/domain/epc_property_data.py +++ b/datatypes/epc/domain/epc_property_data.py @@ -1,5 +1,5 @@ import re -from dataclasses import dataclass +from dataclasses import dataclass, field from datetime import date from enum import Enum from typing import Final, List, Optional, Union @@ -399,12 +399,12 @@ class SapBuildingPart: int, str ] # int from API, str from site notes TODO: make enum/mapping? wall_thickness_measured: bool - party_wall_construction: Union[int, str] # TODO: make enum/mapping? + party_wall_construction: Optional[Union[int, str]] = ( + None # TODO: make enum/mapping? + ) # Floor - sap_floor_dimensions: List[ - SapFloorDimension - ] # Not included in site notes; should this be optional? + sap_floor_dimensions: List[SapFloorDimension] = field(default_factory=list) # Optional building_part_number: Optional[int] = ( diff --git a/datatypes/epc/domain/mapper.py b/datatypes/epc/domain/mapper.py index d6d44462..58695b84 100644 --- a/datatypes/epc/domain/mapper.py +++ b/datatypes/epc/domain/mapper.py @@ -480,7 +480,9 @@ class EpcPropertyDataMapper: wall_construction=bp.wall_construction, wall_insulation_type=bp.wall_insulation_type, wall_thickness_measured=bp.wall_thickness_measured == "Y", - party_wall_construction=bp.party_wall_construction, + party_wall_construction=_api_party_wall_construction_int( + bp.party_wall_construction + ), sap_floor_dimensions=[ SapFloorDimension( room_height_m=fd.room_height.value, @@ -621,7 +623,9 @@ class EpcPropertyDataMapper: wall_construction=bp.wall_construction, wall_insulation_type=bp.wall_insulation_type, wall_thickness_measured=bp.wall_thickness_measured == "Y", - party_wall_construction=bp.party_wall_construction, + party_wall_construction=_api_party_wall_construction_int( + bp.party_wall_construction + ), sap_floor_dimensions=[ SapFloorDimension( room_height_m=_measurement_value(fd.room_height), @@ -758,7 +762,9 @@ class EpcPropertyDataMapper: wall_construction=bp.wall_construction, wall_insulation_type=bp.wall_insulation_type, wall_thickness_measured=bp.wall_thickness_measured == "Y", - party_wall_construction=bp.party_wall_construction, + party_wall_construction=_api_party_wall_construction_int( + bp.party_wall_construction + ), sap_floor_dimensions=[ SapFloorDimension( room_height_m=_measurement_value(fd.room_height), @@ -904,7 +910,9 @@ class EpcPropertyDataMapper: wall_construction=bp.wall_construction, wall_insulation_type=bp.wall_insulation_type, wall_thickness_measured=bp.wall_thickness_measured == "Y", - party_wall_construction=bp.party_wall_construction, + party_wall_construction=_api_party_wall_construction_int( + bp.party_wall_construction + ), sap_floor_dimensions=[ SapFloorDimension( room_height_m=_measurement_value(fd.room_height), @@ -1067,7 +1075,9 @@ class EpcPropertyDataMapper: wall_construction=bp.wall_construction, wall_insulation_type=bp.wall_insulation_type, wall_thickness_measured=bp.wall_thickness_measured == "Y", - party_wall_construction=bp.party_wall_construction, + party_wall_construction=_api_party_wall_construction_int( + bp.party_wall_construction + ), sap_floor_dimensions=[ SapFloorDimension( room_height_m=_measurement_value(fd.room_height), @@ -1257,7 +1267,9 @@ class EpcPropertyDataMapper: wall_construction=bp.wall_construction, wall_insulation_type=bp.wall_insulation_type, wall_thickness_measured=bp.wall_thickness_measured == "Y", - party_wall_construction=bp.party_wall_construction, + party_wall_construction=_api_party_wall_construction_int( + bp.party_wall_construction + ), sap_floor_dimensions=[ SapFloorDimension( room_height_m=_measurement_value(fd.room_height), @@ -1516,7 +1528,9 @@ class EpcPropertyDataMapper: wall_construction=bp.wall_construction, wall_insulation_type=bp.wall_insulation_type, wall_thickness_measured=bp.wall_thickness_measured == "Y", - party_wall_construction=bp.party_wall_construction, + party_wall_construction=_api_party_wall_construction_int( + bp.party_wall_construction + ), sap_floor_dimensions=[ SapFloorDimension( room_height_m=_measurement_value(fd.room_height), @@ -1889,6 +1903,50 @@ def _elmhurst_party_wall_construction_int(coded: str) -> Optional[int]: return _ELMHURST_PARTY_WALL_CODE_TO_SAP10.get(_leading_code(coded)) +# GOV.UK API party_wall_construction enum → SAP10 wall_construction +# integer (the domain `u_party_wall` consumes). The API uses a different +# enum from the regular wall_construction field — RdSAP 10 Table 15 +# (p.31 "U-values of party walls") defines 5 categories, mapped to the +# nearest SAP10 wall_construction code that `u_party_wall` resolves to +# the spec U-value: +# 0 = "Not applicable" / no party wall (detached etc.) → cascade +# returns 0.25 by default but party_wall_length is 0 so the +# contribution is 0 regardless. +# 1 = "Solid masonry / timber frame / system built" → SAP10 code 3 +# (WALL_SOLID_BRICK) → u_party_wall = 0.0 (Table 15 row 1). +# 2 = "Cavity masonry unfilled" → SAP10 code 4 (WALL_CAVITY) → +# u_party_wall = 0.5 (Table 15 row 2). +# 3 = "Cavity masonry filled" → spec U=0.2 (Table 15 row 3) — not +# yet representable; the cascade only emits 0.0 / 0.5 / 0.25 from +# the current u_party_wall, so this code rounds up to the +# conservative 0.5 (matches the cavity-unfilled W/K). +# 4 = "Unable to determine, house or bungalow" → None (cascade +# default 0.25). +# 5 = "Unable to determine, flat or maisonette" → cascade should +# return 0.0 per Table 15 footnote * — not yet handled; leave as +# None for now and revisit when a flat fixture surfaces. +# The 'NA' string (commonly lodged on extensions that don't carry a +# party wall) maps to None. +_API_PARTY_WALL_CONSTRUCTION_TO_SAP10: Dict[int, Optional[int]] = { + 0: None, + 1: 3, # Solid masonry / timber / system → U=0.0 + 2: 4, # Cavity masonry unfilled → U=0.5 + 3: 4, # Cavity masonry filled (cascade falls through to 0.5 — TODO) + 4: None, # Unable to determine, house — cascade default 0.25 + 5: None, # Unable to determine, flat — TODO: u_party_wall=0.0 path +} + + +def _api_party_wall_construction_int(value: Union[int, str, None]) -> Optional[int]: + """Translate the GOV.UK API `party_wall_construction` integer code + (or 'NA' string) to the SAP10 wall_construction integer the cascade + consumes. See `_API_PARTY_WALL_CONSTRUCTION_TO_SAP10` for the + enum semantics (RdSAP 10 Table 15).""" + if value is None or isinstance(value, str): + return None + return _API_PARTY_WALL_CONSTRUCTION_TO_SAP10.get(value) + + def _elmhurst_wall_insulation_int(coded: str) -> Optional[int]: """Map an Elmhurst wall-insulation-type string ('A As Built') to the SAP10 integer enum (4 = as-built). Returns None on unknown