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

Separate coil configuration from data #83

Closed
elupus opened this issue Jan 1, 2023 · 3 comments
Closed

Separate coil configuration from data #83

elupus opened this issue Jan 1, 2023 · 3 comments

Comments

@elupus
Copy link
Collaborator

elupus commented Jan 1, 2023

Right now the coil object database ends up being state-full, with the value being stored in the object loaded by the json database. This make things somewhat tricky to test and can have odd behaviours with the coil value changing behind ones back.

coil = heatpump.get_coil(1234)
coil.value = 1

await asyncio.sleep()
# During the await, we get an out of band update of coil 1234,
# which will revert it's value to what it was before we changes it's value to 1, let's say 0.

# Now we try to write the coil, which have reverted back to 0.
await connection.write_coil(coil)

The coil value should not be stored in the Coil object.

@yozik04
Copy link
Owner

yozik04 commented Jan 1, 2023

I was initially thinking about it. But decided to have value inside coil. What is wrong with testing?

@elupus
Copy link
Collaborator Author

elupus commented Jan 1, 2023

doing stuff like write_coil.assert_called_with() is hard, since you need to be able to provide a new instance of a Coil object with same parameters as the original Coil object in the data base, but with the expected value.

If you naively grab the Coil from the coil db, and update it's value to the expected value, you break the instance that is you are checking against.

@yozik04
Copy link
Owner

yozik04 commented Feb 6, 2023

I was thinking how to approach that. Decided to create such a class.

@dataclass
class CoilData:
    coil: Coil
    value: Union[int, float, str, None] = None

# def is_valid:
# ... etc ...

Then all read_coil would return an instance of CoilData and write_coil would expect CoilData as input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants