Coverage for src / rasp_shutter / type_defs.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-02-13 00:10 +0900

1#!/usr/bin/env python3 

2""" 

3rasp-shutter アプリケーション用の型定義 

4 

5センサーデータやAPIレスポンスなどの構造を dataclass で明示的に定義します。 

6""" 

7 

8import datetime 

9from dataclasses import dataclass, field 

10from typing import TypedDict 

11 

12 

13@dataclass 

14class SensorValue: 

15 """センサー値の型定義 

16 

17 Attributes 

18 ---------- 

19 valid: データが有効かどうか 

20 value: センサーの測定値(valid=True の場合のみ有効) 

21 time: 測定時刻(valid=True の場合のみ有効) 

22 

23 """ 

24 

25 valid: bool 

26 value: float | None = None 

27 time: datetime.datetime | None = None 

28 

29 @classmethod 

30 def create_valid(cls, value: float, time: datetime.datetime) -> "SensorValue": 

31 """有効なセンサー値を作成""" 

32 return cls(valid=True, value=value, time=time) 

33 

34 @classmethod 

35 def create_invalid(cls) -> "SensorValue": 

36 """無効なセンサー値を作成""" 

37 return cls(valid=False) 

38 

39 

40@dataclass 

41class SensorData: 

42 """センサーデータ全体の型定義 

43 

44 Attributes 

45 ---------- 

46 lux: 照度センサーデータ 

47 solar_rad: 日射センサーデータ 

48 altitude: 太陽高度データ 

49 

50 """ 

51 

52 lux: SensorValue 

53 solar_rad: SensorValue 

54 altitude: SensorValue 

55 

56 

57@dataclass 

58class ShutterStateEntry: 

59 """シャッター状態エントリの型定義 

60 

61 Attributes 

62 ---------- 

63 name: シャッター名 

64 state: シャッター状態(0=開、1=閉、2=不明) 

65 

66 """ 

67 

68 name: str 

69 state: int 

70 

71 

72def state_to_action_text(state: str) -> str: 

73 """state ("open"/"close") を動作テキスト ("開け"/"閉め") に変換""" 

74 return "開け" if state == "open" else "閉め" 

75 

76 

77@dataclass 

78class ShutterStateResponse: 

79 """シャッター状態レスポンスの型定義 

80 

81 Attributes 

82 ---------- 

83 state: シャッター状態のリスト 

84 result: 処理結果 

85 

86 """ 

87 

88 state: list[ShutterStateEntry] = field(default_factory=list) 

89 result: str = "success" 

90 

91 

92# ====================================================================== 

93# スケジュール関連の型定義 

94# ====================================================================== 

95class ScheduleEntry(TypedDict): 

96 """スケジュールエントリの型定義 

97 

98 開く/閉じる制御のスケジュール設定を表します。 

99 

100 Attributes 

101 ---------- 

102 is_active: スケジュールが有効かどうか 

103 time: 実行時刻 (HH:MM形式) 

104 wday: 曜日ごとの有効フラグ [日, 月, 火, 水, 木, 金, 土] 

105 solar_rad: 日射閾値 (W/m^2) 

106 lux: 照度閾値 (LUX) 

107 altitude: 太陽高度閾値 (度) 

108 

109 """ 

110 

111 is_active: bool 

112 time: str 

113 wday: list[bool] 

114 solar_rad: int 

115 lux: int 

116 altitude: int 

117 

118 

119class ScheduleData(TypedDict): 

120 """スケジュールデータ全体の型定義 

121 

122 開く/閉じる両方のスケジュールを保持します。 

123 

124 Attributes 

125 ---------- 

126 open: 開く制御のスケジュール 

127 close: 閉じる制御のスケジュール 

128 

129 """ 

130 

131 open: ScheduleEntry 

132 close: ScheduleEntry