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

Extracting zoneminder to a new library #16527

Merged

Conversation

rohankapoorcom
Copy link
Member

@rohankapoorcom rohankapoorcom commented Sep 10, 2018

Description:

In #15324, @jantman was trying to expose the ZoneMinder run state to Home Assistant and @MartinHjelmare and @balloob asked for the device specific logic to be moved to a new pypi library before any new functionality changes be made.

I've taken a first stab at splitting out the platform with the camera.zoneminder component so far. I was hoping to get a quick review to confirm that I'm on the right track before I continued with migrating out switch.zoneminder and sensor.zoneminder. I have intentionally not set the REQUIREMENTS variable since I have not published the library yet.

I've spun up a new repo to hold the new library and have opened a PR in it for visibility: rohankapoorcom/zm-py#2

This should not require any configuration changes (nor any documentation updates) in Home Assistant.

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If the code communicates with devices, web services, or third-party tools:

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New or updated dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.

homeassistant/components/zoneminder.py Outdated Show resolved Hide resolved
homeassistant/components/zoneminder.py Outdated Show resolved Hide resolved
homeassistant/components/camera/zoneminder.py Outdated Show resolved Hide resolved
homeassistant/components/zoneminder.py Outdated Show resolved Hide resolved


def get_state(api_url):
"""Get a state from the ZoneMinder API service."""
return _zm_request('get', api_url)
global ZM
return ZM._zm_request('get', api_url)
Copy link
Member

Choose a reason for hiding this comment

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

It would make more sense to make a method get_state in the ZoneMinder class. The user of the library should probably not need to know what request method to use or what api url to use, only the state id to get. Or have specific methods for different state ids.

Copy link
Member Author

@rohankapoorcom rohankapoorcom Sep 10, 2018

Choose a reason for hiding this comment

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

Agreed, this is temporary situation (so I didn't [completely] break the sensor/switch) until I was done refactoring.



def change_state(api_url, post_data):
"""Update a state using the Zoneminder API."""
return _zm_request('post', api_url, data=post_data)
global ZM
return ZM._zm_request('post', api_url, data=post_data)
Copy link
Member

Choose a reason for hiding this comment

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

See above. This would only need to accept the new state info and the state id.

'api/monitors/%i.json' % self._monitor_id
)
monitor = zoneminder.get_state(self.hass,
'api/monitors/%i.json' % self._monitor_id)

Choose a reason for hiding this comment

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

line too long (81 > 79 characters)

archived_filter)
)
event = zoneminder.get_state(self.hass,
'api/events/consoleEvents/{}{}.json'.format(

Choose a reason for hiding this comment

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

line too long (81 > 79 characters)

@rohankapoorcom
Copy link
Member Author

@MartinHjelmare I believe everything should be all set here besides publishing zm-py to pypi and setting the REQUIREMENTS variable in the components that need it. If you think this makes sense, I'll go ahead and wrap that last part up.

Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Looks very good. A question and some comments.

homeassistant/components/camera/zoneminder.py Outdated Show resolved Hide resolved
CONF_NAME: monitor.name,
CONF_MJPEG_URL: monitor.mjpeg_image_url,
CONF_STILL_IMAGE_URL: monitor.still_image_url
}
super().__init__(hass, device_info)
Copy link
Member

Choose a reason for hiding this comment

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

hass isn't used by the MjpegCamera init method. It would be nice to clean that up in a separate PR.

Copy link
Member Author

@rohankapoorcom rohankapoorcom Sep 15, 2018

Choose a reason for hiding this comment

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

Sure, I'm happy to take care of that after this one

homeassistant/components/camera/zoneminder.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/zoneminder.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/zoneminder.py Outdated Show resolved Hide resolved
homeassistant/components/switch/zoneminder.py Outdated Show resolved Hide resolved
homeassistant/components/switch/zoneminder.py Outdated Show resolved Hide resolved
homeassistant/components/switch/zoneminder.py Outdated Show resolved Hide resolved
homeassistant/components/zoneminder.py Outdated Show resolved Hide resolved
homeassistant/components/camera/zoneminder.py Outdated Show resolved Hide resolved
@rohankapoorcom rohankapoorcom changed the title WIP: Extracting zoneminder to a new library Extracting zoneminder to a new library Sep 15, 2018
@rohankapoorcom
Copy link
Member Author

rohankapoorcom commented Sep 15, 2018

@MartinHjelmare All comments addressed, library published on pypi and REQUIREMENTS updated. Let me know if anything else is needed here.

Also since I plan on maintaining these components and own the new library, it might make sense to add me to CODEOWNERS for this component/platform.

self._on_state = on_state
self._off_state = off_state
self._state = None

@property
def name(self):
"""Return the name of the switch."""
return "%s State" % self._monitor_name
return '{}\'s State'.format(self._monitor.name)
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we have changed the name here by including an apostrophe. That will also change the entity_id and make this a breaking change. Is that necessary?

Copy link
Member

@MartinHjelmare MartinHjelmare Sep 15, 2018

Choose a reason for hiding this comment

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

Wait, apostrophes should be removed by the core from the entity_id, so maybe this is ok.

Please confirm that we don't change the entity_id.

Copy link
Member Author

@rohankapoorcom rohankapoorcom Sep 15, 2018

Choose a reason for hiding this comment

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

The apostrophe addition was to make the friendly name make more sense:
FrontDoor's State makes more grammatical sense that FrontDoors State (which was the predecessor).

I wanted to drop the s and just make it FrontDoor State but didn't because it would change the entity_id. I can confirm that the entity_id should not change. My switch is still called switch.frontdoors_state.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

This did in fact change the entity names by adding an "s" :(

Copy link
Member Author

Choose a reason for hiding this comment

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

@mnoorenberghe I'm confused. The s was always in the name. The only change I made there was to add an '

Can you give an example of what your switch was named before/after this change?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's incorrect, %s is for the format identifier for a string so %s would be replaced by "Basement" for example. There was no literal 's' before.

Copy link
Contributor

Choose a reason for hiding this comment

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

The previous code was like:

        return "{} State".format(self._monitor_name)

Copy link
Member Author

Choose a reason for hiding this comment

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

D'oh you are right. I totally messed that up when changing it to the .format approach for string formatting. :(

@MartinHjelmare is this something we want to hotfix? Because it's now a breaking change. But fixing it would make another breaking change.

Copy link
Member Author

Choose a reason for hiding this comment

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

#17026 to fix this

conf.get(CONF_PATH_ZMS),
conf.get(CONF_VERIFY_SSL))

return hass.data[DOMAIN].login()
Copy link
Member

Choose a reason for hiding this comment

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

Will this log an error and return False if eg credentials are bad?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Great!

Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Awesome!

@MartinHjelmare MartinHjelmare merged commit 1ca09ea into home-assistant:dev Sep 15, 2018
@ghost ghost removed the in progress label Sep 15, 2018
@MartinHjelmare
Copy link
Member

Please make a new PR and add yourself to codeowners. 👍

@rohankapoorcom rohankapoorcom deleted the split-out-zoneminder branch September 15, 2018 06:47
@rohankapoorcom
Copy link
Member Author

Thanks @MartinHjelmare. I'll follow up with one for removing hass from the MjpegCamera as well.

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

Successfully merging this pull request may close these issues.

5 participants