Coverage for python_carrier_infinity/system.py: 100%

37 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-08-19 01:56 +0000

1"""Contains the System class""" 

2from __future__ import annotations 

3from textwrap import dedent 

4from . import api, config, status 

5from .types import ActivityName 

6from .gql_schemas import ( 

7 get_user_query, 

8 get_config_query, 

9 get_status_query, 

10 update_zone_config_query, 

11 update_activity_query, 

12) 

13 

14 

15class System: 

16 """Represents a Carrier Infinity system""" 

17 

18 def __init__(self, data: dict, location: str, auth: api.Auth): 

19 self.data = data 

20 self.location = location 

21 self.auth = auth 

22 

23 @property 

24 def name(self) -> str: 

25 """The name""" 

26 return self.data["name"] 

27 

28 @property 

29 def serial(self) -> str: 

30 """The serial number""" 

31 return self.data["serial"] 

32 

33 def __str__(self) -> str: 

34 return dedent( 

35 f"""\ 

36 Name: {self.name} 

37 Serial Number: {self.serial} 

38 Location: {self.location}""") 

39 

40 async def get_status(self) -> status.System: 

41 """Fetch current system status""" 

42 response = await api.gql_request(get_status_query(self.serial), self.auth) 

43 return status.System(response["data"]["infinityStatus"]) 

44 

45 async def get_config(self) -> config.System: 

46 """Fetch current system config""" 

47 response = await api.gql_request(get_config_query(self.serial), self.auth) 

48 return config.System(response["data"]["infinityConfig"]) 

49 

50 async def set_zone_activity_hold( 

51 self, 

52 zone_id: str, 

53 hold_activity: ActivityName | None, 

54 hold_until: str | None, 

55 ) -> None: 

56 """Set the activity hold of a zone""" 

57 hold_activity_string = hold_activity.value if hold_activity else None 

58 await api.gql_request( 

59 update_zone_config_query( 

60 self.serial, zone_id, hold_activity_string, hold_until 

61 ), 

62 self.auth, 

63 ) 

64 

65 async def set_zone_activity_temp( 

66 self, zone_id: str, activity: ActivityName, cool_temp: int, heat_temp: int 

67 ) -> None: 

68 """Set the target temperatures of an activity for a given zone""" 

69 await api.gql_request( 

70 update_activity_query( 

71 self.serial, zone_id, activity.value, cool_temp, heat_temp 

72 ), 

73 self.auth, 

74 ) 

75 

76async def get_systems(auth: api.Auth) -> dict[str, System]: 

77 """Fetch list of systems""" 

78 response = await api.gql_request(get_user_query(auth.username), auth) 

79 systems_dict = {} 

80 for location in response["data"]["user"]["locations"]: 

81 for system_data in location["systems"]: 

82 system = System(system_data["profile"], location["name"], auth) 

83 systems_dict[system.name] = system 

84 return systems_dict