Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add S2 Message Union type #37

Merged
merged 18 commits into from
Jan 23, 2025
Merged

add S2 Message Union type #37

merged 18 commits into from
Jan 23, 2025

Conversation

victorgarcia98
Copy link
Collaborator

Recently, I had the need to wrap the S2 messages in an envelope to simulate accelerated interactions between a CEM and a RM.

Nonetheless, for this to work, the message_type had to be a Literal. This PR refactors the code to use Literals instead of regular fields.

Example usage:

from pydantic import BaseModel, Field
from typing import Union, Annotated,Type, Literal
from datetime import datetime

from s2python.message import S2Message

class MetaData(BaseModel):
    dt : datetime

class S2Wrapper(BaseModel):
    message : S2Message = Field(discriminator='message_type')
    metadata : MetaData

Signed-off-by: Victor Garcia Reolid <[email protected]>
@victorgarcia98 victorgarcia98 added the enhancement New feature or request label Mar 21, 2024
@victorgarcia98 victorgarcia98 self-assigned this Mar 21, 2024
@lfse-slafleur
Copy link
Member

@victorgarcia98 Apologies for a late response. I do not work at TNO anymore and it took some time to get settled in my new situation again. Reviewing now!

development_utilities/get_all_messages.py Show resolved Hide resolved
src/s2python/message.py Show resolved Hide resolved
src/s2python/generated/gen_s2.py Outdated Show resolved Hide resolved
@Flix6x Flix6x self-assigned this Aug 29, 2024
# Conflicts:
#	src/s2python/generated/gen_s2.py
…ut-file-type openapi --output src/s2python/generated/gen_s2.py`

Signed-off-by: F.N. Claessen <[email protected]>
…s2-message/literals

# Conflicts:
#	src/s2python/common/handshake.py
#	src/s2python/common/handshake_response.py
#	src/s2python/common/instruction_status_update.py
#	src/s2python/common/number_range.py
#	src/s2python/common/power_forecast.py
#	src/s2python/common/power_forecast_element.py
#	src/s2python/common/power_forecast_value.py
#	src/s2python/common/power_measurement.py
#	src/s2python/common/power_range.py
#	src/s2python/common/power_value.py
#	src/s2python/common/reception_status.py
#	src/s2python/common/resource_manager_details.py
#	src/s2python/common/revoke_object.py
#	src/s2python/common/role.py
#	src/s2python/common/select_control_type.py
#	src/s2python/common/session_request.py
#	src/s2python/common/timer.py
#	src/s2python/common/transition.py
#	src/s2python/frbc/frbc_actuator_description.py
#	src/s2python/frbc/frbc_actuator_status.py
#	src/s2python/frbc/frbc_fill_level_target_profile.py
#	src/s2python/frbc/frbc_fill_level_target_profile_element.py
#	src/s2python/frbc/frbc_instruction.py
#	src/s2python/frbc/frbc_leakage_behaviour.py
#	src/s2python/frbc/frbc_leakage_behaviour_element.py
#	src/s2python/frbc/frbc_operation_mode.py
#	src/s2python/frbc/frbc_operation_mode_element.py
#	src/s2python/frbc/frbc_storage_description.py
#	src/s2python/frbc/frbc_storage_status.py
#	src/s2python/frbc/frbc_system_description.py
#	src/s2python/frbc/frbc_timer_status.py
#	src/s2python/frbc/frbc_usage_forecast.py
#	src/s2python/frbc/frbc_usage_forecast_element.py
#	src/s2python/generated/gen_s2.py
@Flix6x
Copy link
Collaborator

Flix6x commented Jan 15, 2025

As suggested, I removed all changes relating to the Literal issue. This PR now only contains the addition of the S2Message class and the renaming of the old S2Message class into S2MessageComponent.

(On a side note, Home Assistant has now released a version compatible with Pydantic v2. 😅 )

@Flix6x Flix6x requested a review from lfse-slafleur January 15, 2025 22:42
Copy link
Member

@lfse-slafleur lfse-slafleur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the addition! I added a couple of comments as examples but I believe we should be able to limit a number of types from S2MessageComponent to S2Message as only messages are expected there.

src/s2python/s2_parser.py Outdated Show resolved Hide resolved
src/s2python/s2_parser.py Outdated Show resolved Hide resolved
src/s2python/s2_control_type.py Outdated Show resolved Hide resolved
…ble type "str": "type[PPBCScheduleInstruction]"; expected "str": "Union[type[FRBCActuatorStatus], type[FRBCFillLevelTargetProfile], type[FRBCInstruction], type[FRBCLeakageBehaviour], type[FRBCStorageStatus], <13 more items>]" [dict-item]`

Signed-off-by: F.N. Claessen <[email protected]>
Signed-off-by: F.N. Claessen <[email protected]>
@Flix6x Flix6x self-requested a review January 23, 2025 11:04
Copy link
Collaborator

@Flix6x Flix6x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved by Víctor and I.

@Flix6x Flix6x merged commit cb08e31 into main Jan 23, 2025
19 checks passed
@Flix6x Flix6x deleted the generic-s2-message branch January 23, 2025 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants