Skip to content

Commit 403ebfc

Browse files
committed
Refactor EvohomeFactory to module level, align defaults, drop redundant evohome_client teardown
1 parent b8074fa commit 403ebfc

1 file changed

Lines changed: 46 additions & 46 deletions

File tree

tests/conftest.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class FakeZone:
5353
active_faults: list = field(default_factory=list)
5454
mode: ZoneMode = ZoneMode.FOLLOW_SCHEDULE
5555
temperature_status: dict = field(default_factory=lambda: {"temperature": 19})
56-
setpoint_status: dict = field(default_factory=lambda: {"setpoint_mode": "FollowSchedule", "target_heat_temperature": 20})
56+
setpoint_status: dict = field(default_factory=lambda: {"setpoint_mode": "FollowSchedule", "target_heat_temperature": 21})
5757
schedule: list = field(default_factory=_uniform_schedule)
5858

5959

@@ -104,63 +104,63 @@ class State(NamedTuple):
104104
zone: FakeZone
105105

106106

107+
class EvohomeFactory:
108+
@staticmethod
109+
def zone(**kwargs):
110+
return FakeZone(**kwargs)
111+
112+
@staticmethod
113+
def control_system(mode=SystemMode.AUTO, zones=None, system_id="system-1"):
114+
return FakeControlSystem(mode=mode, zones=zones or [], id=system_id)
115+
116+
@staticmethod
117+
def location(*, name="Home", control_systems=None):
118+
gateway = FakeGateway(systems=control_systems or [])
119+
return FakeLocation(name=name, gateways=[gateway])
120+
121+
@staticmethod
122+
def switchpoint(time_of_day, heat_setpoint):
123+
return _make_switchpoint(time_of_day, heat_setpoint)
124+
125+
@staticmethod
126+
def day_schedule(day_of_week_int, switchpoints):
127+
return _make_day_schedule(day_of_week_int, switchpoints)
128+
129+
@staticmethod
130+
def uniform_schedule(setpoint=20.0, time_of_day="07:00:00"):
131+
return _uniform_schedule(setpoint, time_of_day)
132+
133+
@staticmethod
134+
def complete_state(*, location_name="Home", zone_mode=ZoneMode.FOLLOW_SCHEDULE, system_mode=SystemMode.AUTO, with_fault=False, setpoint=21, schedule=None):
135+
zone = EvohomeFactory.zone(
136+
name="Living",
137+
mode=zone_mode,
138+
setpoint_status={"setpoint_mode": zone_mode, "target_heat_temperature": setpoint},
139+
temperature_status={"temperature": 20},
140+
active_faults=["fault"] if with_fault else [],
141+
schedule=schedule if schedule is not None else EvohomeFactory.uniform_schedule(setpoint=float(setpoint)),
142+
)
143+
control = EvohomeFactory.control_system(mode=system_mode, zones=[zone])
144+
location = EvohomeFactory.location(name=location_name, control_systems=[control])
145+
return State(location=location, control_system=control, zone=zone)
146+
147+
107148
@pytest.fixture
108149
def evohome_factory():
109-
class Factory:
110-
@staticmethod
111-
def zone(**kwargs):
112-
return FakeZone(**kwargs)
113-
114-
@staticmethod
115-
def control_system(mode=SystemMode.AUTO, zones=None, system_id="system-1"):
116-
return FakeControlSystem(mode=mode, zones=zones or [], id=system_id)
117-
118-
@staticmethod
119-
def location(*, name="Home", control_systems=None):
120-
gateway = FakeGateway(systems=control_systems or [])
121-
return FakeLocation(name=name, gateways=[gateway])
122-
123-
@staticmethod
124-
def switchpoint(time_of_day, heat_setpoint):
125-
return _make_switchpoint(time_of_day, heat_setpoint)
126-
127-
@staticmethod
128-
def day_schedule(day_of_week_int, switchpoints):
129-
return _make_day_schedule(day_of_week_int, switchpoints)
130-
131-
@staticmethod
132-
def uniform_schedule(setpoint=20.0, time_of_day="07:00:00"):
133-
return _uniform_schedule(setpoint, time_of_day)
134-
135-
@staticmethod
136-
def complete_state(*, location_name="Home", zone_mode=ZoneMode.FOLLOW_SCHEDULE, system_mode=SystemMode.AUTO, with_fault=False, setpoint=21, schedule=None):
137-
zone = FakeZone(
138-
name="Living",
139-
mode=zone_mode,
140-
setpoint_status={"setpoint_mode": zone_mode, "target_heat_temperature": setpoint},
141-
temperature_status={"temperature": 20},
142-
active_faults=["fault"] if with_fault else [],
143-
schedule=schedule if schedule is not None else Factory.uniform_schedule(setpoint=float(setpoint)),
144-
)
145-
control = FakeControlSystem(mode=system_mode, zones=[zone], id="sys-1")
146-
location = FakeLocation(name=location_name, gateways=[FakeGateway(systems=[control])])
147-
return State(location=location, control_system=control, zone=zone)
148-
149-
return Factory
150+
return EvohomeFactory
150151

151152

152153
@pytest.fixture
153-
def evohome_client(evohome_factory):
154+
def evohome_client():
154155
from evohome_helper import evohome
155156

156157
def _build(*, location_name="Home", **state_kwargs):
157-
state = evohome_factory.complete_state(location_name=location_name, **state_kwargs)
158+
state = EvohomeFactory.complete_state(location_name=location_name, **state_kwargs)
158159
client = FakeEvohomeClient(locations=[state.location])
159160
evohome._evohome_client = client
160161
return state
161162

162-
yield _build
163-
evohome._evohome_client = None
163+
return _build
164164

165165

166166
@pytest.fixture(autouse=True)

0 commit comments

Comments
 (0)