|
12 | 12 | ServiceCall, |
13 | 13 | ServiceResponse, |
14 | 14 | SupportsResponse, |
| 15 | + callback, |
15 | 16 | ) |
16 | 17 | from homeassistant.exceptions import ServiceValidationError |
17 | 18 | from homeassistant.helpers import config_validation as cv, selector |
|
70 | 71 | ) |
71 | 72 |
|
72 | 73 |
|
73 | | -def setup_services(hass: HomeAssistant) -> None: |
74 | | - """Set up the services for the seventeentrack integration.""" |
| 74 | +async def _get_packages(call: ServiceCall) -> ServiceResponse: |
| 75 | + """Get packages from 17Track.""" |
| 76 | + config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID] |
| 77 | + package_states = call.data.get(ATTR_PACKAGE_STATE, []) |
75 | 78 |
|
76 | | - async def get_packages(call: ServiceCall) -> ServiceResponse: |
77 | | - """Get packages from 17Track.""" |
78 | | - config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID] |
79 | | - package_states = call.data.get(ATTR_PACKAGE_STATE, []) |
| 79 | + await _validate_service(call.hass, config_entry_id) |
80 | 80 |
|
81 | | - await _validate_service(config_entry_id) |
| 81 | + seventeen_coordinator: SeventeenTrackCoordinator = call.hass.data[DOMAIN][ |
| 82 | + config_entry_id |
| 83 | + ] |
| 84 | + live_packages = sorted( |
| 85 | + await seventeen_coordinator.client.profile.packages( |
| 86 | + show_archived=seventeen_coordinator.show_archived |
| 87 | + ) |
| 88 | + ) |
82 | 89 |
|
83 | | - seventeen_coordinator: SeventeenTrackCoordinator = hass.data[DOMAIN][ |
84 | | - config_entry_id |
| 90 | + return { |
| 91 | + "packages": [ |
| 92 | + _package_to_dict(package) |
| 93 | + for package in live_packages |
| 94 | + if slugify(package.status) in package_states or package_states == [] |
85 | 95 | ] |
86 | | - live_packages = sorted( |
87 | | - await seventeen_coordinator.client.profile.packages( |
88 | | - show_archived=seventeen_coordinator.show_archived |
89 | | - ) |
90 | | - ) |
| 96 | + } |
91 | 97 |
|
92 | | - return { |
93 | | - "packages": [ |
94 | | - package_to_dict(package) |
95 | | - for package in live_packages |
96 | | - if slugify(package.status) in package_states or package_states == [] |
97 | | - ] |
98 | | - } |
99 | 98 |
|
100 | | - async def add_package(call: ServiceCall) -> None: |
101 | | - """Add a new package to 17Track.""" |
102 | | - config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID] |
103 | | - tracking_number = call.data[ATTR_PACKAGE_TRACKING_NUMBER] |
104 | | - friendly_name = call.data[ATTR_PACKAGE_FRIENDLY_NAME] |
| 99 | +async def _add_package(call: ServiceCall) -> None: |
| 100 | + """Add a new package to 17Track.""" |
| 101 | + config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID] |
| 102 | + tracking_number = call.data[ATTR_PACKAGE_TRACKING_NUMBER] |
| 103 | + friendly_name = call.data[ATTR_PACKAGE_FRIENDLY_NAME] |
105 | 104 |
|
106 | | - await _validate_service(config_entry_id) |
| 105 | + await _validate_service(call.hass, config_entry_id) |
107 | 106 |
|
108 | | - seventeen_coordinator: SeventeenTrackCoordinator = hass.data[DOMAIN][ |
109 | | - config_entry_id |
110 | | - ] |
| 107 | + seventeen_coordinator: SeventeenTrackCoordinator = call.hass.data[DOMAIN][ |
| 108 | + config_entry_id |
| 109 | + ] |
111 | 110 |
|
112 | | - await seventeen_coordinator.client.profile.add_package( |
113 | | - tracking_number, friendly_name |
114 | | - ) |
| 111 | + await seventeen_coordinator.client.profile.add_package( |
| 112 | + tracking_number, friendly_name |
| 113 | + ) |
115 | 114 |
|
116 | | - async def archive_package(call: ServiceCall) -> None: |
117 | | - config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID] |
118 | | - tracking_number = call.data[ATTR_PACKAGE_TRACKING_NUMBER] |
119 | 115 |
|
120 | | - await _validate_service(config_entry_id) |
| 116 | +async def _archive_package(call: ServiceCall) -> None: |
| 117 | + config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID] |
| 118 | + tracking_number = call.data[ATTR_PACKAGE_TRACKING_NUMBER] |
121 | 119 |
|
122 | | - seventeen_coordinator: SeventeenTrackCoordinator = hass.data[DOMAIN][ |
123 | | - config_entry_id |
124 | | - ] |
| 120 | + await _validate_service(call.hass, config_entry_id) |
125 | 121 |
|
126 | | - await seventeen_coordinator.client.profile.archive_package(tracking_number) |
127 | | - |
128 | | - def package_to_dict(package: Package) -> dict[str, Any]: |
129 | | - result = { |
130 | | - ATTR_DESTINATION_COUNTRY: package.destination_country, |
131 | | - ATTR_ORIGIN_COUNTRY: package.origin_country, |
132 | | - ATTR_PACKAGE_TYPE: package.package_type, |
133 | | - ATTR_TRACKING_INFO_LANGUAGE: package.tracking_info_language, |
134 | | - ATTR_TRACKING_NUMBER: package.tracking_number, |
135 | | - ATTR_LOCATION: package.location, |
136 | | - ATTR_STATUS: package.status, |
137 | | - ATTR_INFO_TEXT: package.info_text, |
138 | | - ATTR_FRIENDLY_NAME: package.friendly_name, |
139 | | - } |
140 | | - if timestamp := package.timestamp: |
141 | | - result[ATTR_TIMESTAMP] = timestamp.isoformat() |
142 | | - return result |
143 | | - |
144 | | - async def _validate_service(config_entry_id): |
145 | | - entry: ConfigEntry | None = hass.config_entries.async_get_entry(config_entry_id) |
146 | | - if not entry: |
147 | | - raise ServiceValidationError( |
148 | | - translation_domain=DOMAIN, |
149 | | - translation_key="invalid_config_entry", |
150 | | - translation_placeholders={ |
151 | | - "config_entry_id": config_entry_id, |
152 | | - }, |
153 | | - ) |
154 | | - if entry.state != ConfigEntryState.LOADED: |
155 | | - raise ServiceValidationError( |
156 | | - translation_domain=DOMAIN, |
157 | | - translation_key="unloaded_config_entry", |
158 | | - translation_placeholders={ |
159 | | - "config_entry_id": entry.title, |
160 | | - }, |
161 | | - ) |
| 122 | + seventeen_coordinator: SeventeenTrackCoordinator = call.hass.data[DOMAIN][ |
| 123 | + config_entry_id |
| 124 | + ] |
| 125 | + |
| 126 | + await seventeen_coordinator.client.profile.archive_package(tracking_number) |
| 127 | + |
| 128 | + |
| 129 | +def _package_to_dict(package: Package) -> dict[str, Any]: |
| 130 | + result = { |
| 131 | + ATTR_DESTINATION_COUNTRY: package.destination_country, |
| 132 | + ATTR_ORIGIN_COUNTRY: package.origin_country, |
| 133 | + ATTR_PACKAGE_TYPE: package.package_type, |
| 134 | + ATTR_TRACKING_INFO_LANGUAGE: package.tracking_info_language, |
| 135 | + ATTR_TRACKING_NUMBER: package.tracking_number, |
| 136 | + ATTR_LOCATION: package.location, |
| 137 | + ATTR_STATUS: package.status, |
| 138 | + ATTR_INFO_TEXT: package.info_text, |
| 139 | + ATTR_FRIENDLY_NAME: package.friendly_name, |
| 140 | + } |
| 141 | + if timestamp := package.timestamp: |
| 142 | + result[ATTR_TIMESTAMP] = timestamp.isoformat() |
| 143 | + return result |
| 144 | + |
| 145 | + |
| 146 | +async def _validate_service(hass: HomeAssistant, config_entry_id: str) -> None: |
| 147 | + entry: ConfigEntry | None = hass.config_entries.async_get_entry(config_entry_id) |
| 148 | + if not entry: |
| 149 | + raise ServiceValidationError( |
| 150 | + translation_domain=DOMAIN, |
| 151 | + translation_key="invalid_config_entry", |
| 152 | + translation_placeholders={ |
| 153 | + "config_entry_id": config_entry_id, |
| 154 | + }, |
| 155 | + ) |
| 156 | + if entry.state != ConfigEntryState.LOADED: |
| 157 | + raise ServiceValidationError( |
| 158 | + translation_domain=DOMAIN, |
| 159 | + translation_key="unloaded_config_entry", |
| 160 | + translation_placeholders={ |
| 161 | + "config_entry_id": entry.title, |
| 162 | + }, |
| 163 | + ) |
| 164 | + |
| 165 | + |
| 166 | +@callback |
| 167 | +def async_setup_services(hass: HomeAssistant) -> None: |
| 168 | + """Set up the services for the seventeentrack integration.""" |
162 | 169 |
|
163 | 170 | hass.services.async_register( |
164 | 171 | DOMAIN, |
165 | 172 | SERVICE_GET_PACKAGES, |
166 | | - get_packages, |
| 173 | + _get_packages, |
167 | 174 | schema=SERVICE_GET_PACKAGES_SCHEMA, |
168 | 175 | supports_response=SupportsResponse.ONLY, |
169 | 176 | ) |
170 | 177 |
|
171 | 178 | hass.services.async_register( |
172 | 179 | DOMAIN, |
173 | 180 | SERVICE_ADD_PACKAGE, |
174 | | - add_package, |
| 181 | + _add_package, |
175 | 182 | schema=SERVICE_ADD_PACKAGE_SCHEMA, |
176 | 183 | ) |
177 | 184 |
|
178 | 185 | hass.services.async_register( |
179 | 186 | DOMAIN, |
180 | 187 | SERVICE_ARCHIVE_PACKAGE, |
181 | | - archive_package, |
| 188 | + _archive_package, |
182 | 189 | schema=SERVICE_ARCHIVE_PACKAGE_SCHEMA, |
183 | 190 | ) |
0 commit comments